oro-functions-client
TypeScript icon, indicating that this package has built-in type declarations

2.3.3 • Public • Published

Oro Functions Client

Overview

Ofn contains utility static methods (helpers).

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

If you are going to use this package in js-server environments like Nodejs, it's recommended to use oro-functions instead.

Functions could be divided in groups:
· General
· Numbers
· String
· Crypto
· Functions
· Classes
· Objects
· Arrays
· Dates
· URLs
· Files
· PHP Serialize
· Response

Installation

npm install oro-functions-client

Example:

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

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

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

also every method could be called individually:

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

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

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

Methods


General

Ofn.issetGet()

Ofn.issetGet<
  T,
  O extends Record<string | number, any> = Record<string | number, any>, // object | array
  K extends string | number = string | number
>( obj: O, key: K, def?: T, allowFalsy = true ) => T | null;

// < T output-type, O object-interface, K allowed-keys>
// Note: 'key' should be number when 'obj' is an array
// js
const obj1 = { name: 'Foo', label: 'foo' };
const obj2 = { name: 'Foo', label: '' };
const obj3 = { name: 'Foo' };

Ofn.issetGet(obj1, 'label', 'default');
// -> 'foo'
Ofn.issetGet(obj2, 'label', 'default');
// -> ''
Ofn.issetGet(obj2, 'label', 'default', false);
// -> 'default'
Ofn.issetGet(obj3, 'label', 'default');
// -> 'default'
// ts
interface Custom {
  name: string;
  label?: string;
}
type AllowedKeys = 'label' | 'another-label';

const obj1: Custom = { name: 'Foo', label: 'foo' };
const obj2: Custom = { name: 'Foo', label: '' };
const obj3: Custom = { name: 'Foo' };

Ofn.issetGet<string>(obj1, 'label', 'default');
// -> 'foo'
Ofn.issetGet<string, Custom>(obj2, 'label', 'default');
// -> ''
Ofn.issetGet<string, Custom, AllowedKeys>(obj2, 'label', 'default', false);
// -> 'default'
Ofn.issetGet<string, Custom, keyof Custom>(obj3, 'label', 'default');
// -> 'default'

Ofn.type()

Ofn.type( value: any, strict?: boolean ) => string;
Ofn.type('foo'); // -> 'string'
Ofn.type(true); // -> 'boolean'
Ofn.type(12); // -> 'number'
Ofn.type({}); // -> 'object'
Ofn.type([]); // -> 'array'
Ofn.type(function () {}); // -> 'function'
Ofn.type(new Date()); // -> 'date'
// Note: when obj is an obj-class

class MyClass {}
const foo = new MyClass();

Ofn.type(MyClass); // -> 'class'
Ofn.type(foo); // -> 'object'
Ofn.type(foo, true); // -> 'MyClass'

Ofn.isArray()

Ofn.isArray( value: any ) => boolean;
Ofn.isArray([1, 2, 3]); // -> true

Ofn.isBoolean()

Ofn.isBoolean( value: any ) => boolean;
Ofn.isBoolean(true); // -> true
Ofn.isBoolean(false); // -> true

Ofn.isClass()

Ofn.isClass( value: any ) => boolean;
Ofn.isClass(Ofn); // -> true

Ofn.isDate()

Ofn.isDate( value: any ) => boolean;
Ofn.isDate(new Date()); // -> true

Ofn.isFunction()

Ofn.isFunction( value: any ) => boolean;
Ofn.isFunction(function () {}); // -> true
Ofn.isFunction(() => {}); // -> true

Ofn.isNull()

Ofn.isNull( value: any ) => boolean;
Ofn.isNull(); // -> false
Ofn.isNull(null); // -> true

Ofn.isNully()

Ofn.isNully( value: any ) => boolean;
Ofn.isNully(); // -> true
Ofn.isNully(null); // -> true

Ofn.isNumber()

Ofn.isNumber( value: any ) => boolean;
Ofn.isNumber(12); // -> true
Ofn.isNumber(1.2); // -> true
Ofn.isNumber(NaN); // -> true

Ofn.isObject()

Ofn.isObject( value: any ) => boolean;
Ofn.isObject({}); // true

Ofn.isRegexp()

Ofn.isRegtsexp( value: any ) => boolean;
Ofn.isRegexp(/([A-Z])\w+/); // -> true

Ofn.isString()

Ofn.isString( value: any ) => boolean;
Ofn.isString('foo'); // -> true

Ofn.isUndefined()

Ofn.isUndefined( value: any ) => boolean;
Ofn.isUndefined(); // -> true
Ofn.isUndefined(null); // -> false

Numbers

Ofn.isNumeric()

Ofn.isNumeric( value: any ) => boolean;
Ofn.isNumeric(NaN); // -> false
Ofn.isNumeric(2.14); // -> true
Ofn.isNumeric('2.14'); // -> true
Ofn.isNumeric('2,14'); // -> false
// Note: for dislexia issues (with Ofn.isNumber)
Ofn.isNumberic(num);

Ofn.isEven()

Ofn.isEven( num: number ) => boolean;
Ofn.isEven(0); // -> true
Ofn.isEven(1); // -> false

Ofn.isEven(NaN); // -> false
Ofn.isEven(0.2); // -> true
Ofn.isEven(1.2); // -> false

Ofn.isOdd()

Ofn.isOdd( num: number ) => boolean;
Ofn.isOdd(0); // -> false
Ofn.isOdd(1); // -> true

Ofn.isOdd(NaN); // -> false
Ofn.isOdd(0.2); // -> false
Ofn.isOdd(1.2); // -> true

Ofn.numberFixDecimals()

Ofn.numberFixDecimals(
  num: number | string,
  decimalLength?: number | false,   // def: 2
  allowAllRightZeros?: boolean,     // def: true
  minRightZeros?: number | boolean, // def: true
) => string;
Ofn.numberFixDecimals('2,1499'); // -> '2.15'
Ofn.numberFixDecimals('2.1499'); // -> '2.15'

