Code coverage report for object/forIn.js

Statements: 82.61% (19 / 23)      Branches: 50% (5 / 10)      Functions: 100% (5 / 5)      Lines: 80.95% (17 / 21)     

All files » object/ » forIn.js
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 631   1     1 1                   1   1 1                 1 190         190   190 429 24       190                         1 429     1      
define(function () {
 
    var _hasDontEnumBug,
        _dontEnums;
 
    function checkDontEnum(){
        _dontEnums = [
                'toString',
                'toLocaleString',
                'valueOf',
                'hasOwnProperty',
                'isPrototypeOf',
                'propertyIsEnumerable',
                'constructor'
            ];
 
        _hasDontEnumBug = true;
 
        for (var key in {'toString': null}) {
            _hasDontEnumBug = false;
        }
    }
 
    /**
     * Similar to Array/forEach but works over object properties and fixes Don't
     * Enum bug on IE.
     * based on: http://whattheheadsaid.com/2010/10/a-safer-object-keys-compatibility-implementation
     */
    function forIn(obj, fn, thisObj){
        var key, i = 0;
        // no need to check if argument is a real object that way we can use
        // it for arrays, functions, date, etc.
 
        //post-pone check till needed
        if (_hasDontEnumBug == null) checkDontEnum();
 
        for (key in obj) {
            if (exec(fn, obj, key, thisObj) === false) {
                break;
            }
        }
 
        Iif (_hasDontEnumBug) {
            while (key = _dontEnums[i++]) {
                // since we aren't using hasOwn check we need to make sure the
                // property was overwritten
                if (obj[key] !== Object.prototype[key]) {
                    if (exec(fn, obj, key, thisObj) === false) {
                        break;
                    }
                }
            }
        }
    }
 
    function exec(fn, obj, key, thisObj){
        return fn.call(thisObj, obj[key], key, obj);
    }
 
    return forIn;
 
});