//#region src/arrayUtils.d.ts /** * Allow to filter and map with better typing ergonomics * * In the `mapFilter` function return `false` to reject the item, or any other * value to map it. * * @example * // Filter reject and turn value into `value mapped` * const items = ['value', 'value', 'reject', 'reject']; * * const mappedItems = filterAndMap(items, (item) => * item === 'reject' ? false : `${item} mapped`, * ); * * mappedItems; // ['value mapped', 'value mapped'] * * @param array * @param mapFilter */ declare function filterAndMap(array: IterableIterator | readonly T[], mapFilter: (item: T, index: number) => false | R): R[]; type FilterAndMapReturn = false | T; type SortOrder = 'desc' | 'asc'; type SortByValueFn = (item: T) => (number | string)[] | number | string; type SortByProps = { order?: SortOrder | SortOrder[]; } | SortOrder | SortOrder[]; /** * Sort an array based on a value * * Sort by `ascending` order by default * * Use `Infinity` as as wildcard to absolute max and min values * * @example * const items = [1, 3, 2, 4]; * * const sortedItems = sortBy(items, (item) => item); * // [1, 2, 3, 4] * * const items2 = [ * { a: 1, b: 2 }, * { a: 2, b: 1 }, * { a: 1, b: 1 }, * ]; * * // return a array to sort by multiple values * const sortedItems = sortBy(items, (item) => [item.a, item.b]); * * @param arr * @param sortByValue * @param props */ declare function sortBy(arr: T[], sortByValue: SortByValueFn, props?: SortByProps): T[]; /** * Get the correct 0 based value for sync with other array in ascending order * * @example * ```ts * const items = [1, 2, 3]; * * const index = sortBy( * items, * (item) => getAscIndexOrder( * followOrder.findIndex((order) => order === item) * ) * ); * ```; * * @param index */ declare function getAscIndexOrder(index: number | undefined): number; declare function arrayWithPrev(array: T[]): [current: T, prev: T | null][]; declare function arrayWithPrevAndIndex(array: T[]): { item: T; prev: T | null; index: number; }[]; declare function isInArray(value: T, oneOf: readonly U[]): value is U; declare function isInArrayWithoutNarrowing(value: T, oneOf: readonly U[]): boolean; declare function looseIsInArray(value: unknown, array: readonly unknown[]): boolean; declare function findAfterIndex(array: T[], index: number, predicate: (item: T) => boolean): T | undefined; declare function findBeforeIndex(array: T[], index: number, predicate: (item: T) => boolean): T | undefined; declare function rejectArrayUndefinedValues(array: T): T; declare function hasDuplicates(array: T[], getKey?: (item: T) => unknown): boolean; declare function rejectDuplicates(array: T[] | readonly T[], getKey?: (item: T) => unknown): T[]; declare function truncateArray(array: T[], maxLength: number, appendIfTruncated?: T | ((truncatedCount: number) => T)): T[]; /** * Finds the first item in an array where the predicate returns a non-false * value and returns that mapped value. * * Combines find and map operations - applies the predicate to each item until * one returns a value that is not `false`, then returns that mapped value. If * no item matches, returns `undefined`. * * @example * const users = [ * { id: 1, name: 'Alice' }, * { id: 2, name: 'Bob' }, * ]; * * const foundName = findAndMap(users, (user) => * user.id === 2 ? user.name.toUpperCase() : false, * ); * // foundName is 'BOB' * * @param array - The array to search through * @param predicate - Function that returns a mapped value or `false` to skip * the item * @returns The first mapped value that is not `false`, or `undefined` if no * item matches */ declare function findAndMap(array: T[], predicate: (value: T) => R | false): R | undefined; type ArrayOps = { /** * Filter and map an array * * @example * const items = [1, 2, 3]; * * const enhancedItems = arrayOps(items); * * enhancedItems.filterAndMap((item) => (item === 2 ? false : item)); * * @param mapFilter - A function that takes an item and returns a value or * `false` to reject the item. */ filterAndMap: (mapFilter: (item: T, index: number) => false | R) => R[]; sortBy: (sortByValue: SortByValueFn, props: SortByProps) => T[]; rejectDuplicates: (getKey: (item: T) => unknown) => T[]; findAndMap: (predicate: (value: T) => R | false) => R | undefined; }; /** * Enhance an array with extra methods * * @example * const enhancedItems = arrayOps(array); * * enhancedItems.filterAndMap((item) => (item === 2 ? false : item)); * enhancedItems.sortBy((item) => item); * enhancedItems.rejectDuplicates((item) => item); * * @param array */ declare function arrayOps(array: T[]): ArrayOps; /** * Inserts a separator value between each element in an array. * * @example * intersperse([1, 2, 3], 0); // [1, 0, 2, 0, 3] * * @param array - The array to intersperse * @param separator - The value to insert between elements * @returns A new array with separator values inserted between elements */ declare function intersperse(array: T[], separator: I): (T | I)[]; /** * Creates an array by repeating a value a specified number of times, optionally * with a separator between each repetition. * * @example * repeat('x', 3); // ['x', 'x', 'x'] * repeat('x', 3, '-'); // ['x', '-', 'x', '-', 'x'] * * @param value - The value to repeat * @param count - Number of times to repeat the value * @param separator - Optional separator to insert between repetitions * @returns A new array with the repeated values */ declare function repeat(value: T, count: number, separator?: T): T[]; //#endregion export { FilterAndMapReturn, SortByProps, SortByValueFn, arrayOps, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, findAfterIndex, findAndMap, findBeforeIndex, getAscIndexOrder, hasDuplicates, intersperse, isInArray, isInArrayWithoutNarrowing, looseIsInArray, rejectArrayUndefinedValues, rejectDuplicates, repeat, sortBy, truncateArray };