Ofn.numberFixDecimals('2.14990', 0); // -> '2'
Ofn.numberFixDecimals('2.14990', 3); // -> '2.150'
Ofn.numberFixDecimals('2,14990', 6); // -> '2.149000'
Ofn.numberFixDecimals('2.14990', false); // -> '2.149'

Ofn.numberFixDecimals('2.14990', 6, false); // -> '2.1499'
Ofn.numberFixDecimals('2.14990', 3, false); // -> '2.15'
Ofn.numberFixDecimals('2.14990', 3, false, 3); // -> '2.150'

Ofn.numberFixDecimals('2.1387', 3, false, 2); // -> '2.139'
Ofn.numberFixDecimals('2.1999', 3, false, 2); // -> '2.20'

// More examples

Ofn.numberFixDecimals('2.1999', 2); // -> '2.20'
Ofn.numberFixDecimals('2', 2); // -> '2.00'

Ofn.numberFixDecimals('2.1999', 2, true, false); // -> '2.20'
Ofn.numberFixDecimals('2', 2, true, false); // -> '2'

Ofn.numberPrintDecimals()

Ofn.numberPrintDecimals(
  num: number | string,
  decimalLength?: number | false,   // def: 2
  allowAllRightZeros?: boolean,     // def: true
  minRightZeros?: number | boolean, // def: 2
) => string;

Is the same as Ofn.numberFixDecimals, but the separator is a comma 2,10, not a dot 2.10.

It's better to print, for example, in excel fields.

Ofn.numberPrintDecimals('2,1499'); // -> '2,15'
Ofn.numberPrintDecimals('2.1499'); // -> '2,15'

Ofn.randomMinMax()

Ofn.randomMinMax( min?: number, max?: number ) => number;
Ofn.randomMinMax(); // -> random between 0 - 100
Ofn.randomMinMax(1, 10); // -> random between 1 - 10

Ofn.randomMinMax(10, 1); // -> random between 1 - 10
Ofn.randomMinMax(50); // -> random between 50 - 100

String

Ofn.capitalize()

Ofn.capitalize( str: string ) => string;
Ofn.capitalize('foo'); // -> 'Foo'
Ofn.capitalize('foo bar'); // -> 'Foo bar'

Ofn.chunkStringByCharSize()

Ofn.chunkStringByCharSize(
  str: string,
  char: string,
  size: number,
  orientation?: LeftRight  // def: 'left'
) => string;

type LeftRight = 'left' | 'right';
Ofn.chunkStringByCharSize('1234567', '_', 2);
// -> '12_34_56_7'
Ofn.chunkStringByCharSize('1234567', '.', 3, 'right');
// -> '1.234.567'

Ofn.escAttr()

Ofn.escAttr( str: string ) => string;
Ofn.escAttr('<span>foo</span>');
// -> '&amp;lt;span&amp;gt;foo&amp;lt;/span&amp;gt;'

Ofn.isStringJson()

Ofn.isStringJson( str: string, allowArray?: boolean ) => boolean;
Ofn.isStringJson('{ "foo": "bar" }'); // -> true
Ofn.isStringJson('[ "foo", "bar" ]'); // -> false
Ofn.isStringJson('[ "foo", "bar" ]', true); // -> true

Ofn.jsonParse()

Ofn.jsonParse<T>( str: string, strict?: boolean ) => strict === true ? T | null : T | string
Ofn.jsonParse('foo'); // -> 'foo'
Ofn.jsonParse('foo', true); // -> null

Ofn.jsonParse('{"foo":true}'); // -> { foo: true }

Ofn.jsonParse('[ 1, 2, 3 ]'); // -> [ 1, 2, 3 ]
Ofn.jsonParse('{"0":1,"1":2,"2":3}'); // -> [ 1, 2, 3 ]
Ofn.jsonParse('{"0":1,"1":2,"2":3}', true); // -> { "0": 1, "1": 2, "2": 3 }

Ofn.jsonStringify()

Ofn.jsonStringify<T extends Record<string | number, any>>(
  json: T,
  beautify = false,
) => string;
Ofn.jsonStringify('foo'); // -> ''
Ofn.jsonStringify({ foo: true }); // -> '{"foo":true}'
Ofn.jsonStringify({ foo: true }, true); // -> `{\n  "foo": true\n}`

Ofn.jsonStringify([1, 2, 3]); // -> '{"0":1,"1":2,"2":3}'

Ofn.jsonize()

Ofn.jsonize<T>( str: string, strict?: boolean ) => strict === true ? T | null : T | string;
Ofn.jsonize('foo'); // -> 'foo'
Ofn.jsonize('foo', true); // -> null

Ofn.jsonize('{"foo":true}'); // -> { foo: true }

Ofn.jsonize('[ 1, 2, 3 ]'); // -> [ 1, 2, 3 ]
Ofn.jsonize('{"0":1,"1":2,"2":3}'); // -> [ 1, 2, 3 ]

// note: to avoid cast array-object to array, use: Ofn.jsonParse(string, true)

Ofn.arrayize()

Ofn.arrayize< T, O extends Record<string, any> = Record<string, any> > ( strOrObject: string | O, strict?: boolean )
=> strict === true ? T | null : T | string | O;
Ofn.arrayize('foo'); // -> 'foo'
Ofn.arrayize('foo', true); // -> null

Ofn.arrayize('{"foo":true}'); // -> [] with property foo = true
Ofn.arrayize({ foo: true }); // -> [] with property foo = true

Ofn.arrayize('[ 1, 2, 3 ]');
// -> [ 1, 2, 3 ]
Ofn.arrayize('{ "0":1, "2":3, "foo":true }');
// -> [ 1, undefined, 3 ] with property foo = true

Ofn.arrayizeString()

