source: node_modules/ramda-adjunct/es/reduceRightP.js

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

Initial commit

  • Property mode set to 100644
File size: 5.0 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, pipe, equals, reduceRight, length, concat } from 'ramda';
8import isUndefined from './isUndefined';
9import resolveP from './resolveP';
10import allP from './allP';
11
12// in older ramda versions the order of the arguments is flipped
13var flipArgs = pipe(reduceRight(concat, ''), equals('ba'))(['a', 'b']);
14
15/* eslint-disable max-len */
16/**
17 * Given an `Iterable`(arrays are `Iterable`), or a promise of an `Iterable`,
18 * which produces promises (or a mix of promises and values),
19 * iterate over all the values in the `Iterable` into an array and
20 * reduce the array to a value using the given iterator function.
21 *
22 * Similar to {@link RA.reduceP|reduceP} except moves through the input list from the right to the left.
23 * The iterator function receives two values: (value, acc),
24 * while the arguments' order of reduceP's iterator function is (acc, value).
25 *
26 * @func reduceRightP
27 * @memberOf RA
28 * @since {@link https://char0n.github.io/ramda-adjunct/1.13.0|v1.13.0}
29 * @category List
30 * @typedef MaybePromise = Promise.<*> | *
31 * @sig ((MaybePromise b, Promise a) -> Promise a) -> MaybePromise a -> MaybePromise [MaybePromise b] -> Promise a
32 * @param {Function} fn The iterator function. Receives two values, the current element from the list and the accumulator
33 * @param {*|Promise.<*>} acc The accumulator value
34 * @param {Array.<*>|Promise.<Array<*|Promise.<*>>>} list The list to iterate over
35 * @return {Promise} The final, accumulated value
36 * @see {@link RA.reduceP|reduceP}, {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce}
37 * @example
38 *
39 * RA.reduceRightP(
40 * (fileName, total) => fs
41 * .readFileAsync(fileName, 'utf8')
42 * .then(contents => total + parseInt(contents, 10)),
43 * 0,
44 * ['file1.txt', 'file2.txt', 'file3.txt']
45 * ); // => Promise(10)
46 *
47 * RA.reduceRightP(
48 * (fileName, total) => fs
49 * .readFileAsync(fileName, 'utf8')
50 * .then(contents => total + parseInt(contents, 10)),
51 * Promise.resolve(0),
52 * ['file1.txt', 'file2.txt', 'file3.txt']
53 * ); // => Promise(10)
54 *
55 * RA.reduceRightP(
56 * (fileName, total) => fs
57 * .readFileAsync(fileName, 'utf8')
58 * .then(contents => total + parseInt(contents, 10)),
59 * 0,
60 * [Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt']
61 * ); // => Promise(10)
62 *
63 * RA.reduceRightP(
64 * (fileName, total) => fs
65 * .readFileAsync(fileName, 'utf8')
66 * .then(contents => total + parseInt(contents, 10)),
67 * 0,
68 * Promise.resolve([Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt'])
69 * ); // => Promise(10)
70 *
71 */
72/* esline-enable max-len */
73var reduceRightP = curryN(3, function (fn, acc, list) {
74 return resolveP(list).then(function (iterable) {
75 var listLength = length(iterable);
76 if (listLength === 0) {
77 return acc;
78 }
79 var reducer = reduceRight(function (arg1, arg2) {
80 var accP;
81 var currentValueP;
82 if (flipArgs) {
83 accP = arg1;
84 currentValueP = arg2;
85 } else {
86 accP = arg2;
87 currentValueP = arg1;
88 }
89 return accP.then(function (previousValue) {
90 return allP([previousValue, currentValueP]);
91 }).then(function (_ref) {
92 var _ref2 = _slicedToArray(_ref, 2),
93 previousValue = _ref2[0],
94 currentValue = _ref2[1];
95 if (isUndefined(previousValue) && listLength === 1) {
96 return currentValue;
97 }
98 return fn(currentValue, previousValue);
99 });
100 });
101 return reducer(resolveP(acc), iterable);
102 });
103});
104export default reduceRightP;
Note: See TracBrowser for help on using the repository browser.