"use strict"; 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) { 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; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } /* 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. * * If the iterator function returns a promise, then the result of the promise is awaited, * before continuing with next iteration. If any promise in the array is rejected or a promise * returned by the iterator function is rejected, the result is rejected as well. * * If `initialValue` is `undefined` (or a promise that resolves to `undefined`) and * the `Iterable` contains only 1 item, the callback will not be called and * the `Iterable's` single item is returned. If the `Iterable` is empty, the callback * will not be called and `initialValue` is returned (which may be undefined). * * This function is basically equivalent to {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce}. * * @func reduceP * @memberOf RA * @since {@link https://char0n.github.io/ramda-adjunct/1.13.0|v1.13.0} * @category List * @typedef MaybePromise = Promise.<*> | * * @sig ((Promise a, MaybePromise b) -> Promise a) -> MaybePromise a -> MaybePromise [MaybePromise b] -> Promise a * @param {Function} fn The iterator function. Receives two values, the accumulator and the current element from the list * @param {*|Promise.<*>} acc The accumulator value * @param {Array.<*>|Promise.>>} list The list to iterate over * @return {Promise} The final, accumulated value * @see {@link http://ramdajs.com/docs/#reduce|R.reduce}, {@link RA.reduceRightP|reduceRightP}, {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce} * @example * * RA.reduceP( * (total, fileName) => fs * .readFileAsync(fileName, 'utf8') * .then(contents => total + parseInt(contents, 10)), * 0, * ['file1.txt', 'file2.txt', 'file3.txt'] * ); // => Promise(10) * * RA.reduceP( * (total, fileName) => fs * .readFileAsync(fileName, 'utf8') * .then(contents => total + parseInt(contents, 10)), * Promise.resolve(0), * ['file1.txt', 'file2.txt', 'file3.txt'] * ); // => Promise(10) * * RA.reduceP( * (total, fileName) => fs * .readFileAsync(fileName, 'utf8') * .then(contents => total + parseInt(contents, 10)), * 0, * [Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt'] * ); // => Promise(10) * * RA.reduceP( * (total, fileName) => 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 reduceP = (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.reduce)(function (accP, currentValueP) { 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(previousValue, currentValue); }); }); return reducer((0, _resolveP["default"])(acc), iterable); }); }); var _default = reduceP; exports["default"] = _default;