no way to compare when less than two revisions

Differences

This shows you the differences between two versions of the page.


Previous revision
Last revision
fuss:javascript [2022/04/19 08:28] – external edit 127.0.0.1
Line 1: Line 1:
 +====== Wizardry and Steamworks JavaScript Libraries ======
 +
 +^ Name       ^ Link ^ Description ^
 +| ''was.js'' | [[javascript/bower/packages/was.js|was.js]] | General Purpose Library |
 +
 +====== Compare Arrays ======
 +
 +The following code:
 +
 +<code javascript>
 +// 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;
 +}
 +</code>
 +
 +will add an ''.equals'' call to arrays so you can use it as:
 +<code javascript>
 +var a = [ "a", "b" ];
 +var b = [ "a", "c" ];
 +alert(a.equals(b));
 +</code>
 +
 +====== Linearly Map a Value in a Range into another Range ======
 +
 +Following the formula to [[fuss/mathematics/algebra#linearly_map_a_value_in_a_range_into_another_range|linearly map a value in a range to another range]], the javascript variation thereof can be found here.
 +
 +<code javascript>
 +/*************************************************************************/
 +/*    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    */
 +/*************************************************************************/
 +function wasMapValueToRange(value, xMin, xMax, yMin, yMax) {
 +    return yMin + (
 +        ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin )
 +    );
 +}
 +</code>
 +
 +====== RGB Color to Hexadecimal Color ======
 +
 +<code javascript>
 +function wasRGBToHex(r, g, b) {
 +    return "#" + (
 +        (1 << 24) + 
 +        (r << 16) + 
 +        (g << 8) + 
 +        b
 +    ).toString(16).slice(1);
 +}
 +</code>
 +
 +where ''r'', ''g'' and ''b'' are in the range $[0-255]$.
 +
 +====== Hexadecimal Color to RGB Color ======
 +
 +<code javascript>
 +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;
 +}
 +</code>
 +
 +where ''hex'' can be in long or short CSS notation.
 +
  

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


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