UNPKG

dev-utils-plus

Version:

Type-safe utility functions for JavaScript/TypeScript: string, array, object, date, validation, crypto, format, math

125 lines 4.17 kB
/** * Array utility functions for common operations */ /** * Removes duplicate elements from an array * @param arr - The array to remove duplicates from * @returns A new array with unique elements * @example * unique([1, 2, 2, 3, 3, 4]) // [1, 2, 3, 4] */ export declare function unique<T>(arr: T[]): T[]; /** * Shuffles an array using Fisher-Yates algorithm * @param arr - The array to shuffle * @returns A new shuffled array * @example * shuffle([1, 2, 3, 4]) // [3, 1, 4, 2] (random order) */ export declare function shuffle<T>(arr: T[]): T[]; /** * Groups array elements by a key function */ export declare function groupBy<T, K extends string | number>(arr: T[], keyFn: (item: T) => K): Record<K, T[]>; /** * Chunks an array into smaller arrays of specified size * @param arr - The array to chunk * @param size - The size of each chunk (must be positive integer) * @returns Array of chunked arrays * @example * chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]] */ export declare function chunk<T>(arr: T[], size: number): T[][]; /** * Flattens a nested array to a single level * @param arr - The array to flatten * @param depth - Maximum depth to flatten (default: Infinity) * @returns A new flattened array * @example * flatten([1, [2, 3], [4, [5, 6]]]) // [1, 2, 3, 4, 5, 6] * flatten([1, [2, [3, [4]]]], 2) // [1, 2, 3, [4]] */ export declare function flatten<T>(arr: T[], depth?: number): T[]; /** * Finds the intersection of multiple arrays */ export declare function intersection<T>(...arrays: T[][]): T[]; /** * Finds the difference between two arrays */ export declare function difference<T>(arr1: T[], arr2: T[]): T[]; /** * Sorts an array of objects by a specific key */ export declare function sortBy<T>(arr: T[], key: keyof T, direction?: 'asc' | 'desc'): T[]; /** * Creates a range of numbers */ export declare function range(start: number, end: number, step?: number): number[]; /** * Counts occurrences of elements in an array * @param arr - The array to count elements from * @param keyFn - Optional function to generate custom keys * @returns Object with element counts * @example * countBy([1, 2, 2, 3, 3, 3]) // {'1': 1, '2': 2, '3': 3} * countBy(['apple', 'banana', 'apple'], x => x.length) // {'5': 2, '6': 1} */ export declare function countBy<T, K extends string | number = string>(arr: T[], keyFn?: (item: T) => K): Record<K, number>; /** * Removes falsy values from an array */ export declare function compact<T>(arr: T[]): T[]; /** * Gets the last element of an array * @param arr - The array to get the last element from * @returns The last element or undefined if array is empty * @example * last([1, 2, 3]) // 3 * last([]) // undefined */ export declare function last<T>(arr: T[]): T | undefined; /** * Gets the first element of an array * @param arr - The array to get the first element from * @returns The first element or undefined if array is empty * @example * first([1, 2, 3]) // 1 * first([]) // undefined */ export declare function first<T>(arr: T[]): T | undefined; /** * Takes the first n elements from an array * @param arr - The array to take elements from * @param n - Number of elements to take * @returns Array with first n elements * @example * take([1, 2, 3, 4, 5], 3) // [1, 2, 3] */ export declare function take<T>(arr: T[], n: number): T[]; /** * Drops the first n elements from an array * @param arr - The array to drop elements from * @param n - Number of elements to drop * @returns Array with first n elements removed * @example * drop([1, 2, 3, 4, 5], 2) // [3, 4, 5] */ export declare function drop<T>(arr: T[], n: number): T[]; /** * Finds the maximum element in an array * @param arr - Array of numbers * @returns Maximum value or undefined if empty * @example * max([1, 5, 3, 9, 2]) // 9 */ export declare function max(arr: number[]): number | undefined; /** * Finds the minimum element in an array * @param arr - Array of numbers * @returns Minimum value or undefined if empty * @example * min([1, 5, 3, 9, 2]) // 1 */ export declare function min(arr: number[]): number | undefined; //# sourceMappingURL=index.d.ts.map