moltres-utils
Version:
Utils for Moltres apps
81 lines (65 loc) • 2.65 kB
JavaScript
require("core-js/modules/es6.object.define-property");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es7.symbol.async-iterator");
require("core-js/modules/es6.symbol");
var _curry = _interopRequireDefault(require("../common/curry"));
var _defn = _interopRequireDefault(require("../common/defn"));
var _isPromise = _interopRequireDefault(require("../lang/isPromise"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/**
* Returns the first element of the list which matches the predicate, or `undefined` if no element matches starting at the given index.
*
* Dispatches to the `findAtIndex` method of the last 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] -> a | undefined
* @param {Function} fn The predicate function used to determine if the element is the
* desired one.
* @param {Integer} index The index to start at.
* @param {Array} list The array to consider.
* @returns {*|Promise} The element found, or `undefined`.
* @example
*
* const xs = [{a: 1}, {a: 2}, {a: 3}];
* findAtIndex(propEq('a'), 0)(xs) //=> {a: 2}
* findAtIndex(propEq('a', 2), 2)(xs) //=> undefined
*/
var findAtIndex = (0, _curry.default)((0, _defn.default)('findAtIndex', function (fn, index, list) {
var length = list.length;
var idx = index || 0; // TODO BRN: abstract this while loop pattern and make it reusable
var _loop = function _loop() {
var value = list[idx];
var result = fn(list[idx], idx);
if ((0, _isPromise.default)(result)) {
return {
v: result.then(function (resolvedResult) {
if (resolvedResult) {
return value;
}
return findAtIndex(fn, idx + 1, list);
})
};
} else if (result) {
return {
v: value
};
}
idx += 1;
};
while (idx < length) {
var _ret = _loop();
if (_typeof(_ret) === "object") return _ret.v;
}
}));
var _default = findAtIndex;
exports.default = _default;
//# sourceMappingURL=findAtIndex.js.map
;