Ofn.arrayizeString<T>(str: string, strict?: boolean ) => strict === true ? T | null : T | string;
Ofn.arrayizeString('foo'); // -> 'foo'
Ofn.arrayizeString('foo', true); // -> null

Ofn.arrayizeString('{"foo":true}'); // -> [] with property foo = true
Ofn.arrayizeString({ foo: true }, true); // -> null

Ofn.arrayizeString('[ 1, 2, 3 ]');
// -> [ 1, 2, 3 ]
Ofn.arrayizeString('{ "0":1, "2":3, "foo":true }');
// -> [ 1, undefined, 3 ] with property foo = true

Ofn.arrayizeObject()

Ofn.arrayizeObject< T, O extends Record<string, any> = Record<string, any> >(object: O, strict?: boolean)
=> strict === true ? T | null : T | O;
Ofn.arrayizeObject('{"foo":true}', true); // -> null
Ofn.arrayizeObject({ foo: true }); // -> [] with property foo = true

Ofn.arrayizeObject([1, 2, 3]);
// -> [ 1, 2, 3 ]
Ofn.arrayizeObject({ 0: 1, 2: 3, foo: true });
// -> [ 1, undefined, 3 ] with property foo = true

Ofn.randomString()

Ofn.randomString(
  len: number // def: 8
) => string;
Ofn.randomString(); // -> 'lw5ucmgj'
Ofn.randomString(24); // -> 'a6ji656qovq078j1vpcmxzms'

Ofn.slugify()

Ofn.slugify( str: string ) => string;
Ofn.slugify('FOO Bar bazBaz'); // -> 'foo-bar-baz-baz'

Ofn.snakeify()

Ofn.snakeify( str: string ) => string;
Ofn.snakeify('FOO Bar bazBaz'); // -> 'foo_bar_baz_baz'

Ofn.splitStringNumber()

Ofn.splitStringNumber(
  str: string,
  sep?: string // def: '|'
) => string[];
Ofn.splitStringNumber('1.234.567', '.');
// -> [ '1', '234', '567' ]

Ofn.strPad()

Ofn.strPad = strPad(
  str: string | number,
  length: number,
  pad?: string | number, // def: ' '
  leftRight?: LeftRight  // def: 'left'
) => string;

type LeftRight = 'left' | 'right';
Ofn.strPad('123', 5); // -> '  123'
Ofn.strPad('123', 5, '0'); // -> '00123'
Ofn.strPad('123', 5, 'x', 'right'); // -> '123xx'

Ofn.substrCount()

Ofn.substrCount( str: string, substr: string ) => number;
Ofn.substrCount('This is an example', 'is'); // -> 2

Ofn.trimAll()

Ofn.trimAll( str: string ) => string;
Ofn.trimAll('  string    with    spaces   ');
// -> 'string with spaces'

Ofn.textTruncate()

Ofn.textTruncate = textTruncate(
  str: string,
  max?: number | true, // def: 120
  suffix?: string      // def: '...'
) => string;
Ofn.textTruncate('This is a example', 9, '... (read more)');
// -> 'This is a... (read more)'

Crypto

Ofn.md5()

Ofn.md5( str: string ) => string;
Ofn.md5('This is an example');
// -> 'f1bbf779adc2b5e8ada0fc5f6337d96d'

Ofn.strEncrypt()

Ofn.strEncrypt(
  str: string,
  key?: string,             // def: ''
  iv?: string,              // def: ''
  mode?: EncryptMode,       // def: 'CBC'
  padding?: EncryptPadding, // def: 'Pkcs7'
) => string;

type EncryptMode = 'CBC' | 'CTR' | 'CTRGladman' | 'OFB' | 'ECB';
type EncryptPadding = 'Pkcs7' | 'Iso97971' | 'AnsiX923' | 'Iso10126' | 'ZeroPadding' | 'NoPadding';
Ofn.strEncrypt('This is an example', 'foo');
// -> 'MHVuUmE5MHAvOXpOazkwckhyTGc5VUFUM0NXWkNPUFdzcU9wZE5ydlo4Zz0='

Ofn.strDecrypt()

Ofn.strDecrypt(
  str: string,
  key?: string,             // def: ''
  iv?: string,              // def: ''
  mode?: EncryptMode,       // def: 'CBC'
  padding?: EncryptPadding, // def: 'Pkcs7'
) => string;

type EncryptMode = 'CBC' | 'CTR' | 'CTRGladman' | 'OFB' | 'ECB';
type EncryptPadding = 'Pkcs7' | 'Iso97971' | 'AnsiX923' | 'Iso10126' | 'ZeroPadding' | 'NoPadding';
Ofn.strDecrypt('MHVuUmE5MHAvOXpOazkwckhyTGc5VUFUM0NXWkNPUFdzcU9wZE5ydlo4Zz0=', 'foo');
// -> 'This is an example'

Functions

await Ofn.sleep( ms )

Ofn.sleep(
  ms?: number // def: 0
) => Promise<void>;
await Ofn.sleep(5000); // -> 5 second

Ofn.getFunctionName()

Ofn.getFunctionName( fn?: string | Function ) => string;
function fnTest() {}

const callback = fnTest;

Ofn.getFunctionName(callback);
// -> 'fnTest'

Classes

Ofn.getClassName()

Ofn.getClassName( classy: any ) => string;
class MyClass {}

const ExampleClass = MyClass;

Ofn.getClassName(MyClass);
// -> 'MyClass'
Ofn.getClassName(ExampleClass);
// -> 'MyClass'

Ofn.getClassMethods()

Ofn.getClassName( classy: any ) => string[];
class MyClass {
  static varStatic1 = '';
  static varStatic2 = '';

  varStatic1 = '';
  varStatic2 = '';

  static fnStatic1() {}
  static fnStatic2() {}

  fn1() {}
  fn2() {}
}

