forked from codemix/fast.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce.js
24 lines (22 loc) · 819 Bytes
/
reduce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'use strict';
var reduceArray = require('./array/reduce'),
reduceObject = require('./object/reduce');
/**
* # Reduce
*
* A fast `.reduce()` implementation.
*
* @param {Array|Object} subject The array or object to reduce over.
* @param {Function} fn The reducer function.
* @param {mixed} initialValue The initial value for the reducer, defaults to subject[0].
* @param {Object} thisContext The context for the reducer.
* @return {Array|Object} The array or object containing the results.
*/
module.exports = function fastReduce (subject, fn, initialValue, thisContext) {
if (subject instanceof Array) {
return reduceArray(subject, fn, initialValue, thisContext);
}
else {
return reduceObject(subject, fn, initialValue, thisContext);
}
};