diffusion
Version:
Diffusion JavaScript client
71 lines (61 loc) • 1.59 kB
JavaScript
/*eslint valid-jsdoc: "off"*/
/**
* Remove an element from an array.
*
* @param {Array} - The array
* @param {*} - The object to remove
* @returns {Boolean} If the element was present in the array
*/
function remove(arr, e) {
var i = arr.indexOf(e);
if (i > -1) {
arr.splice(i, 1);
return true;
}
return false;
}
/**
* Fill an array with a given value, within an optional range
*
* @param array {Array} - The array to fill
* @param value {*} - The value to fill the array with
* @param start {Number} [0] - Optional range start
* @param end {Number} [array.length] - Optional range end
*/
function fill(array, value, start, end) {
start = start || 0;
end = end || array.length;
for (var i = start; i < end; ++i) {
array[i] = value;
}
}
/**
* Create an array of a given size, optionally pre-filled with a specified initial value.
*
* @param size {Number} - The size of the array to create
* @param initialValue {*} [] - The initial value to assign to each element
*/
function ofSize(size, initialValue) {
var a = [];
a.length = size;
if (initialValue !== undefined) {
fill(a, initialValue);
}
return a;
}
var s = Array.prototype.slice;
/**
* Convert an Arguments object to an array.
*
* @param {Arguments} args - The arguments to convert
* @returns {Array} The argument in an Array.
*/
function argumentsToArray(args) {
return s.call(args, 0);
}
module.exports = {
fill : fill,
ofSize : ofSize,
remove : remove,
argumentsToArray : argumentsToArray
};