Ofn.getClassMethods(MyClass);
// -> [ 'fn1', 'fn2' ]

Ofn.getClassStaticMethods()

Ofn.getClassStaticMethods( classy: any ) => string[];
class MyClass {
  static varStatic1 = '';
  static varStatic2 = '';

  varStatic1 = '';
  varStatic2 = '';

  static fnStatic1() {}
  static fnStatic2() {}

  fn1() {}
  fn2() {}
}

Ofn.getClassStaticMethods(MyClass);
// -> [ 'fnStatic1', 'fnStatic2' ]

Objects

Ofn.cloneObject()

Ofn.cloneObject<T>( obj: T ) => NonNullable<T>;
//deep clone

const obj1 = { foo: { bar: true } };
const obj2 = Ofn.cloneObject(obj1);

obj2.foo.bar = false;

// obj1 = { foo: { bar: true } }
// obj2 = { foo: { bar: false } }

Ofn.cloneObjectWithKeys()

Ofn.cloneObjectWithKeys<T, K extends keyof T = keyof T>(
  obj: T,
  keys: K[],
) => Partial<Pick<T, K>;
Ofn.cloneObjectWithKeys({ a: true, b: true }, ['a']);
// -> { a: true }
interface Custom {
  a: boolean;
  b?: boolean;
}

Ofn.cloneObjectWithKeys<Custom>({ a: true, b: true }, ['a']);
// -> { a: true }

Ofn.cloneObjectWithoutKeys()

Ofn.cloneObjectWithoutKeys<T, K extends keyof T>(
  obj: T,
  keys: K[],
) => Partial<Omit<T, K>>;
Ofn.cloneObjectWithoutKeys({ a: true, b: true }, ['a']);
// -> { b: true }
interface Custom {
  a: boolean;
  b?: boolean;
}

Ofn.cloneObjectWithoutKeys<Custom>({ a: true, b: true }, ['a']);
// -> { b: true }

Ofn.getObjectMissedKeys()

Ofn.getObjectMissedKeys<T extends Record<string, any>, K = string>(
  obj: T,
  keys: K[],
) => Omit<K[], keyof T>;
Ofn.getObjectMissedKeys({ a: true }, ['a']);
// -> []
Ofn.getObjectMissedKeys({ a: true }, ['a', 'b']);
// -> [ 'b' ]
interface Custom {
  a: boolean;
  b?: boolean;
}

Ofn.objHasKeys<Custom, keyof Custom>({ a: true }, ['a']);
// -> true
Ofn.objHasKeys<Custom, keyof Custom>({ a: true }, ['a', 'b']);
// -> false

Ofn.objGetMethods()

Ofn.objGetMethods<T>( obj: T, allowDefaults?: boolean ) => string[];
class MyClass {
  static varStatic1 = '';
  static varStatic2 = '';

  varStatic1 = '';
  varStatic2 = '';

  static fnStatic1() {}
  static fnStatic2() {}

  fn1() {}
  fn2() {}
}

const obj = new MyClass();

Ofn.objGetMethods(obj);
// -> [ 'fn1', 'fn2' ]

Ofn.objHasKeys()

Ofn.objHasKeys<T extends Record<string, any>, K = string>(
  obj: T,
  keys: K[]
) => boolean;
Ofn.objHasKeys({ a: true }, ['a']);
// -> true
Ofn.objHasKeys({ a: true }, ['a', 'b']);
// -> false
interface Custom {
  a: boolean;
  b?: boolean;
}

Ofn.objHasKeys<Custom, keyof Custom>({ a: true }, ['a']);
// -> true
Ofn.objHasKeys<Custom, keyof Custom>({ a: true }, ['a', 'b']);
// -> false

Ofn.mergeObjectsDeep()

Ofn.mergeObjectsDeep<T extends Record<string, any>>(
  ...args: Array<Partial<T>>
) => T;
Ofn.mergeObjectsDeep({ a: { b: '1', c: '2' } }, { a: { b: '7', d: '3' } });
// -> { a: { b: '7', c: '2', d: '3' }

Ofn.objIsNotEmpty()

Ofn.objIsNotEmpty( obj: any ) => boolean;
// => obj is NonNullable<Record<string | number, any>>;
Ofn.objIsNotEmpty({}); // -> false
Ofn.objIsNotEmpty({ foo: 0 }); // -> true

Ofn.objIsEmpty()

Ofn.objIsEmpty( obj: any ) => boolean;
Ofn.objIsEmpty({}); // -> true
Ofn.objIsEmpty({ foo: 0 }); // -> false

Ofn.objToStringAttr()

Ofn.objToStringAttr<T>( obj: T ) => string;
Ofn.objToStringAttr({ id: 20, name: 'foo' });
// -> ' id="20" name="foo" '

Ofn.objToStringAttrData()

Ofn.objToStringAttrData<T>( obj: T ) => string;
Ofn.objToStringAttrData({ id: 20, name: 'foo' });
// -> ' data-id="20" data-name="foo" '

Ofn.objToStringAttrJs()

Ofn.objToStringAttrJs<T>( obj: T ) => string;
Ofn.objToStringAttrJs({ id: 20, name: 'foo' });
// -> ' id:20;name:foo '

Ofn.objToStringSqlSet()

Ofn.objToStringSqlSet<T>( obj: T ) => string;
const obj = { size: 20, name: "'oro'", last_update: 'NOW()' };
const str = Ofn.objToStringSqlSet( obj );
// -> " size = 20, name = 'foo', last_update = NOW() "

const statement = `UPDATE my_table SET ${str} WHERE id = 7`;
// OR
const statement =
    `INSERT INTO my_table ( ${Object.keys( obj ).join( ', ' )} ) \
     VALUES ( ${Ofn.objToStringSqlSet( obj )} )`;

Arrays

Ofn.cloneArray()

Ofn.cloneArray<T>( arr: T ) => T;
//deep clone

