UNPKG

moltres-utils

Version:
67 lines (52 loc) 2.38 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 _filterAtIndex = _interopRequireDefault(require("./filterAtIndex")); var _isArrayLike = _interopRequireDefault(require("../lang/isArrayLike")); var _isString = _interopRequireDefault(require("../lang/isString")); var _join = _interopRequireDefault(require("./join")); var _keys = _interopRequireDefault(require("./keys")); var _pick = _interopRequireDefault(require("./pick")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // TODO BRN: Improve this method to maintain the original array in memory when no changes are made /** * Takes a predicate and a `Filterable`, and returns a new filterable of the same type containing the members of the given filterable which satisfy the given predicate. Filterable objects include plain objects or any object that has a filter method such as `Array`. * * Dispatches to the `filter` method of the second 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 {*} collection The collection to consider. * @returns {*} The filtered collection * @example * * const isEven = n => n % 2 === 0; * * filter(isEven, [1, 2, 3, 4]) //=> [2, 4] * filter(isEven, {a: 1, b: 2, c: 3, d: 4}) //=> {b: 2, d: 4} * * await filter(async (value) => isEven(value), [1, 2, 3, 4]) //=> [2, 4] */ var filter = (0, _curry.default)((0, _defn.default)('filter', function (fn, collection) { if ((0, _isString.default)(collection)) { return (0, _join.default)('', (0, _filterAtIndex.default)(fn, 0, collection)); } if ((0, _isArrayLike.default)(collection)) { return (0, _filterAtIndex.default)(fn, 0, collection); } return (0, _pick.default)((0, _filterAtIndex.default)(function (key) { return fn(collection[key], key); }, 0, (0, _keys.default)(collection)), collection); })); var _default = filter; exports.default = _default; //# sourceMappingURL=filter.js.map