UNPKG

@monstermann/fn

Version:

A utility library for TypeScript.

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