source: node_modules/ramda-adjunct/es/reduceP.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[d24f17c]1function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
5function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
6function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7import { curryN, reduce, length } from 'ramda';
8import isUndefined from './isUndefined';
9import resolveP from './resolveP';
10import allP from './allP';
11
12/* eslint-disable max-len */
13/**
14 * Given an `Iterable`(arrays are `Iterable`), or a promise of an `Iterable`,
15 * which produces promises (or a mix of promises and values),
16 * iterate over all the values in the `Iterable` into an array and
17 * reduce the array to a value using the given iterator function.
18 *
19 * If the iterator function returns a promise, then the result of the promise is awaited,
20 * before continuing with next iteration. If any promise in the array is rejected or a promise
21 * returned by the iterator function is rejected, the result is rejected as well.
22 *
23 * If `initialValue` is `undefined` (or a promise that resolves to `undefined`) and
24 * the `Iterable` contains only 1 item, the callback will not be called and
25 * the `Iterable's` single item is returned. If the `Iterable` is empty, the callback
26 * will not be called and `initialValue` is returned (which may be undefined).
27 *
28 * This function is basically equivalent to {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce}.
29 *
30 * @func reduceP
31 * @memberOf RA
32 * @since {@link https://char0n.github.io/ramda-adjunct/1.13.0|v1.13.0}
33 * @category List
34 * @typedef MaybePromise = Promise.<*> | *
35 * @sig ((Promise a, MaybePromise b) -> Promise a) -> MaybePromise a -> MaybePromise [MaybePromise b] -> Promise a
36 * @param {Function} fn The iterator function. Receives two values, the accumulator and the current element from the list
37 * @param {*|Promise.<*>} acc The accumulator value
38 * @param {Array.<*>|Promise.<Array<*|Promise.<*>>>} list The list to iterate over
39 * @return {Promise} The final, accumulated value
40 * @see {@link http://ramdajs.com/docs/#reduce|R.reduce}, {@link RA.reduceRightP|reduceRightP}, {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce}
41 * @example
42 *
43 * RA.reduceP(
44 * (total, fileName) => fs
45 * .readFileAsync(fileName, 'utf8')
46 * .then(contents => total + parseInt(contents, 10)),
47 * 0,
48 * ['file1.txt', 'file2.txt', 'file3.txt']
49 * ); // => Promise(10)
50 *
51 * RA.reduceP(
52 * (total, fileName) => fs
53 * .readFileAsync(fileName, 'utf8')
54 * .then(contents => total + parseInt(contents, 10)),
55 * Promise.resolve(0),
56 * ['file1.txt', 'file2.txt', 'file3.txt']
57 * ); // => Promise(10)
58 *
59 * RA.reduceP(
60 * (total, fileName) => fs
61 * .readFileAsync(fileName, 'utf8')
62 * .then(contents => total + parseInt(contents, 10)),
63 * 0,
64 * [Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt']
65 * ); // => Promise(10)
66 *
67 * RA.reduceP(
68 * (total, fileName) => fs
69 * .readFileAsync(fileName, 'utf8')
70 * .then(contents => total + parseInt(contents, 10)),
71 * 0,
72 * Promise.resolve([Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt'])
73 * ); // => Promise(10)
74 *
75 */
76/* esline-enable max-len */
77var reduceP = curryN(3, function (fn, acc, list) {
78 return resolveP(list).then(function (iterable) {
79 var listLength = length(iterable);
80 if (listLength === 0) {
81 return acc;
82 }
83 var reducer = reduce(function (accP, currentValueP) {
84 return accP.then(function (previousValue) {
85 return allP([previousValue, currentValueP]);
86 }).then(function (_ref) {
87 var _ref2 = _slicedToArray(_ref, 2),
88 previousValue = _ref2[0],
89 currentValue = _ref2[1];
90 if (isUndefined(previousValue) && listLength === 1) {
91 return currentValue;
92 }
93 return fn(previousValue, currentValue);
94 });
95 });
96 return reducer(resolveP(acc), iterable);
97 });
98});
99export default reduceP;
Note: See TracBrowser for help on using the repository browser.