UNPKG

ramda

Version:

A practical functional library for JavaScript programmers.

49 lines (48 loc) 1.75 kB
import _arrayReduce from "./internal/_arrayReduce.js"; import _curry2 from "./internal/_curry2.js"; import _dispatchable from "./internal/_dispatchable.js"; import _filter from "./internal/_filter.js"; import _filterMap from "./internal/_filterMap.js"; import _isMap from "./internal/_isMap.js"; import _isObject from "./internal/_isObject.js"; import _xfilter from "./internal/_xfilter.js"; import keys from "./keys.js"; /** * 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, Maps, or any object * that has a filter method such as `Array`. * * Dispatches to the `filter` method of the second argument, if present. * * Acts as a transducer if a transformer is given in list position. * * @func * @memberOf R * @since v0.1.0 * @category List * @category Object * @sig Filterable f => (a -> Boolean) -> f a -> f a * @param {Function} pred * @param {Array} filterable * @return {Array} Filterable * @see R.reject, R.transduce, R.addIndex * @example * * const isEven = n => n % 2 === 0; * * R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4] * * R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4} */ var filter = /*#__PURE__*/_curry2( /*#__PURE__*/_dispatchable(['fantasy-land/filter', 'filter'], _xfilter, function (pred, filterable) { return _isObject(filterable) ? _arrayReduce(function (acc, key) { if (pred(filterable[key])) { acc[key] = filterable[key]; } return acc; }, {}, keys(filterable)) : _isMap(filterable) ? _filterMap(pred, filterable) : // else _filter(pred, filterable); })); export default filter;