These methods can be chained.
var arrayMethods = module.exports = {
// find: see below
// findIndex: see below
appendArray: appendArray,
prependArray: prependArray,
toArray: toArray,
object: object,
mapToObject: mapToObject,
unique: unique,
deepForEach: deepForEach,
spliceItem: spliceItem
};
Functions that Array implements natively are also included for convenience - they can be used with array-like objects and for chaining (native functions are always called).
These methods can be chained too.
var nativeArrayMethodsNames = [ 'join', 'pop', 'push', 'concat',
'reverse', 'shift', 'unshift', 'slice', 'splice',
'sort', 'filter', 'forEach', 'some', 'every',
'map', 'indexOf', 'lastIndexOf', 'reduce', 'reduceRight'];
var nativeArrayMethods = mapToObject.call(nativeArrayMethodsNames,
function(methodName) {
return Array.prototype[methodName];
});
__.extend.call(arrayMethods, nativeArrayMethods);
Option name | Type | Description |
---|---|---|
self | Array | array to search in |
callback | Function | should return |
thisArg | Object | optional context ( |
return | Any |
Implementation of ES6 Array find method (native method is used if available).
Returns array element that passes callback test.
arrayMethods.find = Array.prototype.find
|| utils.makeFindMethod(arrayMethods.some, 'value');
Option name | Type | Description |
---|---|---|
self | Array | array to search in |
callback | Function | should return |
thisArg | Object | optional context ( |
return | Integer |
Implementation of ES6 Array findIndex method (native method is used if available).
Returns the index of array element that passes callback test. Returns -1
if not found.
arrayMethods.findIndex = Array.prototype.findIndex
|| utils.makeFindMethod(arrayMethods.some, 'index');
Option name | Type | Description |
---|---|---|
self | Array | An array that will be modified |
arrayToAppend | Array | An array that will be appended |
return | Array |
Appends arrayToAppend
to the end of array self
in place (can be an instance of Array or array-like object).
Changes the value of self
(it uses Array.prototype.splice
) and returns self
.
function appendArray(arrayToAppend) {
if (! arrayToAppend.length) return this;
if (! Array.isArray(arrayToAppend))
arrayToAppend = toArray.call(arrayToAppend);
var args = [this.length, 0].concat(arrayToAppend);
arrayMethods.splice.apply(this, args);
return this;
}
Option name | Type | Description |
---|---|---|
self | Array | An array that will be modified |
arrayToAppend | Array | An array that will be prepended |
return | Array |
Prepends arrayToPrepend
to the beginnig of array self
in place (can be an instance of Array or array-like object).
Changes the value of self
(it uses Array.prototype.splice
) and returns self
.
function prependArray(arrayToPrepend) {
if (! arrayToPrepend.length) return this;
if (! Array.isArray(arrayToPrepend))
arrayToPrepend = toArray.call(arrayToPrepend);
var args = [0, 0].concat(arrayToPrepend);
arrayMethods.splice.apply(this, args);
return this;
}
Option name | Type | Description |
---|---|---|
self | Array | An array that will be modified |
item | Any | item to be removed |
return | Array |
Removes item from array that is found using indexOf (i.e. '===')
Modifies original array and returns the reference to it.
function spliceItem(item) {
var index = this.indexOf(item);
if (index >= 0) this.splice(index, 1);
return this;
}
Option name | Type | Description |
---|---|---|
self | PseudoArray | Object with numeric property length |
return | Array |
Returns new array created from array-like object (e.g., arguments
pseudo-array).
function toArray() {
return arrayMethods.slice.call(this);
}
Option name | Type | Description |
---|---|---|
self | Array | Array of keys |
values | Array, any | Optional array of values or the value to be assigned to each property. |
return | Object |
Returns an object created from the array of keys
and optional array of values
.
function object(values) {
var obj = {}
, valuesIsArray = Array.isArray(values);
arrayMethods.forEach.call(this, function(key, index) {
obj[key] = valuesIsArray ? values[index] : values;
});
return obj;
}
Option name | Type | Description |
---|---|---|
self | Array | An array which values will become keys of the result |
callback | Function | Callback is passed |
thisArg | Object | An optional context of iteration (the valueof |
return | Object |
Maps array to object.
Array elements become keys, value are taken from callback
.
function mapToObject(callback, thisArg) {
var result = {};
Array.prototype.forEach.call(this, function(value, index) {
result[value] = callback.call(thisArg, value, index, this);
}, this);
return result;
}
Option name | Type | Description |
---|---|---|
self | Array | original array |
callback | Function | comparison function, should return true for equal items, "===" is used if not passed. |
return | Array |
Returns array without duplicates. Does not modify original array.
function unique(callback) {
var filtered = [];
if (! callback)
itemIndex = itemIndexOf;
this.forEach(function(item) {
var index = itemIndex(item);
if (index == -1)
filtered.push(item);
});
return filtered;
function itemIndex(item) {
return arrayMethods.findIndex.call(filtered, function(it) {
return callback(item, it);
});
}
function itemIndexOf(item) {
return filtered.indexOf(item);
}
}
Option name | Type | Description |
---|---|---|
self | Array | array of elements and arraysto iterate. |
callback | Function | called for each item that is not an array. Callback is passed item, index and original array as parameters. |
thisArg | Any | optional callback envocation context |
Iterates array and elements that are arrays calling callback with each element that is not an array. Can be used to iterate over arguments list to avoid checking whether array or list of parameters is passed.
function deepForEach(callback, thisArg) {
var index = 0, arr = this;
_deepForEach.call(this);
function _deepForEach() {
arrayMethods.forEach.call(this, function(value) {
if (Array.isArray(value))
_deepForEach.call(value);
else
callback.call(thisArg, value, index++, arr);
});
}
}