es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
37 lines (36 loc) • 1.08 kB
JavaScript
import { uniq as uniq$1 } from "../../array/uniq.mjs";
import { combineEagerAndLazyFunctions } from "../_internal/lazy.mjs";
//#region src/fp/array/uniq.ts
/**
* Creates a function that removes duplicate values from an array, preserving
* the order of first occurrence. Equality follows SameValueZero, matching
* Set semantics. Use it with {@link pipe}.
*
* The returned function is lazy-capable inside {@link pipe}. It keeps a Set of
* values already emitted during each pipeline run.
*
* @template T - The type of elements in the array.
* @returns A function that maps a readonly array to a duplicate-free array.
*
* @example
* import { pipe, uniq } from 'es-toolkit/fp';
*
* pipe([1, 2, 2, 3, 3, 3], uniq());
* // => [1, 2, 3]
*/
function uniq() {
function uniqEager(array) {
return uniq$1(array);
}
const uniqLazy = (emit) => {
const seen = /* @__PURE__ */ new Set();
return (value) => {
if (seen.has(value)) return true;
seen.add(value);
return emit(value);
};
};
return combineEagerAndLazyFunctions(uniqEager, uniqLazy);
}
//#endregion
export { uniq };