moltres-utils
Version:
Utils for Moltres apps
110 lines (92 loc) • 4.3 kB
JavaScript
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 _iterate = _interopRequireDefault(require("../common/iterate"));
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 and the current value from the collection, and then passing the result to the next call.
*
* The iterator function receives three values: *(acc, value, kdx)*.
*
* Note: This method automatically upgrades to async.
* - If an async `iteratee` is given to this method it will return a Promise.
* - If a Promise is given for `iteratee`, `accumulator`, or `collection`, this method will resolve the Promise(s) and return a Promise
*
* Note: for arrays, `reduce` does not skip deleted or unassigned indices (sparse arrays), unlike the native `Array.prototype.reduce` method. For more details on this behavior, see:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#Description
*
* Dispatches to the `reduce` method of the third argument, if present.
*
* This method will resolve the parameters but it will not resolve the values passed to the iteratee. It will only resolve the values returned by the iteratee.
*
* @function
* @since v0.0.3
* @category data
* @param {Function} iteratee 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
*
* reduce((accum, value, key) => {
* ...
* return accum
* }, myAccum, myObject)
*
* reduce((accum, value, index) => {
* ...
* return accum
* }, myAccum, myArray)
*
* reduce((accum, value) => {
* accum.push(value)
* return accum
* }, [], [ Promise.resolve('foo') ]) // => [ Promise { 'foo' } ]
*
*
* reduce(subtract, 0, [1, 2, 3, 4]) // => ((((0 - 1) - 2) - 3) - 4) = -10
* // - -10
* // / \ / \
* // - 4 -6 4
* // / \ / \
* // - 3 ==> -3 3
* // / \ / \
* // - 2 -1 2
* // / \ / \
* // 0 1 0 1
*/
var reduce = (0, _curry.default)((0, _defn.default)('reduce', function (iteratee, accumulator, collection) {
var accum = accumulator;
return (0, _iterate.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 = reduce;
exports.default = _default;
//# sourceMappingURL=reduce.js.map
;