UNPKG

@rimbu/base

Version:

Utilities to implement Rimbu collections

240 lines 8.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapSparse = exports.copySparse = exports.splice = exports._spliceOld = exports._spliceNew = exports.init = exports.tail = exports.insert = exports._insertOld = exports._insertNew = exports.mod = exports._modOld = exports._modNew = exports.update = exports._updateOld = exports._updateNew = exports.last = exports._lastOld = exports._lastNew = exports.prepend = exports._prependOld = exports._prependNew = exports.reverseMap = exports.map = exports.forEach = exports.reverse = exports._reverseOld = exports._reverseNew = exports.concat = exports.append = exports._appendOld = exports._appendNew = void 0; var tslib_1 = require("tslib"); var common_1 = require("@rimbu/common"); function _appendNew(array, value) { return array.toSpliced(array.length, 0, value); } exports._appendNew = _appendNew; function _appendOld(array, value) { var clone = array.slice(); clone.push(value); return clone; } exports._appendOld = _appendOld; // Returns a copy of the array with the given value appended exports.append = "toSpliced" in Array.prototype ? _appendNew : _appendOld; // Returns the concatenation of the two arrays, potentially reusing the input array if one of the arrays is empty function concat(first, second) { if (first.length === 0) return second; if (second.length === 0) return first; return first.concat(second); } exports.concat = concat; function _reverseNew(array, start, end) { var source = undefined !== start || undefined !== end ? array.slice(start !== null && start !== void 0 ? start : 0, (end !== null && end !== void 0 ? end : array.length - 1) + 1) : array; return source.toReversed(); } exports._reverseNew = _reverseNew; function _reverseOld(array, start, end) { var _start = start !== null && start !== void 0 ? start : 0; var _end = end !== null && end !== void 0 ? end : array.length - 1; var length = _end - _start + 1; var res = []; var arrayIndex = _start - 1; var resIndex = length - 1; while (++arrayIndex <= _end) res[resIndex--] = array[arrayIndex]; return res; } exports._reverseOld = _reverseOld; // Returns an copy of the array between the start and end indices, with the elements in reversed order. exports.reverse = 'toReversed' in Array.prototype ? _reverseNew : _reverseOld; // Performs given function on each element of the array, in reverse order if 'reversed' is true. function forEach(array, f, state, reversed) { if (state === void 0) { state = (0, common_1.TraverseState)(); } if (reversed === void 0) { reversed = false; } if (state.halted) return; var halt = state.halt; if (reversed) { var i = array.length; while (!state.halted && --i >= 0) { f(array[i], state.nextIndex(), halt); } } else { var length = array.length; var i = -1; while (!state.halted && ++i < length) { f(array[i], state.nextIndex(), halt); } } } exports.forEach = forEach; // Returns a copy of the array where given function is applied to each element function map(array, f, indexOffset) { if (indexOffset === void 0) { indexOffset = 0; } if (indexOffset === 0) { // without offset, can use standard array map return array.map(f); } var result = []; var index = indexOffset; var i = -1; var length = array.length; while (++i < length) { result[i] = f(array[i], index++); } return result; } exports.map = map; // Returns a copy of the array where given functio is applied to each element in reverse order function reverseMap(array, f, indexOffset) { if (indexOffset === void 0) { indexOffset = 0; } var result = []; var index = indexOffset; var arrayIndex = array.length; var resultIndex = 0; while (--arrayIndex >= 0) result[resultIndex++] = f(array[arrayIndex], index++); return result; } exports.reverseMap = reverseMap; function _prependNew(array, value) { return array.toSpliced(0, 0, value); } exports._prependNew = _prependNew; function _prependOld(array, value) { var clone = array.slice(); clone.unshift(value); return clone; } exports._prependOld = _prependOld; // Returns a copy of the given array with the given value added at the start exports.prepend = "toSpliced" in Array.prototype ? _prependNew : _prependOld; function _lastNew(arr) { return arr.at(-1); } exports._lastNew = _lastNew; function _lastOld(arr) { return arr[arr.length - 1]; } exports._lastOld = _lastOld; // Returns the last element of the array exports.last = "at" in Array.prototype ? _lastNew : _lastOld; function _updateNew(arr, index, updater) { if (index < 0 || index >= arr.length) { return arr; } var curValue = arr[index]; var newValue = (0, common_1.Update)(curValue, updater); if (Object.is(newValue, curValue)) { return arr; } return arr.with(index, newValue); } exports._updateNew = _updateNew; function _updateOld(arr, index, updater) { if (index < 0 || index >= arr.length) { return arr; } var curValue = arr[index]; var newValue = (0, common_1.Update)(curValue, updater); if (Object.is(newValue, curValue)) { return arr; } var newArr = arr.slice(); newArr[index] = newValue; return newArr; } exports._updateOld = _updateOld; // Returns a copy of the array where the element at given index is replaced by the given updater. // If the new element is the same as the old element, the original array is returned exports.update = "with" in Array.prototype ? _updateNew : _updateOld; function _modNew(arr, index, f) { if (index < 0 || index >= arr.length) { return arr; } var curValue = arr[index]; var newValue = f(curValue); if (Object.is(newValue, curValue)) { return arr; } return arr.with(index, newValue); } exports._modNew = _modNew; function _modOld(arr, index, f) { if (index < 0 || index >= arr.length) { return arr; } var curValue = arr[index]; var newValue = f(curValue); if (Object.is(newValue, curValue)) { return arr; } var newArr = arr.slice(); newArr[index] = newValue; return newArr; } exports._modOld = _modOld; // Returns a copy of the array where the element at given index is replaced by applying given function. // If the new element is the same as the old element, the original array is returned exports.mod = "with" in Array.prototype ? _modNew : _modOld; function _insertNew(arr, index, value) { return arr.toSpliced(index, 0, value); } exports._insertNew = _insertNew; function _insertOld(arr, index, value) { var clone = arr.slice(); clone.splice(index, 0, value); return clone; } exports._insertOld = _insertOld; // Returns a copy of the array where at given index the given value is inserted exports.insert = "toSpliced" in Array.prototype ? _insertNew : _insertOld; // Returns a copy of the array, without its first element function tail(arr) { return arr.slice(1); } exports.tail = tail; // Returns a copy of the array, without its last element function init(arr) { return arr.slice(0, arr.length - 1); } exports.init = init; function _spliceNew(arr, start, deleteCount) { var items = []; for (var _i = 3; _i < arguments.length; _i++) { items[_i - 3] = arguments[_i]; } return arr.toSpliced.apply(arr, tslib_1.__spreadArray([start, deleteCount], tslib_1.__read(items), false)); } exports._spliceNew = _spliceNew; function _spliceOld(arr, start, deleteCount) { var items = []; for (var _i = 3; _i < arguments.length; _i++) { items[_i - 3] = arguments[_i]; } var clone = arr.slice(); clone.splice.apply(clone, tslib_1.__spreadArray([start, deleteCount], tslib_1.__read(items), false)); return clone; } exports._spliceOld = _spliceOld; // Immutable version of the array .splice command, always returns a new array exports.splice = "toSpliced" in Array.prototype ? _spliceNew : _spliceOld; // Returns a copy of the array, where its 'sparse' property is kept (sparse = not all indices have a value) function copySparse(arr) { var clone = []; for (var key in arr) { clone[key] = arr[key]; } return clone; } exports.copySparse = copySparse; // Returns a copy of the array with given function applied to each element, where its 'sparse' property is kept // (sparse = not all indices have a value) function mapSparse(arr, f) { var result = Array(arr.length); for (var key in arr) { result[key] = f(arr[key], key); } return result; } exports.mapSparse = mapSparse; //# sourceMappingURL=arr.cjs.map