moltres-utils
Version:
Utils for Moltres apps
85 lines (68 loc) • 2.39 kB
JavaScript
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 at least one of elements of the list match the predicate starting at the given index, `false` otherwise.
*
* Dispatches to the `anyAtIndex` 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.3
* @category data
* @sig (a -> Boolean) -> [a] -> Boolean
* @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)
* anyAtIndex(lessThan0, 0, [3, 2, 1]) //=> false
* anyAtIndex(lessThan2, 1, [3, 2, 1]) //=> true
*
* await anyAtIndex(async (value) => lessThan2(value), 0, [1, 2]) //=> true
*/
var anyAtIndex = (0, _curry.default)((0, _defn.default)('anyAtIndex', 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 true;
}
return anyAtIndex(fn, idx + 1, list);
});
} else if (result) {
return true;
}
idx += 1;
}
return false;
}));
var _default = anyAtIndex;
exports.default = _default;
//# sourceMappingURL=anyAtIndex.js.map
;