138 lines
4.9 KiB
JavaScript
138 lines
4.9 KiB
JavaScript
define(["require", "exports"], function (require, exports) {
|
|
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.arraysEqual = exports.flatten = exports.addElementAtIndex = exports.replaceElement = exports.removeIndex = exports.toMatrix = exports.createArray = exports.find = exports.findIndex = void 0;
|
|
/**
|
|
* Helper to find the index of an item within an array, using a callback to
|
|
* determine the match.
|
|
*
|
|
* @public
|
|
* @param array - Array to search.
|
|
* @param cb - Callback which returns true on matches.
|
|
* @param fromIndex - Optional index to start from (defaults to 0)
|
|
*/
|
|
function findIndex(array, cb, fromIndex) {
|
|
if (fromIndex === void 0) { fromIndex = 0; }
|
|
var index = -1;
|
|
for (var i = fromIndex; array && i < array.length; i++) {
|
|
if (cb(array[i], i)) {
|
|
index = i;
|
|
break;
|
|
}
|
|
}
|
|
return index;
|
|
}
|
|
exports.findIndex = findIndex;
|
|
/**
|
|
* Helper to find the first item within an array that satisfies the callback.
|
|
* @param array - Array to search
|
|
* @param cb - Callback which returns true on matches
|
|
*/
|
|
function find(array, cb) {
|
|
var index = findIndex(array, cb);
|
|
if (index < 0) {
|
|
return undefined;
|
|
}
|
|
return array[index];
|
|
}
|
|
exports.find = find;
|
|
/**
|
|
* Creates an array of a given size and helper method to populate.
|
|
*
|
|
* @public
|
|
* @param size - Size of array.
|
|
* @param getItem - Callback to populate given cell index.
|
|
*/
|
|
function createArray(size, getItem) {
|
|
var array = [];
|
|
for (var i = 0; i < size; i++) {
|
|
array.push(getItem(i));
|
|
}
|
|
return array;
|
|
}
|
|
exports.createArray = createArray;
|
|
/**
|
|
* Convert the given array to a matrix with columnCount number
|
|
* of columns.
|
|
*
|
|
* @public
|
|
* @param items - The array to convert
|
|
* @param columnCount - The number of columns for the resulting matrix
|
|
* @returns A matrix of items
|
|
*/
|
|
function toMatrix(items, columnCount) {
|
|
return items.reduce(function (rows, currentValue, index) {
|
|
if (index % columnCount === 0) {
|
|
rows.push([currentValue]);
|
|
}
|
|
else {
|
|
rows[rows.length - 1].push(currentValue);
|
|
}
|
|
return rows;
|
|
}, []);
|
|
}
|
|
exports.toMatrix = toMatrix;
|
|
/**
|
|
* Given an array, it returns a new array that does not contain the item at the given index.
|
|
* @param array - The array to operate on
|
|
* @param index - The index of the element to remove
|
|
*/
|
|
function removeIndex(array, index) {
|
|
return array.filter(function (_, i) { return index !== i; });
|
|
}
|
|
exports.removeIndex = removeIndex;
|
|
/**
|
|
* Given an array, this function returns a new array where the element at a given index has been replaced.
|
|
* @param array - The array to operate on
|
|
* @param newElement - The element that will be placed in the new array
|
|
* @param index - The index of the element that should be replaced
|
|
*/
|
|
function replaceElement(array, newElement, index) {
|
|
var copy = array.slice();
|
|
copy[index] = newElement;
|
|
return copy;
|
|
}
|
|
exports.replaceElement = replaceElement;
|
|
/**
|
|
* Given an array, this function returns a new array where an element has been inserted at the given index.
|
|
* @param array - The array to operate on
|
|
* @param index - The index where an element should be inserted
|
|
* @param itemToAdd - The element to insert
|
|
*/
|
|
function addElementAtIndex(array, index, itemToAdd) {
|
|
var copy = array.slice();
|
|
copy.splice(index, 0, itemToAdd);
|
|
return copy;
|
|
}
|
|
exports.addElementAtIndex = addElementAtIndex;
|
|
/**
|
|
* Given an array where each element is of type T or T[], flatten it into an array of T
|
|
* @param array - The array where each element can optionally also be an array
|
|
*/
|
|
function flatten(array) {
|
|
var result = [];
|
|
array.forEach(function (item) { return (result = result.concat(item)); });
|
|
return result;
|
|
}
|
|
exports.flatten = flatten;
|
|
/**
|
|
* Returns a boolean indicating if the two given arrays are equal in length and values.
|
|
*
|
|
* @param array1 - First array to compare
|
|
* @param array2 - Second array to compare
|
|
* @returns True if the arrays are the same length and have the same values in the same positions, false otherwise.
|
|
*/
|
|
function arraysEqual(array1, array2) {
|
|
if (array1.length !== array2.length) {
|
|
return false;
|
|
}
|
|
for (var i = 0; i < array1.length; i++) {
|
|
if (array1[i] !== array2[i]) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
exports.arraysEqual = arraysEqual;
|
|
});
|
|
//# sourceMappingURL=array.js.map
|