const arr1 = [{ foo: true }, { bar: false }];
const arr2 = Ofn.cloneArray(arr1);

arr2[1].bar = true;

// arr1 = [ { foo: true }, { bar: false } ]
// arr2 = [ { foo: true }, { bar: true } ]

Ofn.arrayCountByKey()

Ofn.arrayCountByKey<T>(
  array: T[],
  key: string,
  strict?: boolean // def: false,
) => Record<string, number>;
Ofn.arrayCountByKey(
  [
    { id: 'alpha', category: 'male' },
    { id: 'bravo', category: 'female' },
  ],
  'category',
);
// -> { 'male': 1, 'female': 1 }

Ofn.arrayGetUnique()

Ofn.arrayGetUnique<T>( array: T[] ) => T[];
Ofn.arrayGetUnique([1, 1, 2, 1, 3]);
// -> [ 1, 2, 3 ]

Ofn.arrayGroupByKey()

Ofn.arrayGroupByKey<T>(
  array: T[],
  key: string,
  strict?: boolean, // def: false
) => Record<string, T[]>;
Ofn.arrayGroupByKey(
  [
    { id: 'alpha', category: 'male' },
    { id: 'bravo', category: 'female' },
  ],
  'category',
);
// -> {
//      'male': [ { id: 'alpha', category: 'male' } ],
//      'female': [ { id: 'bravo', category: 'female' } ]
//    }

Ofn.arraySortByKey()

Ofn.arraySortByKey<T>(
  arr: T[],
  key: string,
  sortArray?: string[]
) => T[];
Ofn.arraySortByKey(
  [
    { id: 'alpha', category: 'male' },
    { id: 'bravo', category: 'female' },
  ],
  'category',
);
// -> [
//      { id: 'bravo', category: 'female' },
//      { id: 'alpha', category: 'male' }
//    ]

Ofn.arraySortByKey(
  [
    { id: 'alpha', category: 'male' },
    { id: 'bravo', category: 'female' },
  ],
  'category',
  ['male', 'female'],
);
// -> [
//      { id: 'alpha', category: 'male' },
//      { id: 'bravo', category: 'female' }
//    ]

Ofn.arrayToObjectByKey()

Ofn.arrayToObjectByKey<T>(
  arr: T[],
  key: string,
  strict?: boolean // def: false
) => Record<string, T>;
Ofn.arrayToObjectByKey(
  [
    { id: 'alpha', name: 'Alpha' },
    { id: 'bravo', name: 'Bravo' },
  ],
  'id',
);
// -> {
//      'alpha': { id: 'alpha', name: 'Alpha' },
//      'bravo': { id: 'bravo', name: 'Bravo'
//    }

Ofn.arrayValuesByKey()

Ofn.arrayValuesByKey<T, V>(array: T[], key: strict?: boolean ) => strict === true ? Array<V> : Array<V | undefined>;
Ofn.arrayValuesByKey(
  [
    { id: 'alpha', name: 'Alpha' },
    { id: 'bravo', name: 'Bravo' },
  ],
  'id',
);
// -> [ 'alpha', 'bravo' ]

Ofn.arraysDifference()

Ofn.arraysDifference<T>( ...args: T[] ) => T[];
Ofn.arraysDifference([1, 2, 3], [2, 3, 4]);
// -> [ 1 ]

Ofn.arraysDifferenceAll()

Ofn.arraysDifferenceAll<T>( ...args: T[] ) => T[];
Ofn.arraysDifferenceAll([1, 2, 3], [2, 3, 4]);
// -> [ 1, 4 ]

Ofn.arraysIntersection()

Ofn.arraysIntersection<T>( ...args: T[] ) => T[];
Ofn.arraysIntersection([1, 2, 3], [2, 3, 4]);
// -> [ 2, 3 ]

Dates

To use date, better use dateObj.

Note: When you load new Date(), the output string date is shown with the timezone UTC.

In dateObj this output string date is saved as iso, and the rest of params uses the local date.

By default, the param sep is /, but it could be changed with opts = { sep: '.' }

const date = new Date('2021-08-13T09:57:34.000Z');

const dateObj = {
  iso: '2021-08-13T09:57:34.000Z',
  // 'local' depends of where is the device globally located
  local: '2021-08-13T10:57:34.000Z',
  sep: '/',
  year: '2021',
  month: '08',
  day: '13',
  hour: '10',
  minute: '57',
  second: '34',
  time: '10:57',
  times: '10:57:34',
  date: '13/08/2021',
  datetime: '13/08/2021 10:57',
  datetimes: '13/08/2021 10:57:34',
  sqldate: '2021-08-13',
  sqldatetime: '2021-08-13 10:57:34',
  fulldate: '13/08/2021 10:57:34',
  fulldateStart: '13/08/2021 00:00:00',
  fulldateEnd: '13/08/2021 23:59:59',
  weekday: 5,
  monthdays: 31,
  timestamp: 1628848654000,
  jsdate: new Date(date),
};
interface DateObj {
  iso: string;
  sep: string;
  local: string;
  year: string;
  month: string;
  day: string;
  hour: string;
  minute: string;
  second: string;
  time: string;
  times: string;
  date: string;
  datetime: string;
  datetimes: string;
  sqldate: string;
  sqldatetime: string;
  sqldatetimeIso: string;
  fulldate: string;
  fulldateStart: string;
  fulldateEnd: string;
  weekday: number;
  monthdays: number;
  timestamp: number;
  jsdate: Date;
}

Ofn.dateObjByDate()

Ofn.dateObjByDate(
  date: Date,
  options: DateObjOptions | string = {}
) => DateObj;

interface DateObjOptions {
  sep?: string;
}
Ofn.dateObjByDate(new Date('2021-08-13 10:57:34'));
// -> local: '2021-08-13T10:57:34.000Z'

Ofn.dateObjByHtml()

