/** * Copyright (c) 2025-present, NDLA. * * This source code is licensed under the GPLv3 license found in the * LICENSE file in the root directory of this source tree. * */ /** * Groups an array of items by a key returned by the predicate function. * @param arr The array of items to group. * @param predicate A function converts the item to a key to group it by. * @returns An object where the keys are the result of the predicate function and the values are arrays of items with that key. * @example const grouped = groupBy([{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 1, name: 'Charlie' }], item => item.id) * // => { 1: [{ id: 1, name: 'Alice' }, { id: 1, name: 'Charlie' }], 2: [{ id: 2, name: 'Bob' }] } */ export declare const groupBy: (arr: readonly T[] | undefined | null, predicate: (item: T) => K) => Record; /** * Remove duplicate items from an array. * @param arr The array to remove duplicates from. * @returns A new array with duplicate items removed. * @example const unique = uniq([1, 2, 3, 1, 2, 4]) * // => [1, 2, 3, 4] */ export declare const uniq: (arr: readonly T[] | undefined | null) => T[]; /** * Removes duplicate items from an array based the result of a predicate function. The predicate function is called with each item in the array and should return a key to compare items by. * The first item with a given key is kept, and all subsequent items with the same key are removed. * @param arr The array to remove duplicates from. * @param predicate A function that converts an item to a key to compare items by. * @returns A new array with duplicate items removed. * @example const unique = uniqBy([{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 1, name: 'Charlie' }], item => item.id) * // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }] */ export declare const uniqBy: (arr: readonly T[] | undefined | null, predicate: (item: T) => K) => T[]; /** * Separates an array into two arrays based on the result of a predicate function. The first array contains items where the predicate function returns true, and the second array contains items where the predicate function returns false. * @param arr The array to partition. * @param predicate A function that returns true or false for each item. * @returns A tuple containing two arrays: the first array contains items where the predicate function returns true, and the second array contains items where the predicate function returns false. * @example const [even, odd] = partition([1, 2, 3, 4, 5], n => n % 2 === 0) * // => [ [2, 4], [1, 3, 5] ] */ export declare const partition: (arr: readonly T[] | undefined | null, predicate: (item: T) => boolean) => [T[], T[]]; /** * Converts an array of items into an object where the keys are the result of a predicate function and the values are the items. * The last item with a given key is kept, and all previous items with the same key are overwritten. * @param arr The array of items to convert. * @param predicate A function that converts an item to a key. * @returns An object where the keys are the result of the predicate function and the values are the items. * @example const keyed = keyBy([{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }], item => item.id) * // => { 1: { id: 1, name: 'Alice' }, 2: { id: 2, name: 'Bob' } } */ export declare const keyBy: (arr: readonly T[] | undefined | null, predicate: (item: T) => K) => Record; /** * Sorts an array of items based on the result of a predicate function. This is a stable sort, so items with the same value will retain their original order. * Beware; Returning mixed values from the predicate function can yield unexpected results. For best results, return strings, booleans, numbers or null/undefined. * @param arr The array of items to sort. * @param predicate A function that converts an item to a value to sort by. * @returns A new array with the items sorted by the result of the predicate function. * @example const sorted = sortBy([{ id: 2, name: 'Bob' }, { id: 1, name: 'Alice' }], item => item.id) * // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }] */ export declare const sortBy: (arr: readonly T[] | undefined | null, predicate: (value: T) => K) => T[];