UNPKG

moltres-utils

Version:
74 lines (65 loc) 2.67 kB
"use strict"; require("core-js/modules/es6.object.define-property"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; 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 }; } /** * Iterate over a collection calling a provided function `fn` for each element in the collection . * * `fn` receives two arguments: *(value, kdx)* * * Note: `forEach` does not skip deleted or unassigned indices (sparse * arrays), unlike the native `Array.prototype.forEach` method. For more * details on this behavior, see: * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Description * * Also note that, unlike `Array.prototype.forEach`, this `forEach` returns * the original value. In some libraries this function is named `each`. * * Dispatches to the `forEach` method of the second argument, if present. * * This method automatically upgrades to async. * - If the `iteratee` or the `collection` arguments are Promises, this method will resolve those values before executing and this method will return a `Promise`. * - If the `iteratee` returns a `Promise`, this method will reutrn a `Promise` * * This method executes in **series**. If the iteratee returns a `Promise`, it will wait till the `Promise` resolves before it executes the next iteration. * * @function * @since v0.0.3 * @category data * @param {Function} fn The function to invoke. Receives two arguments, `value` and either `index` for arrays or `key` for objects. * @param {*} collection The collection to iterate over. * @returns {*} The original collection. * @example * * const printXPlusFive = x => console.log(x + 5); * forEach(printXPlusFive, [1, 2, 3]); //=> [1, 2, 3] * // logs 6 * // logs 7 * // logs 8 */ var forEach = (0, _curry.default)((0, _defn.default)('forEach', function (fn, collection) { return (0, _pipe.default)(function () { return (0, _iterate.default)(function (next) { return (0, _pipe.default)(function (pNext) { if (pNext.done) { return pNext; } return fn(pNext.value, pNext.kdx, collection); }, function () { return next; })(next); }, collection); }, function () { return collection; })(); })); var _default = forEach; exports.default = _default; //# sourceMappingURL=forEach.js.map