32 lines
1.3 KiB
JavaScript
32 lines
1.3 KiB
JavaScript
import { differenceBy as differenceBy$1 } from "../../array/differenceBy.mjs";
|
|
import { combineEagerAndLazyFunctions, createLazyFunction } from "../_internal/lazy.mjs";
|
|
//#region src/fp/array/differenceBy.ts
|
|
/**
|
|
* Creates a function that returns values whose mapped identity is absent from another array.
|
|
*
|
|
* The mapper is applied to values from both arrays. The returned function is
|
|
* lazy-capable inside {@link pipe}.
|
|
*
|
|
* @template T - The type of elements in the piped array.
|
|
* @template U - The type of elements in the configured array.
|
|
* @param secondArray - Values to exclude from the piped array after mapping.
|
|
* @param mapper - Maps values from both arrays to comparison keys.
|
|
* @returns A function that maps the piped array to its mapped difference.
|
|
*
|
|
* @example
|
|
* import { differenceBy, pipe } from 'es-toolkit/fp';
|
|
*
|
|
* pipe([{ id: 1 }, { id: 2 }], differenceBy([2], value => typeof value === 'number' ? value : value.id));
|
|
* // => [{ id: 1 }]
|
|
*/
|
|
function differenceBy(secondArray, mapper) {
|
|
const mappedSecondSet = new Set(secondArray.map((item) => mapper(item)));
|
|
function differenceByEager(array) {
|
|
return differenceBy$1(array, secondArray, mapper);
|
|
}
|
|
return combineEagerAndLazyFunctions(differenceByEager, createLazyFunction((value, _index, emit) => {
|
|
if (!mappedSecondSet.has(mapper(value))) emit(value);
|
|
}));
|
|
}
|
|
//#endregion
|
|
export { differenceBy };
|