UNPKG

moltres-utils

Version:
60 lines (49 loc) 1.94 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 _findAtIndex = _interopRequireDefault(require("./findAtIndex")); var _isArrayLike = _interopRequireDefault(require("../lang/isArrayLike")); var _keys = _interopRequireDefault(require("./keys")); var _pipe = _interopRequireDefault(require("../common/pipe")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Returns the first element of the collection which matches the predicate, or * `undefined` if no element matches. * * Dispatches to the `find` method of the collection 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, b -> Boolean) -> [a] -> a | undefined * @param {Function} fn The predicate function used to determine if the element is the * desired one. * @param {*} collection The collection to consider. * @returns {Object} The element found, or `undefined`. * @example * * const xs = [{a: 1}, {a: 2}, {a: 3}]; * find(propEq('a', 2))(xs); //=> {a: 2} * find(propEq('a', 4))(xs); //=> undefined */ var find = (0, _curry.default)((0, _defn.default)('find', function (fn, collection) { if ((0, _isArrayLike.default)(collection)) { return (0, _findAtIndex.default)(fn, 0, collection); } return (0, _pipe.default)((0, _findAtIndex.default)(function (key) { return fn(collection[key], key); }, 0), function (key) { return collection[key]; })((0, _keys.default)(collection)); })); var _default = find; exports.default = _default; //# sourceMappingURL=find.js.map