| 1 | /**
|
|---|
| 2 | * Splits an array into two groups based on a predicate function.
|
|---|
| 3 | *
|
|---|
| 4 | * This function takes an array and a predicate function. It returns a tuple of two arrays:
|
|---|
| 5 | * the first array contains elements for which the predicate function returns true, and
|
|---|
| 6 | * the second array contains elements for which the predicate function returns false.
|
|---|
| 7 | *
|
|---|
| 8 | * @template T - The type of elements in the array.
|
|---|
| 9 | * @template {T} U - The type being filtered for.
|
|---|
| 10 | * @param {T[]} arr - The array to partition.
|
|---|
| 11 | * @param {(value: T, index: number, array: readonly T[]) => value is U} isInTruthy - A type guard that determines whether an
|
|---|
| 12 | * element should be placed in the truthy array. The function is called with each element
|
|---|
| 13 | * of the array and its index.
|
|---|
| 14 | * @returns {[U[], Exclude<T, U>[]]} A tuple containing two arrays: the first array contains elements for
|
|---|
| 15 | * which the predicate returned true, and the second array contains elements for which the
|
|---|
| 16 | * predicate returned false.
|
|---|
| 17 | *
|
|---|
| 18 | * @example
|
|---|
| 19 | * const array = [1, 2, 3, 4] as const;
|
|---|
| 20 | * const isEven = (x: number): x is 2 | 4 => x % 2 === 0;
|
|---|
| 21 | * const [even, odd]: [(2 | 4)[], (2 | 4)[]] = partition(array, isEven);
|
|---|
| 22 | * // even will be [2, 4], and odd will be [1, 3]
|
|---|
| 23 | */
|
|---|
| 24 | declare function partition<T, U extends T>(arr: readonly T[], isInTruthy: (value: T, index: number, array: readonly T[]) => value is U): [truthy: U[], falsy: Array<Exclude<T, U>>];
|
|---|
| 25 | /**
|
|---|
| 26 | * Splits an array into two groups based on a predicate function.
|
|---|
| 27 | *
|
|---|
| 28 | * This function takes an array and a predicate function. It returns a tuple of two arrays:
|
|---|
| 29 | * the first array contains elements for which the predicate function returns true, and
|
|---|
| 30 | * the second array contains elements for which the predicate function returns false.
|
|---|
| 31 | *
|
|---|
| 32 | * @template T - The type of elements in the array.
|
|---|
| 33 | * @param {T[]} arr - The array to partition.
|
|---|
| 34 | * @param {(value: T, index: number) => boolean} isInTruthy - A predicate function that determines
|
|---|
| 35 | * whether an element should be placed in the truthy array. The function is called with each
|
|---|
| 36 | * element of the array and its index.
|
|---|
| 37 | * @returns {[T[], T[]]} A tuple containing two arrays: the first array contains elements for
|
|---|
| 38 | * which the predicate returned true, and the second array contains elements for which the
|
|---|
| 39 | * predicate returned false.
|
|---|
| 40 | *
|
|---|
| 41 | * @example
|
|---|
| 42 | * const array = [1, 2, 3, 4, 5];
|
|---|
| 43 | * const isEven = x => x % 2 === 0;
|
|---|
| 44 | * const [even, odd] = partition(array, isEven);
|
|---|
| 45 | * // even will be [2, 4], and odd will be [1, 3, 5]
|
|---|
| 46 | */
|
|---|
| 47 | declare function partition<T>(arr: readonly T[], isInTruthy: (value: T, index: number, array: readonly T[]) => boolean): [truthy: T[], falsy: T[]];
|
|---|
| 48 |
|
|---|
| 49 | export { partition };
|
|---|