Socket
Socket
Sign inDemoInstall

oro-functions

Package Overview
Dependencies
35
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    oro-functions

The Class Ofn extends Ofn-client and includes additional static helper functions for: URLs, Files, Operating System.


Version published
Weekly downloads
55
increased by323.08%
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

2.1.1 / 2024-04-26

  • Improved github cicd replacing actions/--@v3 by actions/--@v4.
  • Fixed prettier endOfLine: 'lf'
  • Updated libs:
    • oro-functions-client from v2.2.0 to v2.2.2.
    • zip-lib from v1.0.0 to v1.0.4.
  • Added dev libs:
    • eslint-plugin-github@^4.10.2
  • Updated dev libs:
    • @babel/core from v7.23.9 to v7.24.4.
    • @babel/preset-env from v7.23.9 to v7.24.4.
    • @babel/preset-typescript from v7.23.3 to v7.24.1.
    • @typescript-eslint/eslint-plugin from v7.0.2 to v7.7.1.
    • @typescript-eslint/parser from v7.0.2 to v7.7.1.
    • eslint-plugin-jest from v27.9.0 to v28.2.0.
    • eslint-plugin-unicorn from v51.0.1 to v52.0.0.
    • typescript from v5.3.3 to v5.4.5.

Readme

Source

Oro Functions

  • Overview
  • Installation
  • Example
  • Methods

Overview

Ofn contains utility static methods (helpers).

This package ( oro-functions ) is divided from oro-functions-client to allow using that functions in js-client frameworks like Vuejs or React.

Class oro-functions is extended from oro-functions-client.

If you want to know all fns, read oro-functions-client.

Functions could be divided in groups:
⇢ (Extended)
· URLs
· Crypto
· Files
· Operating System
· Ports
· Console
⇢ (Client)
· General
· Numbers
· String
· Crypto
· Functions
· Classes
· Objects
· Arrays
· Dates
· URLs
· Files
· PHP Serialize
· Response

Installation

npm install oro-functions

Example:

// cjs
const { Ofn } = require( 'oro-functions' );

// mjs, ts
import { Ofn } from 'oro-functions';

Ofn.type( [ 1, 2, 3 ] ); // -> 'array'

also every method could be called individually:

// cjs
const { type } = require( 'oro-functions' );

// mjs, ts
import { type } from 'oro-functions';

type( [ 1, 2, 3 ] ); // -> 'array'

Methods



Oro Functions Client

In Ofn there are all functions of Oro Functions Client.


Extended Functions


URLs


Ofn.jwkTokenDecode()
jwkTokenDecode( token: string ) => string;
Ofn.jwkTokenDecode(
  'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoib3JvcGVzYSIsImlhdCI6MTYyOTcxMzM2MywiZXhwIjoxNjI5NzIwNTYzfQ.2zL8FzvFQCtuqi0fFoby4QVCXSi2pWNS3bzCU53Vd4M',
);
// -> '{"user":"oropesa","iat":1629713363,"exp":1629720563}'

Crypto


Ofn.cryptoGenerateKeyPair()
Ofn.cryptoGenerateKeyPair(
  passphrase?: string,
  options?: CryptoKeyPairOptions
) => Promise<CryptoKeyPairResponse>

interface CryptoKeyPairOptions {
  type?: string;
  modulusLength?: number;
  publicKeyEncodingType?: string;
  publicKeyEncodingFormat?: string;
  privateKeyEncodingType?: string;
  privateKeyEncodingFormat?: string;
  privateKeyEncodingCipher?: string;
}

type CryptoKeyPairResponse = SResponse<
  CryptoKeyPairObject,  // as SResponseOK
  CryptoKeyPairError    // as SResponseKO
>;

interface SResponseOK {
  status: true,
  passphrase: string;
  publicKey: string;
  privateKey: string;
}

interface SResponseKO {
  status: false,
  error: {
    msg: string;
    err: Error;
  }
}