Ofn.dateObjByHtml(
  html: string,
  options: DateObjOptions | string = {}
) => DateObj | null;

interface DateObjOptions {
  sep?: string;
}
Ofn.dateObjByHtml('13/08/2021 10:57:34');
// -> local: '2021-08-13T10:57:34.000Z'

Ofn.dateObjBySql()

Ofn.dateObjBySql(
  sqldate: string,
  options: DateObjOptions | string = {}
) => DateObj | null;

interface DateObjOptions {
  sep?: string;
}
Ofn.dateObjBySql('2021-08-13 10:57:34');
// -> local: '2021-08-13T10:57:34.000Z'

Ofn.dateObjByTimestamp()

Ofn.dateObjByTimestamp(
  timestamp: number,
  options: DateObjOptions | string = {}
) => DateObj | null;

interface DateObjOptions {
  sep?: string;
}
Ofn.dateObjByTimestamp(1628848654000);
// -> iso: '2021-08-13T09:57:34.000Z'

Ofn.dateObjByToday()

Ofn.dateObjByToday( options: DateObjOptions | string = {} ) => DateObj;

interface DateObjOptions {
  sep?: string;
}
Ofn.dateObjByToday();

Ofn.dateObjPlusDays()

Ofn.dateObjPlusDays(
  date: DateObj | Date,
  days: number
) => DateObj;
const date = new Date('2021-08-13 10:57:34');
const dateObj = Ofn.dateObjByDate(date);

Ofn.dateObjPlusDays(date, 1);
// -> local: '2021-08-14T10:57:34.000Z'

Ofn.dateObjPlusDays(dateObj, -1);
// -> local: '2021-08-12T10:57:34.000Z'

Ofn.isDateObj()

Ofn.isDateObj( date: any ) => boolean;
// => date is DateObj

// depricated
Ofn.dateIsObj( date: any ) => boolean;
const date = new Date('2021-08-13 10:57:34');
const dateObj = Ofn.dateObjByDate(date);

Ofn.isDateObj(date);
// -> false
Ofn.isDateObj(dateObj);
// -> true

Ofn.datesCompare()

Ofn.datesCompare(
  date1: DateObj | Date,
  date2: DateObj | Date
) => -1 | 0 | 1;
const date = new Date('2021-08-13 10:57:34');
const todayObj = Ofn.dateObjByToday();

Ofn.datesCompare(date, todayObj);
//  -> 1
Ofn.datesCompare(todayObj, date);
// -> -1
Ofn.datesCompare(todayObj, todayObj);
//  -> 0

Ofn.dateIsBetween()

Ofn.dateIsBetween(
  date: DateObj | Date,
  dateMin: DateObj | Date,
  dateMax: DateObj | Date
) => boolean;
const date = new Date('2021-08-13 10:57:34');
const todayObj = Ofn.dateObjByToday();

Ofn.dateIsBetween(Ofn.dateObjPlusDays(date, 1), date, todayObj);
// -> true

Ofn.datesDiffDays()

Ofn.datesDiffDays(
  date1: DateObj | Date,
  date2: DateObj | Date
) => number;
const dateObj1 = Ofn.dateObjBySql('2021-12-25');
const dateObj2 = Ofn.dateObjBySql('2022-01-05');

Ofn.datesDiffDays(dateObj1, dateObj2);
// -> 11

Ofn.datesDiffMonths()

Ofn.datesDiffMonths(
  date1: DateObj | Date,
  date2: DateObj | Date
) => number;
const dateObj1 = Ofn.dateObjBySql('2021-12-25');
const dateObj2 = Ofn.dateObjBySql('2022-01-05');

Ofn.datesDiffMonths(dateObj1, dateObj2);
// -> 1

Ofn.datesDiffMonthsArray()

Ofn.datesDiffMonthsArray(
  date1: DateObj | Date,
  date2: DateObj | Date
) => DateDiffMonth[];

interface DateDiffMonth {
  year: string;
  month: string;
}
const dateObj1 = Ofn.dateObjBySql('2021-12-25');
const dateObj2 = Ofn.dateObjBySql('2022-01-05');

Ofn.datesDiffMonths(dateObj1, dateObj2);
// -> [ { year: '2021', month: '12' }, { year: '2022', month: '01' } ]

Ofn.dateCheckString()

Ofn.dateCheckString( str: string, format?: DateStringFormat ) => boolean;

type DateStringFormat =
  | 'datetime_strict'
  | 'datetime_default_strict'
  | 'datetime_html_strict'
  | 'datetime_sql_strict'
  | 'datetime'
  | 'datetime_default'
  | 'datetime_html'
  | 'datetime_sql'
  | 'html'
  | 'date_html'
  | 'sql'
  | 'date_sql'
  | 'default'
  | 'date_default';

To check the format, it's used the regexps from (oro-regexp)[https://github.com/oropesa/oro-regexp]

Ofn.dateCheckString('2022-01-05', 'date_sql');
// -> true

Allowed formats:

Format name Example REGEXP
datetime_strict 30-12-2020 11:59[:59] REGEXP.DATETIME_DEFAULT_STRICT
datetime_default_strict 30-12-2020 11:59[:59] REGEXP.DATETIME_DEFAULT_STRICT
datetime_html_strict 30/12/2020 11:59[:59] REGEXP.DATETIME_HTML_STRICT
datetime_sql_strict 2020-12-30 11:59[:59] REGEXP.DATETIME_SQL_STRICT
datetime 30-12-2020 [11:59[:59]] REGEXP.DATETIME_DEFAULT
datetime_default 30-12-2020 [11:59[:59]] REGEXP.DATETIME_DEFAULT
datetime_html 30/12/2020 [11:59[:59]] REGEXP.DATETIME_HTML
datetime_sql 2020-12-30 [11:59[:59]] REGEXP.DATETIME_SQL
html 30/12/2020 REGEXP.DATE_HTML
date_html 30/12/2020 REGEXP.DATE_HTML
sql 2020-12-30 REGEXP.DATE_SQL
date_sql 2020-12-30 REGEXP.DATE_SQL
default 30-12-2020 REGEXP.DATE_DEFAULT
date_default 30-12-2020 REGEXP.DATE_DEFAULT

