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 | // in older ramda versions the order of the arguments is flipped
|
---|
17 | var flipArgs = (0, _ramda.pipe)((0, _ramda.reduceRight)(_ramda.concat, ''), (0, _ramda.equals)('ba'))(['a', 'b']);
|
---|
18 |
|
---|
19 | /* eslint-disable max-len */
|
---|
20 | /**
|
---|
21 | * Given an `Iterable`(arrays are `Iterable`), or a promise of an `Iterable`,
|
---|
22 | * which produces promises (or a mix of promises and values),
|
---|
23 | * iterate over all the values in the `Iterable` into an array and
|
---|
24 | * reduce the array to a value using the given iterator function.
|
---|
25 | *
|
---|
26 | * Similar to {@link RA.reduceP|reduceP} except moves through the input list from the right to the left.
|
---|
27 | * The iterator function receives two values: (value, acc),
|
---|
28 | * while the arguments' order of reduceP's iterator function is (acc, value).
|
---|
29 | *
|
---|
30 | * @func reduceRightP
|
---|
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 ((MaybePromise b, Promise a) -> Promise a) -> MaybePromise a -> MaybePromise [MaybePromise b] -> Promise a
|
---|
36 | * @param {Function} fn The iterator function. Receives two values, the current element from the list and the accumulator
|
---|
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 RA.reduceP|reduceP}, {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce}
|
---|
41 | * @example
|
---|
42 | *
|
---|
43 | * RA.reduceRightP(
|
---|
44 | * (fileName, total) => 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.reduceRightP(
|
---|
52 | * (fileName, total) => 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.reduceRightP(
|
---|
60 | * (fileName, total) => 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.reduceRightP(
|
---|
68 | * (fileName, total) => 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 */
|
---|
77 | var reduceRightP = (0, _ramda.curryN)(3, function (fn, acc, list) {
|
---|
78 | return (0, _resolveP["default"])(list).then(function (iterable) {
|
---|
79 | var listLength = (0, _ramda.length)(iterable);
|
---|
80 | if (listLength === 0) {
|
---|
81 | return acc;
|
---|
82 | }
|
---|
83 | var reducer = (0, _ramda.reduceRight)(function (arg1, arg2) {
|
---|
84 | var accP;
|
---|
85 | var currentValueP;
|
---|
86 | if (flipArgs) {
|
---|
87 | accP = arg1;
|
---|
88 | currentValueP = arg2;
|
---|
89 | } else {
|
---|
90 | accP = arg2;
|
---|
91 | currentValueP = arg1;
|
---|
92 | }
|
---|
93 | return accP.then(function (previousValue) {
|
---|
94 | return (0, _allP["default"])([previousValue, currentValueP]);
|
---|
95 | }).then(function (_ref) {
|
---|
96 | var _ref2 = _slicedToArray(_ref, 2),
|
---|
97 | previousValue = _ref2[0],
|
---|
98 | currentValue = _ref2[1];
|
---|
99 | if ((0, _isUndefined["default"])(previousValue) && listLength === 1) {
|
---|
100 | return currentValue;
|
---|
101 | }
|
---|
102 | return fn(currentValue, previousValue);
|
---|
103 | });
|
---|
104 | });
|
---|
105 | return reducer((0, _resolveP["default"])(acc), iterable);
|
---|
106 | });
|
---|
107 | });
|
---|
108 | var _default = reduceRightP;
|
---|
109 | exports["default"] = _default; |
---|