interface CryptoKeyPairObject {
  passphrase: string;
  publicKey: string;
  privateKey: string;
}

interface CryptoKeyPairError {
  msg: string;
  err: Error;
}

await Ofn.cryptoGenerateKeyPair('example');
// {
//   passphrase: 'example',
//   publicKey: '-----BEGIN RSA PUBLIC KEY-----',
//   privateKey: '-----BEGIN RSA PRIVATE KEY-----'
// }
  • Default options
options = {
  type: 'rsa', // 'rsa', 'rsa-pss', 'dsa', 'ec', 'ed25519', 'ed448', 'x25519', 'x448', 'dh'.
  modulusLength: 4096,
  publicKeyEncodingType: 'spki', // 'pkcs1' (RSA only) or 'spki'.
  publicKeyEncodingFormat: 'pem', // 'pem', 'der', or 'jwk'.
  privateKeyEncodingType: 'pkcs8', // 'pkcs1' (RSA only), 'pkcs8' or 'sec1' (EC only).
  privateKeyEncodingFormat: 'pem', // 'pem', 'der', or 'jwk'.
  privateKeyEncodingCipher: 'aes-256-cbc', // 'aes-256-cbc', 'des-cbc-sha', 'rc4-128-md5', ...
};

Files


await Ofn.obtainOConfig()
Ofn.obtainOConfigSync()
await Ofn.obtainOConfig<T extends object = OConfigDefaultParams>
    ( args?: OConfigArgs ) => Promise<OConfigResponse<T>>;

Ofn.obtainOConfigSync<T extends object = OConfigDefaultParams>
    ( args?: OConfigArgs ) => OConfigResponse<T>;

interface OConfigArgs {
  file?: string;
  deep?: number;
  defaultParams?: string[];
  extraParams?: string[];
}

interface OConfigDefaultParams {
  environment: string;
  projectname: string;
  projectserver: string;
}

type OConfigResponse<T extends object = OConfigDefaultParams> =
    SResponse<
      OConfigObject<T>, // as SResponseOK
      OConfigError      // as SResponseKO
    >;

interface SResponseOK {
  status: true,
  config: T;
}

interface SResponseKO {
  status: false,
  error: {
    msg: string;
    args?: OConfigArgs;
  }
}

interface OConfigObject<T extends object = OConfigDefaultParams> {
  config: T;
}

interface OConfigError {
  msg: string;
  args?: OConfigArgs;
}

Put yourself in this situation, you work with Git and you create modules or projects that need the common file .env to use custom variables, with data about, i.e., connect to dbs, users-passwords, dev-pro or other envs, custom configs.

Then, you have something like:

- parent-folder/
  ├─ project-1/  #git-project-1
  │   └...
  ├─ project-2/  #git-project-2
  │   └...
  └...

Instead of create global variables in the system, or duplicate the file .env, you can centralize all data in oro-config.json.

This file, oro-config.json, could be in the same project and in the parent folders too, in such a way that the final json is merged and overwritten from the parent folders to the project.

So, using this example, you have:

- parent-folder/
  ├─ project-1/
  │   ├─ oro-config.json #json of project 1
  │   └...
  ├─ project-2/
  │   ├─ oro-config.json #json of project 2
  │   └...
  ├─ oro-config.json #json of parent-folder
  └...

Continuing, the oro-config.json files have the next data:

// oro-config.json of Parent Folder
{
  "environment": "dev",
  "projectserver": "laptop-dev",
  "nuxt": {
    "server": "localhost",
    "defaultRole": "default"
  }
}

// oro-config.json of Project 1
{
  "projectname": "project-1"
}

// oro-config.json of Project 2
{
  "projectname": "project-2",
  "nuxt": {
    "pageName": "The Project 2",
    "defaultRole": "client"
  }
}

Finally, the json result is:

/* Inside project 1 */

let obtainConfig = await Ofn.obtainOConfig( { deep: 1 } );
// {
//   status: true,
//   config: {
//     environment: 'dev',
//     projectserver: 'laptop-dev',
//     projectname: 'project-1',
//     nuxt: {
//       server: 'localhost',
//       defaultRole: 'default'
//     }
//   }
// }