URLs

Ofn.urlDecode()

Ofn.urlDecode( url: string ) => string;
Ofn.urlDecode('https%3A%2F%2Fexample.com%3Fname%3Dfoo%20bar%26plus%3D1%2B2');
// -> 'https://example.com?name=foo bar&plus=1+2'

Ofn.urlEncode()

Ofn.urlEncode( url: string ) => string;
Ofn.urlEncode('https://example.com?name=foo bar&plus=1+2');
// -> 'https%3A%2F%2Fexample.com%3Fname%3Dfoo%20bar%26plus%3D1%2B2'

Ofn.urlGetBase()

Ofn.urlGetBase( url: string ) => string;
Ofn.urlGetBase('https://example.com/page?param1=value1&param2=value2');
// -> 'https://example.com'

Ofn.urlGetCurrentByReq()

Ofn.urlGetCurrentByReq(
  req: Pick<Request, 'originalUrl' | 'protocol'>, // type { Request } from 'express'
  isFullpath?: boolean
) => string;
// simulating `Request` of express
const req = {
  protocol: 'https',
  originalUrl: '/',
  get: (key) => {
    const obj = { host: 'example.com' };
    return obj[key];
  },
};

Ofn.urlGetCurrentByReq(req);
// -> 'https://example.com/'
Ofn.urlGetCurrentByReq(req, false);
// -> '/'

Ofn.urlGetHostByReq()

Ofn.urlGetHostByReq(
  req: Pick<Request, 'originalUrl' | 'protocol' | 'get'> // type { Request } from 'express'
) => string;
// simulating `Request` of express
const req = {
  protocol: 'https',
  originalUrl: '/',
  get: (key) => {
    const obj = { host: 'example.com' };
    return obj[key];
  },
};

Ofn.urlGetHostByReq(req);
// -> 'https://example.com'

Ofn.urlGetParams()

Ofn.urlGetParams( urlOrQuery: string ) => Record<string, string | string[]>;
Ofn.urlGetParams('https://example.com/page?param1=value1&param2=value2');
// -> { param1: 'value1', param2: 'value2' }

Ofn.urlIsValid()

Ofn.urlIsValid( url: string ) => boolean;
Ofn.urlIsValid('example.com');
// -> false
Ofn.urlIsValid('https://example.com?foo&bar=1');
// -> true

Ofn.urlObjByUrl()

Ofn.urlObjByUrl( url: string ) => URL | undefined;
Ofn.urlObjByUrl('example.com');
// -> undefined
Ofn.urlObjByUrl('https://example.com?foo&bar=1');
// -> new URL( url )

Ofn.urlPlainToString()

Ofn.urlPlainToString( url: string ) => string;
Ofn.urlPlainToString('https://example.com?name%3dfoo%20bar&plus%3d1+2');
// -> 'https://example.com?name=foo bar&plus=1+2'

Ofn.urlStringToPlain()

Ofn.urlStringToPlain( url: string ) => string;
Ofn.urlStringToPlain('https://example.com?name=foo bar&plus=1+2');
// -> 'https://example.com?name%3dfoo%20bar&plus%3d1+2'

Files

Ofn.getFilenameByPath()

Ofn.getFilenameByPath( path: string ) => string;
Ofn.getFilenameByPath('/var/www/htdoc/filename.pdf');
// -> 'filename.pdf'

Ofn.getFilenameExtByName()

Ofn.getFilenameExtByName( filenameOrPath: string ) => string;
Ofn.getFilenameExtByName('filename.pdf');
// -> 'pdf'

Ofn.getFilenameWOutExtByName()

Ofn.getFilenameWOutExtByName( filenameOrPath: string ) => string;
Ofn.getFilenameWOutExtByName('filename.pdf');
// -> 'filename'

Ofn.getFolderByPath()

Ofn.getFolderByPath(
  path: string,
  deep?: number // def: 1
) => string;
Ofn.getFolderByPath('/var/www/htdoc/filename.pdf');
// -> '/var/www/htdoc'
Ofn.getFolderByPath('/var/www/htdoc/filename.pdf', 3);
// -> '/var'

Ofn.sanitizeFilename()

Ofn.sanitizeFilename( path: string ) => string;
Ofn.sanitizeFilename('/var/tmp/ÁËÌÒÑ.pdf');
// -> 'AEION.pdf'

Ofn.sanitizePath()

Ofn.sanitizePath( path: string ) => string;
Ofn.sanitizePath('/var/tmp/foo.pdf');
// -> '/var/tmp/foo.pdf'
Ofn.sanitizePath('C:\\tmp\\foo.pdf');
// -> 'C:/tmp/foo.pdf'

Ofn.slugifyFilename()

Ofn.slugifyFilename( path: string ) => string;
Ofn.slugifyFilename('/var/www/htdoc/Foo Bar.pdf');
// -> 'foo-bar.pdf'

PHP Serialize

To save an object in db, it's better as json than php-serialize.

So, by default, this functions work as json and work well when in current dbs where already exist fields with php-serialize.

Ofn.phpIsSerialized()

Ofn.phpSerialize( str: string, strict?: string ) => boolean;
Ofn.phpIsSerialized('{"foo":true}');
// -> false
Ofn.phpIsSerialized('a:1:{s:6:"foo";b:1;}');
// -> true

Ofn.phpSerialize()

Ofn.phpSerialize<T>(
  mixedValue: T,
  strict?: boolean // def: false
) => string | T;
Ofn.phpSerialize({ foo: true });
// -> '{"foo":true}'
Ofn.phpSerialize({ foo: true }, true);
// -> 'a:1:{s:6:"foo";b:1;}'

