Wizardry and Steamworks JavaScript Libraries

Name Link Description
was.js was.js General Purpose Library

Compare Arrays

The following code:

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

will add an .equals call to arrays so you can use it as:

var a = [ "a", "b" ];
var b = [ "a", "c" ];
alert(a.equals(b));

Linearly Map a Value in a Range into another Range

Following the formula to linearly map a value in a range to another range, the javascript variation thereof can be found here.

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

RGB Color to Hexadecimal Color

function wasRGBToHex(r, g, b) {
    return "#" + (
        (1 << 24) + 
        (r << 16) + 
        (g << 8) + 
        b
    ).toString(16).slice(1);
}

where r, g and b are in the range $[0-255]$.

Hexadecimal Color to RGB Color

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

where hex can be in long or short CSS notation.


fuss/javascript.txt ยท Last modified: 2017/05/28 19:56 by office

Access website using Tor Access website using i2p


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy and plagiarism pages.