UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.

148 lines 6.45 kB
import type { GenericObject } from '../object/types'; import type { ClassDetails, Constructor, DelayedFn, Primitive, ThrottledFn, VoidFunction } from '../types/index'; import type { ArrayOfObjectsToStringOptions, ArrayOfPrimitivesToStringOptions } from './types'; /** * * Deeply compare two values (arrays, objects, or primitive values). * * @param a First value to compare. * @param b Second value to compare. * @returns Whether the values are deeply equal. */ export declare const isDeepEqual: <T>(a: T, b: T) => boolean; /** * * Converts an array of objects to a string using a specific property (supports nested path to primitive values) and separator. * * @example * const users = [ * { id: 1, name: { first: 'Alice' }, city: 'Bangu', }, * { id: 4, name: { first: 'Bob' }, city: 'Banguland', }, * ]; * convertArrayToString(users, { target: 'name.first', separator: ' | ' }); * // "Alice | Bob" * * @param array Array of objects to convert. * @param options Options including the target property and separator. * @returns String formed by joining the property values with the given separator. */ export declare function convertArrayToString<T extends GenericObject>(array: T[] | undefined, options: ArrayOfObjectsToStringOptions<T>): string; /** * * Converts an array of primitive values to a string using a custom separator. * * @example * convertArrayToString(['red', 'green', 'blue'], { separator: ' - ' }); * // "red - green - blue" * * @example * convertArrayToString([1, 2, 3]); * // "1, 2, 3" * * @param array Array of primitive values to convert. * @param options Optional separator configuration. * @returns String formed by joining array elements with the given separator. */ export declare function convertArrayToString<T extends Primitive>(array: T[] | undefined, options?: ArrayOfPrimitivesToStringOptions): string; /** * * A generic debounce function that delays the execution of a callback. * * @param callback - The function to debounce. * @param delay - The delay in milliseconds. Default is `300ms`. * @returns A debounced version of the callback function. * * @example * const debouncedSearch = debounceAction((query: string) => { * console.log(`Searching for: ${query}`); * }, 300); * * debouncedSearch('laptop'); // Executes after 300ms of inactivity. */ export declare function debounceAction<T extends VoidFunction>(callback: T, delay?: number): DelayedFn<T>; /** * * A generic throttle function that ensures a callback is executed at most once per specified interval. * * @param callback - The function to throttle. * @param delay - The delay in milliseconds. Default is `150ms`. * @returns A throttled version of the callback function. * * @example * const throttledResize = throttleAction(() => { * console.log('Resized'); * }, 300); * * window.addEventListener('resize', throttledResize); */ export declare function throttleAction<T extends VoidFunction>(callback: T, delay?: number): ThrottledFn<T>; /** * * Retrieves the names of all instance methods defined directly on a class prototype. * * @param cls - The class constructor (not an instance). * @returns A sorted array of instance method names. */ export declare function getInstanceMethodNames(cls: Constructor): string[]; /** * * Retrieves the names of all static methods defined directly on a class constructor. * * @param cls - The class constructor (not an instance). * @returns A sorted array of static method names. */ export declare function getStaticMethodNames(cls: Constructor): string[]; /** * * Counts the number of instance methods defined directly on a class prototype. * * @param cls - The class constructor (not an instance). * @returns The number of instance methods defined on the class prototype. */ export declare function countInstanceMethods(cls: Constructor): number; /** * * Counts the number of static methods defined directly on a class constructor. * * @param cls - The class constructor (not an instance). * @returns The number of static methods defined on the class constructor. */ export declare function countStaticMethods(cls: Constructor): number; /** * * Retrieves the names of all instance getters defined directly on a class prototype. * * @param cls - The class constructor (not an instance). * @returns A sorted array of instance getter names. */ export declare function getInstanceGetterNames(cls: Constructor): string[]; /** * * Retrieves the names of all static getters defined directly on a class constructor. * * @param cls - The class constructor (not an instance). * @returns A sorted array of static getter names. */ export declare function getStaticGetterNames(cls: Constructor): string[]; /** * * Gathers detailed information about the instance and static methods of a class. * * @param cls - The class constructor (not an instance). * @returns An object containing names and counts of instance and static methods. */ export declare function getClassDetails(cls: Constructor): ClassDetails; /** * * Parses any valid JSON string, optionally converting stringified primitives inside (nested) arrays or objects. * * @template T - Expected return type (default is unknown). * @param value - The JSON string to parse. * @param parsePrimitives - Whether to convert stringified primitives (default: `true`). * @returns The parsed JSON value typed as `T`, or the original parsed value with optional primitive conversion. * - Returns `{}` if parsing fails, such as when the input is malformed or invalid JSON or passing single quoted string. * * - *Unlike `parseJsonToObject`, which ensures the root value is an object, * this function returns any valid JSON structure such as arrays, strings, numbers, or objects.* * * This is useful when you're not sure of the root structure of the JSON, or when you expect something other than an object. * * @see `parseJsonToObject` for strict object-only parsing. */ export declare const parseJSON: <T = unknown>(value: string, parsePrimitives?: boolean) => T; /** * * Recursively parses primitive values inside objects and arrays. * * @template T - Expected return type after parsing (default is unknown). * @param input - Any input value to parse recursively. * @returns Input with primitives (strings like "true", "123") converted, typed as `T`. */ export declare function deepParsePrimitives<T = unknown>(input: unknown): T; //# sourceMappingURL=index.d.ts.map