UNPKG

1.71 kBJavaScriptView Raw
1var _isArrayLike =
2/*#__PURE__*/
3require("./_isArrayLike");
4
5var _xwrap =
6/*#__PURE__*/
7require("./_xwrap");
8
9var bind =
10/*#__PURE__*/
11require("../bind");
12
13function _arrayReduce(xf, acc, list) {
14 var idx = 0;
15 var len = list.length;
16
17 while (idx < len) {
18 acc = xf['@@transducer/step'](acc, list[idx]);
19
20 if (acc && acc['@@transducer/reduced']) {
21 acc = acc['@@transducer/value'];
22 break;
23 }
24
25 idx += 1;
26 }
27
28 return xf['@@transducer/result'](acc);
29}
30
31function _iterableReduce(xf, acc, iter) {
32 var step = iter.next();
33
34 while (!step.done) {
35 acc = xf['@@transducer/step'](acc, step.value);
36
37 if (acc && acc['@@transducer/reduced']) {
38 acc = acc['@@transducer/value'];
39 break;
40 }
41
42 step = iter.next();
43 }
44
45 return xf['@@transducer/result'](acc);
46}
47
48function _methodReduce(xf, acc, obj, methodName) {
49 return xf['@@transducer/result'](obj[methodName](bind(xf['@@transducer/step'], xf), acc));
50}
51
52var symIterator = typeof Symbol !== 'undefined' ? Symbol.iterator : '@@iterator';
53
54function _reduce(fn, acc, list) {
55 if (typeof fn === 'function') {
56 fn = _xwrap(fn);
57 }
58
59 if (_isArrayLike(list)) {
60 return _arrayReduce(fn, acc, list);
61 }
62
63 if (typeof list['fantasy-land/reduce'] === 'function') {
64 return _methodReduce(fn, acc, list, 'fantasy-land/reduce');
65 }
66
67 if (list[symIterator] != null) {
68 return _iterableReduce(fn, acc, list[symIterator]());
69 }
70
71 if (typeof list.next === 'function') {
72 return _iterableReduce(fn, acc, list);
73 }
74
75 if (typeof list.reduce === 'function') {
76 return _methodReduce(fn, acc, list, 'reduce');
77 }
78
79 throw new TypeError('reduce: list must be array or iterable');
80}
81
82module.exports = _reduce;
\No newline at end of file