Code coverage report for array/reduce.js

Statements: 100% (13 / 13)      Branches: 100% (6 / 6)      Functions: 100% (4 / 4)      Lines: 100% (12 / 12)     

All files » array/ » reduce.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 291         1   12     12 1     11 53 6 6   47       11     1    
define(['./forEach'], function (forEach) {
 
    /**
     * Array reduce
     */
    function reduce(arr, fn, initVal) {
        // check for args.length since initVal might be "undefined" see #gh-57
        var hasInit = arguments.length > 2,
            result = initVal;
 
        if (!arr.length && !hasInit) {
            throw new Error('reduce of empty array with no initial value');
        }
 
        forEach(arr, function (val, i, arr) {
            if (! hasInit) {
                result = val;
                hasInit = true;
            } else {
                result = fn(result, val, i, arr);
            }
        });
 
        return result;
    }
 
    return reduce;
});