UNPKG

moltres-utils

Version:
92 lines (74 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; require("core-js/modules/es6.array.for-each"); require("core-js/modules/es6.array.filter"); require("core-js/modules/web.dom.iterable"); require("core-js/modules/es6.array.iterator"); require("core-js/modules/es6.object.keys"); require("core-js/modules/es6.object.define-property"); var _curry = _interopRequireDefault(require("../common/curry")); var _defn = _interopRequireDefault(require("../common/defn")); var _iterateRight = _interopRequireDefault(require("../common/iterateRight")); var _pipe = _interopRequireDefault(require("../common/pipe")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * Returns a single item by iterating through the collection, successively calling the iterator function and passing it an accumulator value, the current value and the index or key from the collection, and then passing the result to the next call. * * Similar to [`reduce`](#reduce), except moves through the input list from the right to the left. * * The iterator function receives three values: *(acc, value, kdx)*. * * Supports async reducers. This method will automatically upgrade to async if given an async reducer. * * Dispatches to the `reduce` method of the third argument, if present. * * Note: `reduceRight` does not skip deleted or unassigned indices (sparse arrays), unlike the native `Array.prototype.reduceRight` method. For more details on this behavior, see: * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight#Description * * @function * @since v0.0.10 * @category data * @param {Function} fn The iterator function. Receives three values, the accumulator, the current value from the collection and the key or index. * @param {*} accumulator The accumulator value. * @param {Array|string|Object|Promise} collection The collection to iterate over. * @returns {*} The final, accumulated value. * @example * * reduceRight(subtract, 0, [1, 2, 3, 4]) // => (1 - (2 - (3 - (4 - 0)))) = -2 * // - -2 * // / \ / \ * // 1 - 1 3 * // / \ / \ * // 2 - ==> 2 -1 * // / \ / \ * // 3 - 3 4 * // / \ / \ * // 4 0 4 0 */ var reduceRight = (0, _curry.default)((0, _defn.default)('reduceRight', function (iteratee, accumulator, collection) { var accum = accumulator; return (0, _iterateRight.default)(function (next) { return (0, _pipe.default)(function (pNext) { if (pNext.done) { return accum; } return iteratee(accum, pNext.value, pNext.kdx); }, function (nextAccum) { accum = nextAccum; if (next.done) { return _objectSpread({}, next, { value: accum }); } return next; })(next); }, collection); })); var _default = reduceRight; exports.default = _default; //# sourceMappingURL=reduceRight.js.map