UNPKG

dastal

Version:

Data Structures & Algorithms implementations

66 lines 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.splice = exports.isTypedArray = exports.isArray = void 0; const env_1 = require("src/env"); const numberUtils_1 = require("src/math/numberUtils"); /** * Check if a value is an Array or TypedArray. * * @param obj - The value to check. * * @returns `true` if an Array or TypedArray, otherwise `false`. */ function isArray(obj) { return obj instanceof Array || exports.isTypedArray(obj); } exports.isArray = isArray; /** * Check if a value is a TypedArray. * * See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) * for more details. * * @param obj - The value to check. * * @returns `true` if a TypedArray, otherwise `false`. */ exports.isTypedArray = (() => { const TypedArray = Object.getPrototypeOf(Uint8Array); return function isTypedArray(obj) { return obj instanceof TypedArray; }; })(); /** * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. * * Contrary to Array.splice, this will not throw an error if too many new elements are given as arguments. * * @param array - The array to splice. * @param start - The zero-based location in the array from which to start removing elements. * @param count - The number of elements to remove. * @param elements - The new elements to splice in. * * @returns - An array containing the deleted elements. */ function splice(array, start, count, elements) { start = numberUtils_1.clamp(numberUtils_1.wrapLeft(start ?? 0, 0, array.length), 0, array.length); count = numberUtils_1.clamp(count ?? array.length, 0, array.length - start); // Base case. if (elements == null) { return array.splice(start, count); } // Perform the initial splice. const numArgs = Math.floor(0.8 * env_1.getMaxArgumentsLength(0) - 2); let args = elements.slice(0, numArgs); const out = array.splice.bind(array, start, count).apply(array, args); // Splice in new elements as long as there is space in the array const n = elements.length; for (let i = args.length; i < n; i += args.length) { args = elements.splice(i, numArgs); array.splice.bind(array, start + i, 0).apply(array, args); } // Return deleted elements. return out; } exports.splice = splice; //# sourceMappingURL=arrayUtils.js.map