UNPKG

654 BJavaScriptView Raw
1var forOwn = require('./forOwn');
2var size = require('./size');
3
4 /**
5 * Object reduce
6 */
7 function reduce(obj, callback, memo, thisObj) {
8 var initial = arguments.length > 2;
9
10 if (!size(obj) && !initial) {
11 throw new Error('reduce of empty object with no initial value');
12 }
13
14 forOwn(obj, function(value, key, list) {
15 if (!initial) {
16 memo = value;
17 initial = true;
18 }
19 else {
20 memo = callback.call(thisObj, memo, value, key, list);
21 }
22 });
23
24 return memo;
25 }
26
27 module.exports = reduce;
28
29