proto

arrayMethods

declaration
 arrayMethods 

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
};

nativeArrayMethodsNames

declaration
 nativeArrayMethodsNames 

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);

find

property
 arrayMethods.find 

Option name Type Description
self Array

array to search in

callback Function

should return true for item to pass the test, passed value, index and self as parameters

thisArg Object

optional context (this) of callback call

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');

findIndex

property
 arrayMethods.findIndex 

Option name Type Description
self Array

array to search in

callback Function

should return true for item to pass the test, passed value, index and self as parameters

thisArg Object

optional context (this) of callback call

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');

appendArray

function
 appendArray() 

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;
}

prependArray

function
 prependArray() 

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;
}

spliceItem

function
 spliceItem() 

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;
}

toArray

function
 toArray() 

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);
}

object

function
 object() 

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;
}

mapToObject

function
 mapToObject() 

Option name Type Description
self Array

An array which values will become keys of the result

callback Function

Callback is passed value, index and self and should return value that will be included in the result.

thisArg Object

An optional context of iteration (the valueof this), will be undefined if this parameter is not passed.

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;
}

unique

function
 unique() 

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);
    }
}

deepForEach

function
 deepForEach() 

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);
        });
    }
}