UNPKG

made-collections

Version:
153 lines 6.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.shuffle = exports.insertAtPotentialIndex = exports.potentialIndexOf = exports.chunk = exports.takeFromIndex = exports.areEquivalent = exports.removeRange = exports.addRange = exports.makeEqualTo = exports.update = void 0; /** * Updates an item within the array, if the item exists. * @param {T[]} array - The array to update the item in. * @param {T} item - The item to update. * @param {(item: T) => boolean} predicate - The function to find the item within the array. * @return {boolean} True if the item was updated; otherwise, false. */ function update(array, item, predicate) { const index = array.findIndex(predicate); if (index === -1) { return false; } array[index] = item; return true; } exports.update = update; /** * Makes the given destination array items equal to the items in the given source array. * @param {T[]} destination - The destination array to make equal to the source. * @param {T[]} source - The source array to provide the items. * @return {T[]} The updated destination array. */ function makeEqualTo(destination, source) { destination.length = 0; source.forEach(item => destination.push(item)); return destination; } exports.makeEqualTo = makeEqualTo; /** * Adds an array of items to another. * @param {T[]} destination - The destination array to add items from the source to. * @param {T[]} itemsToAdd - The source array items to add. * @return {T[]} The updated destination array. */ function addRange(destination, itemsToAdd) { itemsToAdd.forEach(item => destination.push(item)); return destination; } exports.addRange = addRange; /** * Removes an array of existing items from another. * @param {T[]} destination - The destination array to remove items from. * @param {T[]} itemsToAdd - The source array items to remove that should exist in the destination array. * @return {T[]} The updated destination array. */ function removeRange(destination, itemsToRemove) { itemsToRemove.forEach(item => destination.splice(destination.indexOf(item), 1)); return destination; } exports.removeRange = removeRange; /** * Determines whether two arrays are equivalent, containing all the same items with no regard to order. * @param {T[]} expected - The array of expected items. * @param {T[]} actual - The array of actual items. * @return {boolean} True if the arrays are equivalent; otherwise, false. */ function areEquivalent(expected, actual) { var result = (expected == null && actual == null) || (expected != null && actual != null && expected.length == actual.length); if (result) { expected.forEach(e => { if (!actual.includes(e)) { result = false; } }); } return result; } exports.areEquivalent = areEquivalent; /** * Takes a number of elements from the specified array from the specified starting index. * @param {T[]} array - The array of of items to take from. * @param {number} startIndex - The index to start at in the array. * @param {number} count - The number of items to take from the starting index. * @return {T[]} An array subset of the specified array. */ function takeFromIndex(array, startIndex, count) { const result = new Array(); for (let i = 0; i < count; i++) { if (startIndex + i < array.length) { result.push(array[startIndex + i]); } } return result; } exports.takeFromIndex = takeFromIndex; /** * Chunks an array of items into an array of arrays grouped into the specified chunk size. * @param {T[]} array - The array of items to chunk. * @param {number} size - The size of the chunks. * @return {T[][]} An array of arrays containing the chunked items. */ function chunk(array, size) { var result = new Array(); for (let i = 0, j = array.length; i < j; i += size) { result.push(array.slice(i, i + size)); } return result; } exports.chunk = chunk; /** * Gets the potential index of an item that does not currently exist within an array based on the specified criteria. * @param {T[]} array - The array of items to get the potential index from. * @param {T} value - The object to insert into the array. * @param {(value: T, item: T) => boolean} predicate - The function to determine the position of the item based on the provided value. * @return {number} The potential index of the item in the array. */ function potentialIndexOf(array, value, predicate) { let result = 0; for (let i = 0; i < array.length; i++) { if (predicate(value, array[i])) { result = i + 1; continue; } break; } return result; } exports.potentialIndexOf = potentialIndexOf; /** * Inserts an item to the specified array at the potential index determined by the predicate. * @param {T[]} array - The array of items the item should be inserted into. * @param {T} value - The object to insert into the array. * @param {(value: T, item: T) => boolean} predicate - The function to determine the position of the item based on the provided value. * @return {number} The inserted index of the item in the array. */ function insertAtPotentialIndex(array, value, predicate) { var potentialIndex = potentialIndexOf(array, value, predicate); if (potentialIndex !== -1) { array.splice(potentialIndex, 0, value); } return potentialIndex; } exports.insertAtPotentialIndex = insertAtPotentialIndex; /** * Creates a new array from the specified and shuffles the elements randomly. * @param {T[]} array - The array of items to shuffle. * @return {T[]} The new shuffled array of items. */ function shuffle(array) { const result = array.slice(); for (let i = result.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [result[i], result[j]] = [result[j], result[i]]; } return result; } exports.shuffle = shuffle; //# sourceMappingURL=index.js.map