/* Inside project 2 */

let obtainConfig = await Ofn.obtainOConfig( { deep: 1 } );
// {
//   status: true,
//   config: {
//     environment: 'dev',
//     projectserver: 'laptop-dev',
//     projectname: 'project-2',
//     nuxt: {
//       server: 'localhost',
//       pageName: 'The Project 2',
//       defaultRole: 'client'
//     }
//   }
// }
  • Default args
let args = {
  file: 'oro-config.json',
  deep: 0,
  defaultParams: ['environment', 'projectname', 'projectserver'],
  extraParams: [],
};

By default, there are 3 params that are required in json: environment, projectname, projectserver.

So, if there are missing, the response of Ofn.obtainOConfig() is { status: false, ... }.

Alternatively, you can change defaultParams, or you can add extraParams to be required.

On the other hand, you can change de file name to search, so instead of oro-config.json, you can use custom.json.

Finally, you choose the deep from parents to look for.


await Ofn.getFileJsonRecursively()
Ofn.getFileJsonRecursivelySync()
await Ofn.getFileJsonRecursively<T>
    ( filenameOrPath: string, parentDeep?: number ) => Promise<T>;

Ofn.getFileJsonRecursivelySync<T>
    ( filenameOrPath: string, parentDeep?: number ) => T;

Having this case:

- main/
  ├─ folder/
  │   ├─ subfolder/
  │   │   ├─ index.js
  │   │   └─ custom.json
  │   └ custom.json
  └ custom.json

The final json is merged and overwritten from the parents to the project file.

// in index.js
let custom = await Ofn.getFileJsonRecursively('custom.json', 2);
// { ... }

await Ofn.globFiles()
await Ofn.globFiles = (
  folderPath: string | string[],
  globArgs?: GlobFilesOptions,
) => Promise<string[]>;
await Ofn.globFiles(`folder/*`);
// [
//   `folder/example.txt`,
//   ...
// ]
// default
globArgs = {
  dot: true,
  unique: true,
  onlyFiles: true,
  ignore: ['node_modules/**', '.zero/**'],
};

await Ofn.folderIsEmpty()
await Ofn.folderIsEmpty = (
  folderPath: string,
  globArgs?: GlobFilesOptions,
) => Promise<boolean>;
await Ofn.folderIsEmpty(`folder/`); // false
  • Default args

This function is a wrapper of fast-glob with default args. so GlobFilesOptions are the same as fast-glob Options

// default
globArgs = {
  dot: true,
  unique: true,
  onlyFiles: true,
  ignore: ['node_modules/**', '.zero/**'],
};

await Ofn.pathIsFolder()
await Ofn.pathIsFolder = ( folderPath: string ) => Promise<boolean>;
await Ofn.pathIsFolder(`folder`);
// -> true

await Ofn.zipFolder()
await Ofn.zipFolder = ( folderPath: string, zipPath?: string ) => Promise<ZipFolderResponse>;

type ZipFolderResponse = SResponse<
  ZipFolderObject, // as SResponseOK
  ZipFolderError   // as SResponseKO
>;

interface SResponseOK {
  status: true,
  zipPath: string;
}

interface SResponseKO {
  status: false,
  error: {
    msg: string;
    folderPath?: string;
    zipPath?: string;
  }
}

interface ZipFolderObject {
  zipPath: string;
}

interface ZipFolderError {
  msg: string;
  folderPath?: string;
  zipPath?: string;
}
await Ofn.zipFolder(`folder`, 'folder.zip');
// -> { status: true, zipPath: 'folder.zip' }

Operating System


Ofn.osPlatform()
Ofn.osPlatform = () => NodeJS.Platform;
Ofn.osPlatform();
// -> 'win32' || 'darwin' || 'linux' || ...

Ofn.osIsWindows()
Ofn.osIsWindows = () => boolean;
Ofn.osIsWindows();
// -> true

