index.js 669 B

12345678910111213141516171819202122232425262728293031
  1. /*!
  2. * object.reduce <https://github.com/jonschlinkert/object.reduce>
  3. *
  4. * Copyright (c) 2014-2015, 2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. var makeIterator = require('make-iterator');
  9. var forOwn = require('for-own');
  10. module.exports = function reduce(target, fn, acc, thisArg) {
  11. var first = arguments.length > 2;
  12. if (target && !Object.keys(target).length && !first) {
  13. return null;
  14. }
  15. var iterator = makeIterator(fn, thisArg);
  16. forOwn(target, function(value, key, orig) {
  17. if (!first) {
  18. acc = value;
  19. first = true;
  20. } else {
  21. acc = iterator(acc, value, key, orig);
  22. }
  23. });
  24. return acc;
  25. };