reduce.js 654 B

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