UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

27 lines (25 loc) 654 B
import { dfdlT } from "@monstermann/dfdl"; import { cloneArray, markAsMutable } from "@monstermann/remmi"; //#region src/array/take.ts /** * `take(array, amount)` * * Returns a new array containing the first `amount` elements from `array`. * * ```ts * take([1, 2, 3, 4, 5], 3); // [1, 2, 3] * ``` * * ```ts * pipe([1, 2, 3, 4, 5], take(3)); // [1, 2, 3] * ``` */ const take = dfdlT((target, amount) => { if (!Number.isInteger(amount) || target.length <= amount) return target; if (amount <= 0) return markAsMutable([]); target = cloneArray(target); target.splice(amount, target.length - amount); return target; }, 2); //#endregion export { take };