/** * Creates sort callback which can be used for `array.sort` input. * A custom key function can be supplied to customize the sort order. * Key can return any nested data structure, supports number, booleans, arrays. * * @example * ``` * // Sort by object key * arr.sort(sortByCb(v => v.myKey)) * // Sort by number in reversed order * arr.sort(sortByCb(v => v.numberKey * -1)) * // Sort by object key, then by boolean value in revered order * arr.sort(sortByCb(v => [v.myKey, !v.boolValue]) * ``` * @group Array * @param key a function to execute to decide the order * @returns `array.sort` compatible callback */ export declare function sortByCb(key?: (item: T) => unknown): (a: T, b: T) => number; /** * Stable sort using provided callback without modifications. * A custom key function can be supplied to customize the sort order. * Key can return any nested data structure, supports number, booleans, arrays, objects. * * @example * ``` * // Sort by object key * sortBy(arr, v => v.myKey)) * // Sort by number in reversed order * sortBy(arr, v => v.numberKey * -1)) * // Sort by object key, then by boolean value in revered order * sortBy(arr, v => [v.myKey, !v.boolValue]) * ``` * @group Array * @param arr any array * @param key a function to execute to decide the order * @returns sorted array copy */ export declare function sortBy(arr: T[], key?: (item: T) => unknown): T[]; export declare function sortBy(arr: readonly T[], key?: (item: T) => unknown): readonly T[]; /** * Return an object that produces a array of numbers from start (inclusive) to stop (exclusive) by step. * * @example * ``` * >>> generateRange(4) * [0, 1, 2, 3] * >>> generateRange(3,6) * [3, 4, 5] * >>> generateRange(0,10,2) * [0, 2, 4, 6, 8] * >>> generateRange(10,0,-1) * [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] * >>> generateRange(8,2,-2) * [8, 6, 4] * ``` * @group Array * @param stop an integer number specifying at which position to stop (not included) * @returns generated array by range */ export declare function generateRange(stop: number): number[]; /** * Return an object that produces a array of numbers from start (inclusive) to stop (exclusive) by step. * * @example * ``` * >>> generateRange(4) * [0, 1, 2, 3] * >>> generateRange(3,6) * [3, 4, 5] * >>> generateRange(0,10,2) * [0, 2, 4, 6, 8] * >>> generateRange(10,0,-1) * [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] * >>> generateRange(8,2,-2) * [8, 6, 4] * ``` * @group Array * @param start an integer number specifying at which position to start * @param stop an integer number specifying at which position to stop (not included) * @param step an integer number specifying the incrementation * @returns generated array by range */ export declare function generateRange(start: number, stop: number, step?: number): number[]; /** * Calls a defined callback function on each element of an array. Then, flattens the result into * a new array. * This is identical to a map followed by flat with depth 1. * * Use [Array.flatMap](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap) if possible or polyfill globally: * * ``` * Array.prototype.flatMap = function(callback) { * return flatMap(this, callback) * } * ``` * * @example * ``` * const arr1 = [1, 2, 3, 4] * * arr1.map(x => [x * 2]) * // [[2], [4], [6], [8]] * * flatMap(arr1, x => [x * 2]) * // [2, 4, 6, 8] * * // only one level is flattened * flatMap(arr1, x => [[x * 2]]) * // [[2], [4], [6], [8]] * ``` * @group Array * @tutorial https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap * @param array any array * @param callback A function that accepts up to three arguments. The flatMap method calls the callback function one time for each element in the array. */ export declare function flatMap(array: T[], callback: (item: T, index: number, array: T[]) => U | readonly U[]): U[]; export declare function flatMap(array: readonly T[], callback: (item: T, index: number, array: readonly T[]) => U | readonly U[]): readonly U[]; type FlatArray = { done: Arr; recur: Arr extends ReadonlyArray ? FlatArray : Arr; }[Depth extends -1 ? 'done' : 'recur']; /** * Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth. * * Use [Array.flat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) if possible or polyfill globally: * * ``` * Array.prototype.flat = function(depth) { * return flatten(this, depth) * } * ``` * * @example * ``` * flatten([1, 2, [3, 4]]) * // [1, 2, 3, 4] * * flatten([1, 2, [3, 4, [5, 6]]]) * // [1, 2, 3, 4, [5, 6]] * * flatten([1, 2, [3, 4, [5, 6]]], 2) * // [1, 2, 3, 4, 5, 6] * ``` * @group Array * @tutorial https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat * @param depth The maximum recursion depth */ export declare function flatten(array: A, depth?: D): FlatArray[]; export declare function flatten(array: A, depth?: D): readonly FlatArray[]; /** * Splits an array into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements. * * @example * ``` * chunk([1, 2, 3, 4, 5], 2) * // [[1, 2], [3, 4], [5]] * ``` * @param size Chunk maximum size * @group Array */ export declare function chunk(array: T[], size: number): T[][]; export declare function chunk(array: ReadonlyArray, size: number): ReadonlyArray>; /** * Groups an array based on callback return value * * @example * ``` * groupBy([6.1, 4.2, 6.3], Math.floor) * // [ [4, [4.2]], [6, [6.1, 6.3]] ] * * groupBy(['one', 'two', 'three'], v => [v.length, v.includes('a')]) * // [ [[5, false], ['three']], [[3, false], ['one', 'two']] ] * ``` * @group Array */ export declare function groupBy(array: T[], keyCallback: (value: T) => G): [G, T[]][]; export declare function groupBy(array: ReadonlyArray, keyCallback: (value: T) => G): ReadonlyArray<[G, T[]]>; /** * Creates indexed object by provided callback. * * If array is returned, creates a separate index for each array element. * * @example * ``` * indexBy(['one', 'two', 'three'], v => v.length) * // { '5': ['three'], '3': ['one', 'two'] } * * indexBy(['one', 'two', 'three'], v => [v.length, v.length + 1]) * // { '5': ['three'], '6': ['three'], '3': ['one', 'two'], '4': ['one', 'two'] } * ``` * @group Array */ export declare function indexBy(array: T[], keyCallback: (value: T) => string | number | ReadonlyArray): Record; export declare function indexBy(array: ReadonlyArray, keyCallback: (value: T) => string | number | ReadonlyArray): Record>; /** * Removes duplicate values from the array. * * @example * ``` * deduplicate([1, 1, 2, 3, 3]) * // [1, 2, 3] * ``` * @group Array */ export declare function deduplicate(array: T[]): T[]; export declare function deduplicate(array: ReadonlyArray): ReadonlyArray; /** * Removes duplicate values from the array by given callback. * * @example * ``` * deduplicateBy([ { a: 1 }, { a: 1 } ], v => v.a) * // [ { a: 1 } ] * ``` * @group Array */ export declare function deduplicateBy(array: T[], key: (value: T) => unknown): T[]; export declare function deduplicateBy(array: ReadonlyArray, key: (value: T) => unknown): ReadonlyArray; export declare function deduplicateBy(array: ReadonlyArray, key: (value: T) => unknown): ReadonlyArray; /** * Creates an array of array values not included in the other given arrays. * * @example * ``` * difference([2, 1], [2, 3]) * // [1] * * difference([2, 1], ['2', '3'], (a, b) => a === parseInt(b)) * // [1] * ``` * @group Array */ export declare function difference(array: ReadonlyArray, values: ReadonlyArray, key?: (value: T | T2) => unknown): ReadonlyArray; export declare function difference(array: T[], values: T2[], key?: (value: T) => unknown): T[]; /** * Creates an array of unique values that are included in all given arrays. * * @example * ``` * intersection([[2, 1], [2, 3]]) * // [2] * ``` * @group Array */ export declare function intersection(values: ReadonlyArray>, key?: (value: T) => unknown): ReadonlyArray; export declare function intersection(values: T[][], key?: (value: T) => unknown): T[]; /** * Creates an array of unique values that are included in all given arrays. * * @example * ``` * intersection([[2, 1], [2, 3]]) * // [2] * ``` * @group Array */ export declare function union(values: ReadonlyArray>, key?: (value: T) => unknown): ReadonlyArray; export declare function union(values: T[][], key?: (value: T) => unknown): T[]; /** * Checks if any value from provided values array is included in the provided array using provided `comparator` function (or by default comparing using `===`). * * @example * ``` * includesAny([1, 2, 3], [3, 4, 5]) * // true * ``` * @group Array */ export declare function includesAny(array: ReadonlyArray, values: ReadonlyArray, key?: (value: T | T2) => unknown): boolean; /** * Checks if all values from provided values array are included in the provided array using provided `comparator` function (or by default comparing using `===`). * * @example * ``` * includesAll([1, 2, 3], [1, 2]) * // true * ``` * @group Array */ export declare function includesAll(array: ReadonlyArray, values: ReadonlyArray, key?: (value: T | T2) => unknown): boolean; /** * Returns the index of the first element in the array where predicate is true, and -1 * otherwise. * * @example * ``` * const obj = [{name: 'abc'}] * const index = findIndex(obj, v => v.name = 'abc') * // 0 * ``` * @param predicate find calls predicate once for each element of the array, in ascending * order, until it finds one where predicate returns true. If such an element is found, * findIndex immediately returns that element index. Otherwise, findIndex returns -1. * @group Array */ export declare function findIndex(array: ReadonlyArray, compare: (value: T) => boolean): number; export {};