Ofn.osIsMac()
Ofn.osIsMac = () => boolean;
Ofn.osIsMac();
// -> false

Ofn.osIsLinux()
Ofn.osIsLinux = () => boolean;
Ofn.osIsLinux();
// -> true

Ofn.osIsAndroid()
Ofn.osIsAndroid = () => boolean;
Ofn.osIsAndroid();
// -> true

Ports


await Ofn.isPortFree()
await Ofn.isPortFree = ( port: number ) => Promise<IsPortFreeResponse>;

type IsPortFreeResponse = SResponse<
  PortFreeObject, // as SResponseOK
  IsPortFreeError // as SResponseKO
>;

interface SResponseOK {
  status: true,
  port: number;
}

interface SResponseKO {
  status: false,
  error: {
    msg: string;
    port: number;
  }
}

interface PortFreeObject {
  port: number;
}

interface IsPortFreeError {
  msg: string;
  port: number;
}
await Ofn.isPortFree(3000);
// -> { status: true, port: 3000 }

await Ofn.getPortFree()
await Ofn.getPortFree = ( portStart?: number | number[], portEnd?: number )
    => Promise<GetPortFreeResponse>;

type GetPortFreeResponse = SResponse<
  PortFreeObject,  // as SResponseOK
  GetPortFreeError // as SResponseKO
>;

interface SResponseOK {
  status: true,
  port: number;
}

interface SResponseKO {
  status: false,
  error: {
    msg: string;
    port?: number;
    opts?: {
      random?: boolean;
      port?: number;
      ports?: number[];
      portRange?: number[];
    };
    err?: any;
  }
}

interface PortFreeObject {
  port: number;
}

interface GetPortFreeError {
  msg: string;
  port?: number;
  opts?: {
    random?: boolean;
    port?: number;
    ports?: number[];
    portRange?: number[];
  };
  err?: any;
}
await Ofn.getPortFree();
// -> { status: true, port: 60247 } #random

await Ofn.getPortFree(3000);
// -> { status: true, port: 3000 } #if not allowed, return random

await Ofn.getPortFree([3000, 3001, 3002]);
// -> { status: true, port: 3000 }
// -> { status: false, error: { msg: 'No available ports in array [ 3000, 3001, 3002 ]' } }

await Ofn.getPortFree(3000, 3100);
// -> { status: true, port: 3000 }
// -> { status: false, error: { msg: 'No available ports in range 3000-3100' } }

Console


Ofn.processWrite()
Ofn.processWrite(
  strOrObject: string | ProcessWriteObject,
  color?: string,
  bg?: string
) => string;

type ProcessWriteObject =
    | { s?: string, c?: string, b?: string }
    | { str?: string, cl?: string, bg?: string }
    | { string?: string, color?: string, background?: string }
Ofn.processWrite('info', 'blue');
Ofn.processWrite(' Doing some stuff... ');

Ofn.processWrite({ s: 'Error!', c: 'red', b: 'redlight' });
Ofn.processWrite('\n');
  • Example:

Example Console - Process Write

Note: first param could be a string or an object

  • Allowed Object:

    • s, str, or string
    • c, cl, or color
    • b, bg, or background
  • Allowed Colors and Background:

    • gray
    • red
    • green
    • white
    • yellow
    • blue
    • redlight
    • bluelight

Ofn.processWrites()
Ofn.processWrites( arr: Array<string | ProcessWriteObject> ) => string;

type ProcessWriteObject =
    | { s?: string, c?: string, b?: string }
    | { str?: string, cl?: string, bg?: string }
    | { string?: string, color?: string, background?: string }
Ofn.processWrites([
  { s: ' info ', c: 'blue', b: 'bluelight' },
  ' Doing some stuff... ',
  { s: 'Error!', c: 'red', b: 'redlight' },
  '\n',
]);
  • Example:

Example Console - Process Writes

Keywords

FAQs

Last updated on 26 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc