was.js – Rev 25

Subversion Repositories:
Rev:
/*************************************************************************/
/*    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
function wasProduct(b) {
    var a = this;
    var m = Math.max(this.length, a.length);
    var o = {};
    for(var i = 0; i < m; ++i) {
        o[a[i]] = b[i];
    }
    return o;
}
if (!Array.prototype.product) {
    Array.prototype.product = wasProduct;
}
// jQuery
if(typeof jQuery === 'function') {
    $.extend({
        product: wasProduct
    });
}

/*************************************************************************/
/*    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
function wasStride(s) {
    return this.filter(function(e, i) {
        return i % s === 0;
    });
}
if (!Array.prototype.stride) {
    Array.prototype.stride = wasStride;
}
// jQuery
if(typeof jQuery === 'function') {
    $.extend({
        stride: wasStride
    });
}

/*************************************************************************/
/*    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
// Vanilla JavaScript
function wasChunk(n) {
    if (!this.length) {
        return [];
    }
    return [this.slice(0, n)]
        .concat(this.slice(n).wasChunk(n));
}
if (!Array.prototype.chunk) {
    Array.prototype.chunk = wasChunk;
}
// jQuery
if(typeof jQuery === 'function') {
    $.extend({
        chunk: wasChunk
    });
}

/*************************************************************************/
/*    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
/*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
/*************************************************************************/
// Vanilla JavaScript
function wasEquals(a) {
    // if the other array is a falsy value, return
    if (!a) {
        return false;
    }

    // compare lengths - can save a lot of time 
    if (this.length !== a.length) {
        return false;
    }

    for (var i = 0, l = this.length; i < l; i++) {
        // Check if we have nested arrays
        if (this[i] instanceof Array && a[i] instanceof Array) {
            // recurse into the nested arrays
            if (!this[i].equals(a[i])) {
                return false;
            }
        } else if (this[i] !== a[i]) {
            // Warning - two different object instances will never be equal: {x:20} != {x:20}
            return false;
        }
    }
    return true;
}
if (!Array.prototype.equals) {
    // attach the .equals method to Array's prototype to call it on any array
    Array.prototype.equals = wasEquals;
}
// jQuery
if(typeof jQuery === 'function') {
    $.extend({
        equals: wasEquals
    });
}

/*************************************************************************/
/* Node.JS package export.                                               */
/*************************************************************************/
module.exports.collections.arrays = {
    stride: wasProduct,
    stride: wasStride,
    chunk: wasChunk,
    equals: wasEquals
};