[d24f17c] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | exports.__esModule = true;
|
---|
| 4 | exports["default"] = void 0;
|
---|
| 5 | var _ramda = require("ramda");
|
---|
| 6 | var _isUndefined = _interopRequireDefault(require("./isUndefined"));
|
---|
| 7 | var _resolveP = _interopRequireDefault(require("./resolveP"));
|
---|
| 8 | var _allP = _interopRequireDefault(require("./allP"));
|
---|
| 9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
---|
| 10 | function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
---|
| 11 | function _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."); }
|
---|
| 12 | function _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); }
|
---|
| 13 | function _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; }
|
---|
| 14 | function _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; } }
|
---|
| 15 | function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
---|
| 16 | /* eslint-disable max-len */
|
---|
| 17 | /**
|
---|
| 18 | * Given an `Iterable`(arrays are `Iterable`), or a promise of an `Iterable`,
|
---|
| 19 | * which produces promises (or a mix of promises and values),
|
---|
| 20 | * iterate over all the values in the `Iterable` into an array and
|
---|
| 21 | * reduce the array to a value using the given iterator function.
|
---|
| 22 | *
|
---|
| 23 | * If the iterator function returns a promise, then the result of the promise is awaited,
|
---|
| 24 | * before continuing with next iteration. If any promise in the array is rejected or a promise
|
---|
| 25 | * returned by the iterator function is rejected, the result is rejected as well.
|
---|
| 26 | *
|
---|
| 27 | * If `initialValue` is `undefined` (or a promise that resolves to `undefined`) and
|
---|
| 28 | * the `Iterable` contains only 1 item, the callback will not be called and
|
---|
| 29 | * the `Iterable's` single item is returned. If the `Iterable` is empty, the callback
|
---|
| 30 | * will not be called and `initialValue` is returned (which may be undefined).
|
---|
| 31 | *
|
---|
| 32 | * This function is basically equivalent to {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce}.
|
---|
| 33 | *
|
---|
| 34 | * @func reduceP
|
---|
| 35 | * @memberOf RA
|
---|
| 36 | * @since {@link https://char0n.github.io/ramda-adjunct/1.13.0|v1.13.0}
|
---|
| 37 | * @category List
|
---|
| 38 | * @typedef MaybePromise = Promise.<*> | *
|
---|
| 39 | * @sig ((Promise a, MaybePromise b) -> Promise a) -> MaybePromise a -> MaybePromise [MaybePromise b] -> Promise a
|
---|
| 40 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the current element from the list
|
---|
| 41 | * @param {*|Promise.<*>} acc The accumulator value
|
---|
| 42 | * @param {Array.<*>|Promise.<Array<*|Promise.<*>>>} list The list to iterate over
|
---|
| 43 | * @return {Promise} The final, accumulated value
|
---|
| 44 | * @see {@link http://ramdajs.com/docs/#reduce|R.reduce}, {@link RA.reduceRightP|reduceRightP}, {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce}
|
---|
| 45 | * @example
|
---|
| 46 | *
|
---|
| 47 | * RA.reduceP(
|
---|
| 48 | * (total, fileName) => fs
|
---|
| 49 | * .readFileAsync(fileName, 'utf8')
|
---|
| 50 | * .then(contents => total + parseInt(contents, 10)),
|
---|
| 51 | * 0,
|
---|
| 52 | * ['file1.txt', 'file2.txt', 'file3.txt']
|
---|
| 53 | * ); // => Promise(10)
|
---|
| 54 | *
|
---|
| 55 | * RA.reduceP(
|
---|
| 56 | * (total, fileName) => fs
|
---|
| 57 | * .readFileAsync(fileName, 'utf8')
|
---|
| 58 | * .then(contents => total + parseInt(contents, 10)),
|
---|
| 59 | * Promise.resolve(0),
|
---|
| 60 | * ['file1.txt', 'file2.txt', 'file3.txt']
|
---|
| 61 | * ); // => Promise(10)
|
---|
| 62 | *
|
---|
| 63 | * RA.reduceP(
|
---|
| 64 | * (total, fileName) => fs
|
---|
| 65 | * .readFileAsync(fileName, 'utf8')
|
---|
| 66 | * .then(contents => total + parseInt(contents, 10)),
|
---|
| 67 | * 0,
|
---|
| 68 | * [Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt']
|
---|
| 69 | * ); // => Promise(10)
|
---|
| 70 | *
|
---|
| 71 | * RA.reduceP(
|
---|
| 72 | * (total, fileName) => fs
|
---|
| 73 | * .readFileAsync(fileName, 'utf8')
|
---|
| 74 | * .then(contents => total + parseInt(contents, 10)),
|
---|
| 75 | * 0,
|
---|
| 76 | * Promise.resolve([Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt'])
|
---|
| 77 | * ); // => Promise(10)
|
---|
| 78 | *
|
---|
| 79 | */
|
---|
| 80 | /* esline-enable max-len */
|
---|
| 81 | var reduceP = (0, _ramda.curryN)(3, function (fn, acc, list) {
|
---|
| 82 | return (0, _resolveP["default"])(list).then(function (iterable) {
|
---|
| 83 | var listLength = (0, _ramda.length)(iterable);
|
---|
| 84 | if (listLength === 0) {
|
---|
| 85 | return acc;
|
---|
| 86 | }
|
---|
| 87 | var reducer = (0, _ramda.reduce)(function (accP, currentValueP) {
|
---|
| 88 | return accP.then(function (previousValue) {
|
---|
| 89 | return (0, _allP["default"])([previousValue, currentValueP]);
|
---|
| 90 | }).then(function (_ref) {
|
---|
| 91 | var _ref2 = _slicedToArray(_ref, 2),
|
---|
| 92 | previousValue = _ref2[0],
|
---|
| 93 | currentValue = _ref2[1];
|
---|
| 94 | if ((0, _isUndefined["default"])(previousValue) && listLength === 1) {
|
---|
| 95 | return currentValue;
|
---|
| 96 | }
|
---|
| 97 | return fn(previousValue, currentValue);
|
---|
| 98 | });
|
---|
| 99 | });
|
---|
| 100 | return reducer((0, _resolveP["default"])(acc), iterable);
|
---|
| 101 | });
|
---|
| 102 | });
|
---|
| 103 | var _default = reduceP;
|
---|
| 104 | exports["default"] = _default; |
---|