wishthis/node_modules/object.reduce/index.js
2022-01-21 09:28:41 +01:00

31 lines
669 B
JavaScript

/*!
* object.reduce <https://github.com/jonschlinkert/object.reduce>
*
* Copyright (c) 2014-2015, 2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var makeIterator = require('make-iterator');
var forOwn = require('for-own');
module.exports = function reduce(target, fn, acc, thisArg) {
var first = arguments.length > 2;
if (target && !Object.keys(target).length && !first) {
return null;
}
var iterator = makeIterator(fn, thisArg);
forOwn(target, function(value, key, orig) {
if (!first) {
acc = value;
first = true;
} else {
acc = iterator(acc, value, key, orig);
}
});
return acc;
};