UNPKG

@versatiledatakit/shared

Version:

Versatile Data Kit Shared library enables reusability of shared features like: NgRx Redux, Error Handlers, Utils, Generic Components, etc.

228 lines (227 loc) • 8.93 kB
import { Replacer } from '../../common'; import { Collections, IteratorFnResult, LiteralObjectOrNull, Nil, ObjectIterator, Primitives, PrimitivesDate, Subtract } from '../model'; /** * ** Utility Class for Collections. */ export declare class CollectionsUtil { /** * ** Check if value is of type undefined. */ static isUndefined(value: any): value is undefined; /** * ** Check if value has value null. */ static isNull(value: any): value is null; /** * ** Check if value is undefined or null. */ static isNil(value: any): value is Nil; /** * ** Check if value is defined (opposite of isNil). * * - Not null. * - Not undefined. */ static isDefined<T>(value: T): boolean; /** * ** Check if value T is of type Number. */ static isNumber<T extends number>(num: T | any): num is number; /** * ** Check if value T is of type String. */ static isString<T extends string>(str: T | any): str is string; /** * ** Check if value T is of type Boolean. */ static isBoolean<T extends boolean>(bool: T | any): bool is boolean; /** * ** Check if value is Primitive. * * - String, Number, Boolean, null or undefined. */ static isPrimitive(value: any): value is Primitives | Nil; /** * ** Check if value is NaN. */ static isNaN<T extends number>(value: T | any): boolean; /** * ** Check if value is of type Date. */ static isDate(value: any): value is Date; /** * ** Check if value is Primitive or Date. * * - String, Number, Boolean, null, undefined or Date. */ static isPrimitiveOrDate<T extends PrimitivesDate>(value: T | any): value is Primitives | Nil | Date; /** * ** Check if value T is a reference that points to function (Class/Method). */ static isFunction<T extends (...args: any[]) => any>(value: T | any): value is (...args: any[]) => any; /** * ** Check if value T is of type Object. */ static isObject<T extends Record<string, unknown>>(obj: T | any): obj is Record<string, unknown>; /** * ** Check if value T is instance of Array. */ static isArray<T>(arr: T | any): arr is T extends any[] ? T : any[]; /** * ** Check if value T is not instance of Array or there are no elements in Array. */ static isArrayEmpty<T>(arr: T | any): boolean; /** * ** Returns new Array where items will be filtered by reference and will leave distinct values. */ static uniqueArray<T extends any[]>(arr: T): T; /** * ** Check if value is Map. */ static isMap(obj: Map<any, any> | any): obj is Map<any, any>; /** * ** Check if value is WeakMap. */ static isWeakMap(obj: WeakMap<any, any> | any): obj is WeakMap<any, any>; /** * ** Check if value is Set. */ static isSet(obj: Set<any> | any): obj is Set<any>; /** * ** Check if value T is of type String and has length bigger than 0 after whitespace trim. */ static isStringWithContent<T extends string>(str: T | any): str is string; /** * ** Check if value is Collection (literal Object, Array, Map, WeakMap or Set). */ static isCollection(obj: Collections | any): obj is Collections; /** * ** Check if value is of type Object and not null. */ static isObjectNotNull<T extends Record<string, unknown>>(obj: T | any): obj is Record<string, unknown> & Subtract<Record<string, unknown>, null>; /** * ** Check if some variable is of type Boolean and is true. */ static isBooleanAndTrue(bool: boolean | any): boolean; /** * ** Check if value is literal Object or null. * * - Not an Array, Map, WeakMap or Set. */ static isLiteralObjectOrNull<T extends Record<string, unknown>>(obj: T | any): obj is LiteralObjectOrNull; /** * ** Check if provided value is literal Object. * * - Not and Array, Map, WeakMap, Set or null. */ static isLiteralObject<T extends Record<string, unknown>>(obj: T | any): boolean; /** * ** Check if value is Object and has properties. */ static isObjectWithProperties<T extends Record<string, unknown>>(obj: T | any): boolean; /** * ** Return current Date in ISO string format. */ static dateISO(): string; /** * ** Return current Date milliseconds from 1970. */ static dateNow(): number; /** * ** Performs deep comparison between two values to determine if the are equivalent. */ static isEqual(value1: any, value2: any): boolean; /** * ** Create recursive deep cloned value from provided one. */ static cloneDeep<T>(value: T): T; /** * ** Generate UUID that meats RFC4122 compliance. */ static generateUUID(): string; /** * ** Generate Object UUID that meats RFC4122 compliance and also has Class name identifier inside. * * <br/> * <i>pattern</i>: * <p> * <Class Name><strong>_</strong><UUID RFC4122> * </p> */ static generateObjectUUID(className: string): string; /** * ** Creates random string. */ static generateRandomString(): string; /** * ** Iterates own enumerable properties in Object. * * - use Object.keys method for extraction, and executes provided iteratorFn. * - if iteratorFn returns false or -1 it will break iteration. * - all other return values continue until last property. * * - flag as third parameter is optional: * - Without flag or with flag and has value 'plainObject, method will iterate only through literal Objects. * - With flag and has value 'objectLookLike', method will try to iterate through everything * that passes type value === 'object' (literal Object, Array, Map, Set, WeakMap, etc..). */ static iterateObject<T extends Record<string, unknown>>(obj: T, iteratorFn: ObjectIterator<T, IteratorFnResult>, flag?: 'plainObject' | 'objectLookLike'): T | null; /** * ** Check if value is Literal Object and has properties. */ static isLiteralObjectWithProperties<T extends Record<string, unknown>>(obj: T | unknown): boolean; /** * ** Iterates over object properties and return Array of its values. */ static objectValues<T extends Record<string, any>>(obj: T | null | undefined): Array<T[keyof T]>; /** * ** Transform given Map to Object. */ static transformMapToObject<T extends Map<string, unknown>>(map: T): { [key: string]: unknown; }; /** * ** Transform given Object to Map. */ static transformObjectToMap<T extends { [key: string]: any; }>(obj: T): Map<string, any>; /** * ** Iterates over object properties and return Array of its keys/values in pairs. * <p> * - Returns Array of subArrays that have 2 elements each, first element key and second element value. */ static objectPairs<T extends Record<keyof T, T[keyof T]>>(obj: T | null | undefined): Array<[keyof T, T[keyof T]]>; /** * ** Return own property Descriptor from provided object/function. */ static getObjectPropertyDescriptor<T extends Record<string, any>>(obj: T, key: string): PropertyDescriptor; /** * ** Iterates own enumerable properties (statics) of Function (Class). * * - use Object.getOwnPropertyDescriptors method for extraction, and executes provided iteratorFn. * - if iteratorFn returns false or -1 will break iteration. * - all other return values means continue until last property. */ static iterateClassStatics<T extends Record<string, any>>(fn: T, iteratorFn: (descriptor: PropertyDescriptor, key: string, fn: T) => IteratorFnResult): T & ((...args: any[]) => any); /** * ** Check if two Maps are Equal. * * - They are equal if they have same references. * - They are equal if they have same keys and same values for compared keys. */ static areMapsEqual(m1: Map<unknown, unknown>, m2: Map<unknown, unknown>): boolean; /** * ** Interpolate string and replace while iterating through provided strings. * * - Replacers are strings that are replaced on every place where %s is found starting from index 0. */ static interpolateString(target: string, ...replacers: string[]): string; /** * ** Interpolate text and replace while iterating through provided replacers. * * - Replacers are objects ofType {@link Replacer} that are iterates and consumes, * searchValue is matcher and replaceValue is value that is placed on match. */ static interpolateString(target: string, ...replacers: Array<Replacer<string>>): string; }