diffusion
Version:
Diffusion JavaScript client
57 lines (56 loc) • 1.53 kB
JavaScript
;
/**
* @module Util.Array
*
* @brief Utility functions for array manipulation
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ofSize = exports.fill = exports.remove = void 0;
/**
* Remove an element from an array.
*
* @param arr the array
* @param element the object to remove
* @param <T> the type of the elements stored in the array
* @returns `true` if the element was present in the array
*/
function remove(array, element) {
var i = array.indexOf(element);
if (i > -1) {
array.splice(i, 1);
return true;
}
return false;
}
exports.remove = remove;
/**
* Fill an array with a given value, within an optional range
*
* @param array the array to fill
* @param value the value to fill the array with
* @param start optional range start
* @param end optional range end
*/
function fill(array, value, start, end) {
if (start === void 0) { start = 0; }
if (end === void 0) { end = array.length; }
for (var i = start; i < end; ++i) {
array[i] = value;
}
}
exports.fill = fill;
/**
* Create an array of a given size, optionally pre-filled with a specified initial value.
*
* @param size the size of the array to create
* @param initialValue if specified, the initial will be assigned to each element
*/
function ofSize(size, initialValue) {
var a = [];
a.length = size;
if (initialValue !== undefined) {
fill(a, initialValue);
}
return a;
}
exports.ofSize = ofSize;