1 | import _arrayReduce from "./internal/_arrayReduce.js";
|
---|
2 | import _curry2 from "./internal/_curry2.js";
|
---|
3 | import _dispatchable from "./internal/_dispatchable.js";
|
---|
4 | import _filter from "./internal/_filter.js";
|
---|
5 | import _isObject from "./internal/_isObject.js";
|
---|
6 | import _xfilter from "./internal/_xfilter.js";
|
---|
7 | import 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 | * @category Object
|
---|
23 | * @sig Filterable f => (a -> Boolean) -> f a -> f a
|
---|
24 | * @param {Function} pred
|
---|
25 | * @param {Array} filterable
|
---|
26 | * @return {Array} Filterable
|
---|
27 | * @see R.reject, R.transduce, R.addIndex
|
---|
28 | * @example
|
---|
29 | *
|
---|
30 | * const isEven = n => n % 2 === 0;
|
---|
31 | *
|
---|
32 | * R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
|
---|
33 | *
|
---|
34 | * R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}
|
---|
35 | */
|
---|
36 |
|
---|
37 | var filter =
|
---|
38 | /*#__PURE__*/
|
---|
39 | _curry2(
|
---|
40 | /*#__PURE__*/
|
---|
41 | _dispatchable(['fantasy-land/filter', 'filter'], _xfilter, function (pred, filterable) {
|
---|
42 | return _isObject(filterable) ? _arrayReduce(function (acc, key) {
|
---|
43 | if (pred(filterable[key])) {
|
---|
44 | acc[key] = filterable[key];
|
---|
45 | }
|
---|
46 |
|
---|
47 | return acc;
|
---|
48 | }, {}, keys(filterable)) : // else
|
---|
49 | _filter(pred, filterable);
|
---|
50 | }));
|
---|
51 |
|
---|
52 | export default filter; |
---|