e36fd4aac736046f3171830bb3ade265f4d56ef5
[platform/framework/web/crosswalk-tizen.git] /
1
2
3     /**
4      * Array reduceRight
5      */
6     function reduceRight(arr, fn, initVal) {
7         // check for args.length since initVal might be "undefined" see #gh-57
8         var hasInit = arguments.length > 2;
9
10         if (arr == null || !arr.length) {
11             if (hasInit) {
12                 return initVal;
13             } else {
14                 throw new Error('reduce of empty array with no initial value');
15             }
16         }
17
18         var i = arr.length, result = initVal, value;
19         while (--i >= 0) {
20             // we iterate over sparse items since there is no way to make it
21             // work properly on IE 7-8. see #64
22             value = arr[i];
23             if (!hasInit) {
24                 result = value;
25                 hasInit = true;
26             } else {
27                 result = fn(result, value, i, arr);
28             }
29         }
30         return result;
31     }
32
33     module.exports = reduceRight;
34