UNPKG

1.54 kBJavaScriptView Raw
1import _curry2 from "./internal/_curry2.js";
2import _dispatchable from "./internal/_dispatchable.js";
3import _filter from "./internal/_filter.js";
4import _isObject from "./internal/_isObject.js";
5import _reduce from "./internal/_reduce.js";
6import _xfilter from "./internal/_xfilter.js";
7import keys from "./keys.js";
8/**
9 * Takes a predicate and a `Filterable`, and returns a new filterable of the
10 * same type containing the members of the given filterable which satisfy the
11 * given predicate. Filterable objects include plain objects or any object
12 * that has a filter method such as `Array`.
13 *
14 * Dispatches to the `filter` method of the second argument, if present.
15 *
16 * Acts as a transducer if a transformer is given in list position.
17 *
18 * @func
19 * @memberOf R
20 * @since v0.1.0
21 * @category List
22 * @sig Filterable f => (a -> Boolean) -> f a -> f a
23 * @param {Function} pred
24 * @param {Array} filterable
25 * @return {Array} Filterable
26 * @see R.reject, R.transduce, R.addIndex
27 * @example
28 *
29 * const isEven = n => n % 2 === 0;
30 *
31 * R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
32 *
33 * R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
34 */
35
36var filter =
37/*#__PURE__*/
38_curry2(
39/*#__PURE__*/
40_dispatchable(['filter'], _xfilter, function (pred, filterable) {
41 return _isObject(filterable) ? _reduce(function (acc, key) {
42 if (pred(filterable[key])) {
43 acc[key] = filterable[key];
44 }
45
46 return acc;
47 }, {}, keys(filterable)) : // else
48 _filter(pred, filterable);
49}));
50
51export default filter;
\No newline at end of file