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.
129 lines (107 loc) • 5.47 kB
JavaScript
exports.__esModule = true;
exports["default"] = void 0;
var _ramda = require("ramda");
var _isUndefined = _interopRequireDefault(require("./isUndefined"));
var _resolveP = _interopRequireDefault(require("./resolveP"));
var _allP = _interopRequireDefault(require("./allP"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
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."); }
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); }
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; }
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; 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; }
// in older ramda versions the order of the arguments is flipped
var flipArgs = (0, _ramda.pipe)((0, _ramda.reduceRight)(_ramda.concat, ''), (0, _ramda.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 = (0, _ramda.curryN)(3, function (fn, acc, list) {
return (0, _resolveP["default"])(list).then(function (iterable) {
var listLength = (0, _ramda.length)(iterable);
if (listLength === 0) {
return acc;
}
var reducer = (0, _ramda.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 (0, _allP["default"])([previousValue, currentValueP]);
}).then(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
previousValue = _ref2[0],
currentValue = _ref2[1];
if ((0, _isUndefined["default"])(previousValue) && listLength === 1) {
return currentValue;
}
return fn(currentValue, previousValue);
});
});
return reducer((0, _resolveP["default"])(acc), iterable);
});
});
var _default = reduceRightP;
exports["default"] = _default;
;