UNPKG

ramda-adjunct

Version:

Ramda Adjunct is the most popular and most comprehensive set of utilities for use with Ramda, providing a variety of useful, well tested functions with excellent documentation.

113 lines (99 loc) 4.27 kB
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import { curryN, pipe, equals, reduceRight, length, concat } from 'ramda'; import isUndefined from './isUndefined'; import resolveP from './resolveP'; import allP from './allP'; // in older ramda versions the order of the arguments is flipped var flipArgs = pipe(reduceRight(concat, ''), equals('ba'))(['a', 'b']); /* eslint-disable max-len */ /** * Given an `Iterable`(arrays are `Iterable`), or a promise of an `Iterable`, * which produces promises (or a mix of promises and values), * iterate over all the values in the `Iterable` into an array and * reduce the array to a value using the given iterator function. * * Similar to {@link RA.reduceP|reduceP} except moves through the input list from the right to the left. * The iterator function receives two values: (value, acc), * while the arguments' order of reduceP's iterator function is (acc, value). * * @func reduceRightP * @memberOf RA * @since {@link https://char0n.github.io/ramda-adjunct/1.13.0|v1.13.0} * @category List * @typedef MaybePromise = Promise.<*> | * * @sig ((MaybePromise b, Promise a) -> Promise a) -> MaybePromise a -> MaybePromise [MaybePromise b] -> Promise a * @param {Function} fn The iterator function. Receives two values, the current element from the list and the accumulator * @param {*|Promise.<*>} acc The accumulator value * @param {Array.<*>|Promise.<Array<*|Promise.<*>>>} list The list to iterate over * @return {Promise} The final, accumulated value * @see {@link RA.reduceP|reduceP}, {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce} * @example * * RA.reduceRightP( * (fileName, total) => fs * .readFileAsync(fileName, 'utf8') * .then(contents => total + parseInt(contents, 10)), * 0, * ['file1.txt', 'file2.txt', 'file3.txt'] * ); // => Promise(10) * * RA.reduceRightP( * (fileName, total) => fs * .readFileAsync(fileName, 'utf8') * .then(contents => total + parseInt(contents, 10)), * Promise.resolve(0), * ['file1.txt', 'file2.txt', 'file3.txt'] * ); // => Promise(10) * * RA.reduceRightP( * (fileName, total) => fs * .readFileAsync(fileName, 'utf8') * .then(contents => total + parseInt(contents, 10)), * 0, * [Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt'] * ); // => Promise(10) * * RA.reduceRightP( * (fileName, total) => fs * .readFileAsync(fileName, 'utf8') * .then(contents => total + parseInt(contents, 10)), * 0, * Promise.resolve([Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt']) * ); // => Promise(10) * */ /* esline-enable max-len */ var reduceRightP = curryN(3, function (fn, acc, list) { return resolveP(list).then(function (iterable) { var listLength = length(iterable); if (listLength === 0) { return acc; } var reducer = reduceRight(function (arg1, arg2) { var accP; var currentValueP; if (flipArgs) { accP = arg1; currentValueP = arg2; } else { accP = arg2; currentValueP = arg1; } return accP.then(function (previousValue) { return allP([previousValue, currentValueP]); }).then(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), previousValue = _ref2[0], currentValue = _ref2[1]; if (isUndefined(previousValue) && listLength === 1) { return currentValue; } return fn(currentValue, previousValue); }); }); return reducer(resolveP(acc), iterable); }); }); export default reduceRightP;