UNPKG

tiny-essentials

Version:

Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.

111 lines 4.56 kB
/** @typedef {Object.<string, (val: any) => *>} ExtendObjType */ /** @typedef {Array<[string, (val: any) => *]>} ExtendObjTypeArray */ /** * Adds new type checkers to the typeValidator without overwriting existing ones. * * Accepts either an object with named functions or an array of [key, fn] arrays. * If no index is provided, the type is inserted just before 'object' (if it exists), or at the end. * * @param {ExtendObjType|ExtendObjTypeArray} newItems * - New type validators to be added. * @param {number} [index] - Optional. Position at which to insert each new type. Ignored if the type already exists. * @returns {string[]} - A list of successfully added type names. * * @example * extendObjType({ * htmlElement2: val => typeof HTMLElement !== 'undefined' && val instanceof HTMLElement * }); * * @example * extendObjType([ * ['alpha', val => typeof val === 'string'], * ['beta', val => Array.isArray(val)] * ]); */ export function extendObjType(newItems: ExtendObjType | ExtendObjTypeArray, index?: number): string[]; /** * Reorders the typeValidator.order array according to a custom new order. * All values in the new order must already exist in the current order. * The function does not mutate the original array structure directly. * * @param {string[]} newOrder - The new order of type names. * @returns {boolean} - Returns true if the reorder was successful, false if invalid keys were found. * * @example * reorderObjTypeOrder([ * 'string', 'number', 'array', 'object' * ]); */ export function reorderObjTypeOrder(newOrder: string[]): boolean; /** * Returns a cloned version of the `typeValidator.order` array. * The cloned array will not be affected by future changes to the original `order`. * * @returns {string[]} - A new array with the same values as `typeValidator.order`. */ export function cloneObjTypeOrder(): string[]; /** * Checks the type of a given object or returns its type as a string. * * @param {*} obj - The object to check or identify. * @param {string} [type] - Optional. If provided, checks whether the object matches this type (e.g., "object", "array", "string"). * @returns {boolean|string|null} - Returns `true` if the type matches, `false` if not, * the type string if no type is provided, or `null` if the object is `undefined`. * * @example * objType([], 'array'); // true * objType({}, 'object'); // true * objType('hello'); // "string" * objType(undefined); // null */ export function objType(obj: any, type?: string): boolean | string | null; /** * Checks the type of a given object and returns the validation value if a known type is detected. * * @param {*} obj - The object to check or identify. * @returns {{ valid:*; type: string | null }} - Returns the type result. */ export function checkObj(obj: any): { valid: any; type: string | null; }; /** * Creates a clone of the functions from the `typeValidator` object. * It returns a new object where the keys are the same and the values are the cloned functions. */ export function getCheckObj(): { [k: string]: any; }; /** * Counts the number of elements in an array or the number of properties in an object. * * @param {Array<*>|Record<string|number, any>} obj - The array or object to count. * @returns {number} - The count of items (array elements or object keys), or `0` if the input is neither an array nor an object. * * @example * countObj([1, 2, 3]); // 3 * countObj({ a: 1, b: 2 }); // 2 * countObj('not an object'); // 0 */ export function countObj(obj: Array<any> | Record<string | number, any>): number; /** * Determines whether a given value is a pure JSON object (plain object). * * A pure object satisfies the following: * - It is not null. * - Its type is "object". * - Its internal [[Class]] is "[object Object]". * - It is not an instance of built-in types like Array, Date, Map, Set, etc. * * This function is useful for strict data validation when you want to ensure * a value is a clean JSON-compatible object, free of class instances or special types. * * @param {unknown} value - The value to test. * @returns {value is Record<string | number | symbol, unknown>} Returns true if the value is a pure object. */ export function isJsonObject(value: unknown): value is Record<string | number | symbol, unknown>; export type ExtendObjType = { [x: string]: (val: any) => any; }; export type ExtendObjTypeArray = Array<[string, (val: any) => any]>; //# sourceMappingURL=objFilter.d.mts.map