UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

24 lines (22 loc) 565 B
import { dfdlT } from "@monstermann/dfdl"; import { markAsMutable } from "@monstermann/remmi"; //#region src/array/unique.ts /** * `unique(target)` * * Returns a new array with only the unique elements from `target`, preserving the order of first occurrence. * * ```ts * unique([1, 2, 2, 3, 1]); // [1, 2, 3] * ``` * * ```ts * pipe([1, 2, 2, 3, 1], unique()); // [1, 2, 3] * ``` */ const unique = dfdlT((target) => { const set = new Set(target); return set.size === target.length ? target : markAsMutable(Array.from(set)); }, 1); //#endregion export { unique };