UNPKG

ts-data-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![License](https://img.shields.

148 lines (145 loc) 4.43 kB
import '../../number/branded-types/finite-number.mjs'; import '../../number/branded-types/int.mjs'; import '../../number/branded-types/int16.mjs'; import '../../number/branded-types/int32.mjs'; import '../../number/branded-types/non-negative-finite-number.mjs'; import '../../number/branded-types/non-negative-int16.mjs'; import '../../number/branded-types/non-negative-int32.mjs'; import '../../number/branded-types/non-zero-finite-number.mjs'; import '../../number/branded-types/non-zero-int.mjs'; import '../../number/branded-types/non-zero-int16.mjs'; import '../../number/branded-types/non-zero-int32.mjs'; import '../../number/branded-types/non-zero-safe-int.mjs'; import '../../number/branded-types/non-zero-uint16.mjs'; import '../../number/branded-types/non-zero-uint32.mjs'; import '../../number/branded-types/positive-finite-number.mjs'; import '../../number/branded-types/positive-int.mjs'; import '../../number/branded-types/positive-int16.mjs'; import '../../number/branded-types/positive-int32.mjs'; import '../../number/branded-types/positive-safe-int.mjs'; import '../../number/branded-types/positive-uint16.mjs'; import '../../number/branded-types/positive-uint32.mjs'; import '../../number/branded-types/safe-int.mjs'; import '../../number/branded-types/safe-uint.mjs'; import '../../number/branded-types/uint.mjs'; import '../../number/branded-types/uint16.mjs'; import { Uint32 } from '../../number/branded-types/uint32.mjs'; import '../../number/enum/int8.mjs'; import '../../number/enum/uint8.mjs'; import '../../number/num.mjs'; import '../../number/refined-number-utils.mjs'; import { size } from './array-utils-size.mjs'; import { sliceClamped } from './array-utils-slice-clamped.mjs'; import { isEmpty } from './array-utils-validation.mjs'; /** * Returns all elements of an array except the first one. * * @example * * ```ts * { * const scientists = ['Ada', 'Grace', 'Katherine'] as const; * * const remainder = Arr.tail(scientists); * * assert.deepStrictEqual(remainder, ['Grace', 'Katherine']); * * assert.isTrue(remainder.length === 2); * } * * { * const values = [1, 2, 3] as const; * * const remainder = Arr.rest(values); * * const emptyRemainder = Arr.rest([1] as const); * * assert.deepStrictEqual(remainder, [2, 3] as const); * * assert.deepStrictEqual(emptyRemainder, [] as const); * } * ``` */ const tail = (array) => // eslint-disable-next-line total-functions/no-unsafe-type-assertion array.slice(1); /** * Returns all elements of an array except the last one. * * @example * * ```ts * const queue = ['task-1', 'task-2', 'task-3'] as const; * * const withoutLast = Arr.butLast(queue); * * assert.deepStrictEqual(withoutLast, ['task-1', 'task-2']); * * assert.isTrue(withoutLast.length === 2); * ``` */ const butLast = (array) => // eslint-disable-next-line total-functions/no-unsafe-type-assertion (isEmpty(array) ? [] : array.slice(0, -1)); function take(...args) { switch (args.length) { case 2: { const [array, num] = args; return sliceClamped(array, 0, num); } case 1: { const [num] = args; return (array) => take(array, num); } } } function takeLast(...args) { switch (args.length) { case 2: { const [array, num] = args; return sliceClamped(array, Uint32.sub(size(array), num), size(array)); } case 1: { const [num] = args; return (array) => takeLast(array, num); } } } function skip(...args) { switch (args.length) { case 2: { const [array, num] = args; return sliceClamped(array, num, size(array)); } case 1: { const [num] = args; return (array) => skip(array, num); } } } function skipLast(...args) { switch (args.length) { case 2: { const [array, num] = args; return sliceClamped(array, 0, Uint32.sub(size(array), num)); } case 1: { const [num] = args; return (array) => skipLast(array, num); } } } /** * Alias for `tail`. * * @see {@link tail} */ const rest = tail; /** * Alias for `skip`. * * @see {@link skip} */ const drop = skip; export { butLast, drop, rest, skip, skipLast, tail, take, takeLast }; //# sourceMappingURL=array-utils-slicing.mjs.map