UNPKG

moltres-utils

Version:
81 lines (64 loc) 2.24 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 _errorUnexpectedType = _interopRequireDefault(require("./errors/errorUnexpectedType")); var _isArrayLike = _interopRequireDefault(require("../lang/isArrayLike")); var _isPromise = _interopRequireDefault(require("../lang/isPromise")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Returns `true` if all elements of the list match the predicate starting at the given index, `false` otherwise. * * Dispatches to the `everyAtIndex` method of the list argument, if present. * * Supports async predicates. If a predicate returns a Promise than the entire method will upgrade to async and return a Promise. * * @function * @since v0.0.6 * @category data * @param {Function} fn The predicate function. * @param {Integer} index The index to start at. * @param {Array} list The array to consider. * @returns {Boolean} `true` if the predicate is satisfied by at least one element, `false` otherwise. * @example * * const lessThan0 = flip(lt)(0) * const lessThan2 = flip(lt)(2) * any(lessThan0)([1, 2]) //=> false * any(lessThan2)([1, 2]) //=> true */ var everyAtIndex = (0, _curry.default)((0, _defn.default)('everyAtIndex', function (fn, index, list) { if (!(0, _isArrayLike.default)(list)) { throw (0, _errorUnexpectedType.default)('ArrayLike', list); } var length = list.length; var idx = index || 0; if (idx < 0) { idx = length + idx; } if (idx < 0) { idx = 0; } while (idx < length) { var result = fn(list[idx], idx); if ((0, _isPromise.default)(result)) { return result.then(function (resolvedResult) { if (!resolvedResult) { return false; } return everyAtIndex(fn, idx + 1, list); }); } else if (!result) { return false; } idx += 1; } return true; })); var _default = everyAtIndex; exports.default = _default; //# sourceMappingURL=everyAtIndex.js.map