Ofn.phpUnserialize()

Ofn.phpUnserialize<T>(
  str: string,
  strict?: boolean // def: false
) => T | string;
Ofn.phpUnserialize('{"foo":true}');
// -> { foo: true }
Ofn.phpUnserialize('a:1:{s:6:"foo";b:1;}');
// -> { foo: true }
Ofn.phpUnserialize('{"foo":true}', true);
// -> '{"foo":true}'

Response

export type SResponse<
  OK extends Record<string, any> = {},
  KO extends Record<string, any> = {},
  E extends boolean = false,
> = E extends true ? SResponseOK<OK> | SResponseError<KO> : SResponseOK<OK> | SResponseKO<KO>;

type SResponseOK<T extends Record<string, any> = {}> = T & {
  status: true;
  msg?: string;
};

type SResponseError<T extends Record<string, any> = {}> = T & { msg?: string };

type SResponseKO<T extends Record<string, any> = {}> = {
  status: false;
  error?: SResponseError;
  tryAgain?: boolean;
};

type SResponseError<T extends Record<string, any> = {}> = Error & {
  responseError: SResponseKO<T>;
};

Ofn.setResponseOK()

Ofn.setResponseOK<T extends Record<string, any>>(
  msgOrData?: string | T,
  data?: T
) => SResponseOK<T>;
Ofn.setResponseOK();
// -> { status: true }

Ofn.setResponseOK('Reason');
// -> { status: true, msg: 'Reason' }

Ofn.setResponseOK({ label: 'foo' });
// -> { status: true, label: 'foo' }

Ofn.setResponseOK('Reason', { label: 'foo' });
// -> { status: true, msg: 'Reason', label: 'foo' }

Ofn.setResponseKO('Reason:', { msg: 'custom', label: 'foo' });
// -> { status: true, msg: 'Reason: custom', label: 'foo' }

Ofn.setResponseKO({ msg: 'Reason:' }, { msg: 'custom', label: 'foo' });
// -> { status: true, msg: 'Reason: custom', label: 'foo' }

Ofn.setResponseKO({ msg: 'Reason:', name: 'Example', label: 'bar' }, { msg: 'custom', label: 'foo' });
// -> { status: true, msg: 'Reason: custom', name: Example, label: 'foo' }

Allowed returned type:

SResponseOKBasic`{ status: true  }`;
SResponseOKSimple`{ status: true, msg: string }`;
SResponseOKObject<T>`{ status: true } & T`;
SResponseOKObjectSimple<T>`{ status: true, msg: string} & T`;

Ofn.setResponseKO()

Ofn.setResponseKO<T extends Record<string, any>, E extends boolean = false>(
  msgOrError?: string | T,
  error?: T,
  tryAgain?: boolean, // def: false
  asError?: E // def: false
) => E extends true ? SResponseError<T> : SResponseKO<T>;
Ofn.setResponseKO();
// -> { status: false }

Ofn.setResponseKO('Error Reason');
// -> { status: false, error: { msg: 'Error Reason' } }

Ofn.setResponseKO({ label: 'foo' });
// -> { status: false, error: { label: 'foo' } }

Ofn.setResponseKO('Error Reason', { label: 'foo' });
// -> { status: false, error: { msg: 'Error Reason', label: 'foo' } }

Ofn.setResponseKO('Error Reason:', { msg: 'bad param', label: 'foo' });
// -> { status: false, error: { msg: 'Error Reason: bad param', label: 'foo' } }

Ofn.setResponseKO('Error Reason', { label: 'foo' }, true);
// -> { status: false, tryAgain: true, error: { msg: 'Error Reason', label: 'foo' } }

Ofn.setResponseKO('Error Reason', { label: 'foo' }, null, true);
// -> ( new Error() )
//      .name = 'responseError'
//      .message = 'Error Reason'
//      .responseError = { status: false, error: { msg: 'Error Reason', label: 'foo' } }

Ofn.setResponseKO('Error Reason', { label: 'foo', errorName: 'customError' }, null, true);
// -> ( new Error() )
//      .name = 'customError'
//      .message = 'Error Reason'
//      .responseError = { status: false, error: { msg: 'Error Reason', label: 'foo', errorName: 'customError' } }

Allowed returned type:

SResponseKOBasic`{ status: false }`;
SResponseKOBasicAgain`{ status: false, tryAgain: boolean }`;
SResponseKOSimple`{ status: false, error: { msg: string } }`;
SResponseKOSimpleAgain`{ status: false, error: { msg: string }, tryAgain: boolean }`;
SResponseKOObject`{ status: false, error: T }`;
SResponseKOObjectAgain`{ status: false, error: T, tryAgain: boolean }`;
SResponseKOObjectSimple`{ status: false, error: T & { msg: string } }`;
SResponseKOObjectSimpleAgain`{ status: false, error: T & { msg: string }, tryAgain: boolean }`;

SResponseErrorBasic`Error & { responseError: SResponseKOBasic }`;
SResponseErrorBasicAgain`Error & { responseError: SResponseKOBasicAgain }`;
SResponseErrorSimple`Error & { responseError: SResponseKOSimple }`;
SResponseErrorSimpleAgain`Error & { responseError: SResponseKOSimpleAgain }`;
SResponseErrorObject`Error & { responseError: SResponseKOObject }`;
SResponseErrorObjectAgain`Error & { responseError: SResponseKOObjectAgain }`;
SResponseErrorObjectSimple`Error & { responseError: SResponseKOObjectSimple }`;
SResponseErrorObjectSimpleAgain`Error & { responseError: SResponseKOObjectSimpleAgain }`;

Package Sidebar

Install

npm i oro-functions-client

Weekly Downloads

2

Version

2.3.3

License

MIT

Unpacked Size

239 kB

Total Files

8

Last publish

Collaborators

  • oropesa