all files / montage/core/shim/ array.js

8.82% Statements 3/34
4.17% Branches 1/24
0% Functions 0/3
9.38% Lines 3/32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                                                                                                                                                                                                                                                     
/**
 * Defines standardized shims to intrinsic `Array` object.
 * @see {external:Array}
 * @module montage/core/shim/array
 */
 
/**
 * @external Array
 */
 
/**
 * Returns whether the given value is an array, regardless of which
 * context it comes from.  The context may be another frame.
 *
 * This is the proper idiomatic way to test whether an object is an
 * array and replaces the less generally useful `instanceof`
 * check (which does not work across contexts) and the strangeness that
 * the `typeof` an array is `"object"`.
 *
 * @function external:Array.isArray
 * @param {Any} value any value
 * @returns {boolean} whether the given value is an array
 */
Iif (!Array.isArray) {
    Object.defineProperty(Array, "isArray", {
        value: function (obj) {
            return Object.prototype.toString.call(obj) === "[object Array]";
        },
        writable: true,
        configurable: true
    });
}
 
/**
 *  Compacts a sparse array.
 *
 *  @function external:Date#clone
 *  @returns {Date} - a new date
*/
Object.defineProperty(Array.prototype,"compact", {
    value: function () {
        var u, i=-1, j=-1,length=this.length;
        while(++j<length)
            if(!(this[j]===u))
                this[++i]=this[j];
        this.length=++i;
    },
    writable: true,
    configurable: true
 
});
 
/**
 *  Remove Objects.
 *
 *  @function external:Date#clone
 *  @returns {Date} - a new date
*/
Object.defineProperty(Array.prototype,"removeObjects", {
 
    value: function (objects, clearRemovedObjects) {
        var u, i=-1, j=-1,length=this.length, hasMethod, index, indexCount = 0;
        clearRemovedObjects = arguments.length === 2 ? clearRemovedObjects : true;
        if(typeof objects.has === "function") {
            while(++j<length) {
                if(!(objects.has(this[j]))) {
                    this[++i]=this[j];
                }
                else if(clearRemovedObjects) objects.delete(this[j]);
            }
        }
        else if(typeof objects.hasOwnProperty === "function") {
            while(++j<length) {
                if(!(objects.hasOwnProperty(this[j]))) {
                    this[++i]=this[j];
                }
                else if(clearRemovedObjects) delete objects[this[j]];
            }
        }
        else if(typeof objects.indexOf === "function") {
            while(++j<length) {
                if((index = objects.indexOf(this[j])) === -1) {
                    this[++i]=this[j];
                }
                else {
                    objects[index] = u;
                    indexCount++
                }
            }
            if(objects.length === indexCount) {
                objects.length = 0;
            }
            else objects.compact();
        }
        this.length=++i;
    },
    writable: true,
    configurable: true
});
 
/*
var array = ["a","b",,"c",,"d","e","f",,,,,"g",,,];
array.compact();
console.log(array);
var array2 = ["a","b","c","d","e","f","g"];
var map = new Object();
map.b = true;
map.c = true;
array2.removeObjects(map);
console.log(array2);
 
var a = {"a":true};
var b = {"b":true};
var c = {"c":true};
var d = {"d":true};
var e = {"e":true};
 
var array3 = [a,b,c,d,e];
var map = new Map();
map.set(b,true);
map.set(d,true);
 
array3.removeObjects(map);
console.log(array3);
*/