was.js – Rev 12

Subversion Repositories:
Rev:
/*! was - v1.0.0 - 2017-05-29 
* http://grimore.org
* Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */
/*    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
if (!Array.prototype.product) {
    Array.prototype.product = function(b) {
        var a = this;
        return $.map(
            new Array(Math.max(this.length, a.length)),
            function(e, i) {
                var o = {};
                o[a[i]] = b[i];
                return o;
            });
    };
}
$.extend({
    product: function(a, b) {
        return $.map(
            new Array(Math.max(this.length, a.length)),
            function(e, i) {
                var o = {};
                o[a[i]] = b[i];
                return o;
            });
    }
});

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

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

/*************************************************************************/
/*    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
/*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
/*************************************************************************/
if (!Array.prototype.equals) {
    // attach the .equals method to Array's prototype to call it on any array
    Array.prototype.equals = function(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;
    };
}

$.extend({
    equals: function(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;
    }
});

/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
function wasCSVToArray(csv) {
    var l = [];
    var s = [];
    var m = "";
 
    do {
        var a = csv.charAt(0);
        csv = csv.slice(1, csv.length);
        if(a === ",") {
            if(s[s.length-1] !== '"') {
                l.push(m);
                m = "";
                continue;
            }
            m += a;
            continue;
        }
        if(a === '"' && csv.charAt(0) === a) {
            m += a;
            csv = csv.slice(1, csv.length);
            continue;
        }
        if(a === '"') {
            if(s[s.length-1] !== a) {
                s.push(a);
                continue;
            }
            s.pop();
            continue;
        }
        m += a;
    } while(csv !== "");
 
    l.push(m);
 
    return l;
}

/*************************************************************************/
/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
function wasArrayToCSV(a) {
    var csv = [];
    for(var i=0; i<a.length; ++i) {
        var cell = a[i].toString().replace('"', '""');
        if(/"\s,\r\n/.test(cell)) {
            csv[i] = '"' + cell + '"';
            continue;
        }
        csv[i] = cell;
    }
    return csv.join();
}

/*    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
function wasKeyValueObjectify(a) {
    var o = {};
    a.reduce(function(a, c, i) {
        i = Math.floor(i / 2);
        if (!a[i]) {
            a[i] = [];
        }
        a[i].push(c);
        return a;
    }, []).forEach(function(c, i, a) {
        o[c[0]] = c[1];
    }, o);
    return o;
}

/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
function wasMapValueToRange(value, xMin, xMax, yMin, yMax) {
    return yMin + (
        ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin )
    );
}

/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
function wasHexToRGB(hex) {
    var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
    hex = hex.replace(
        shortRegEx,
        function(m, r, g, b) {
            return r + r + g + g + b + b;
        }
    );

    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}

/*************************************************************************/
/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/
function wasRGBToHex(r, g, b) {
    return "#" + (
        (1 << 24) +
        (r << 16) +
        (g << 8) +
        b
    ).toString(16).slice(1);
}