| 1 | /**
|
|---|
| 2 | * Computes the difference between two arrays after mapping their elements through a provided function.
|
|---|
| 3 | *
|
|---|
| 4 | * This function takes two arrays and a mapper function. It returns a new array containing the elements
|
|---|
| 5 | * that are present in the first array but not in the second array, based on the identity calculated
|
|---|
| 6 | * by the mapper function.
|
|---|
| 7 | *
|
|---|
| 8 | * Essentially, it filters out any elements from the first array that, when
|
|---|
| 9 | * mapped, match an element in the mapped version of the second array.
|
|---|
| 10 | *
|
|---|
| 11 | * @template T, U
|
|---|
| 12 | * @param {T[]} firstArr - The primary array from which to derive the difference.
|
|---|
| 13 | * @param {U[]} secondArr - The array containing elements to be excluded from the first array.
|
|---|
| 14 | * @param {(value: T | U) => unknown} mapper - The function to map the elements of both arrays. This function
|
|---|
| 15 | * is applied to each element in both arrays, and the comparison is made based on the mapped values.
|
|---|
| 16 | * @returns {T[]} A new array containing the elements from the first array that do not have a corresponding
|
|---|
| 17 | * mapped identity in the second array.
|
|---|
| 18 | *
|
|---|
| 19 | * @example
|
|---|
| 20 | * const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|---|
| 21 | * const array2 = [{ id: 2 }, { id: 4 }];
|
|---|
| 22 | * const mapper = item => item.id;
|
|---|
| 23 | * const result = differenceBy(array1, array2, mapper);
|
|---|
| 24 | * // result will be [{ id: 1 }, { id: 3 }] since the elements with id 2 are in both arrays and are excluded from the result.
|
|---|
| 25 | *
|
|---|
| 26 | * @example
|
|---|
| 27 | * const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }];
|
|---|
| 28 | * const array2 = [2, 4];
|
|---|
| 29 | * const mapper = item => (typeof item === 'object' ? item.id : item);
|
|---|
| 30 | * const result = differenceBy(array1, array2, mapper);
|
|---|
| 31 | * // result will be [{ id: 1 }, { id: 3 }] since 2 is present in both arrays after mapping, and is excluded from the result.
|
|---|
| 32 | */
|
|---|
| 33 | declare function differenceBy<T, U>(firstArr: readonly T[], secondArr: readonly U[], mapper: (value: T | U) => unknown): T[];
|
|---|
| 34 |
|
|---|
| 35 | export { differenceBy };
|
|---|