1 | import _curry2 from "./internal/_curry2.js";
|
---|
2 | import _has from "./internal/_has.js";
|
---|
3 | /**
|
---|
4 | * Takes a spec object and a test object; returns true if the test satisfies
|
---|
5 | * the spec. Each of the spec's own properties must be a predicate function.
|
---|
6 | * Each predicate is applied to the value of the corresponding property of the
|
---|
7 | * test object. `where` returns true if all the predicates return true, false
|
---|
8 | * otherwise.
|
---|
9 | *
|
---|
10 | * `where` is well suited to declaratively expressing constraints for other
|
---|
11 | * functions such as [`filter`](#filter) and [`find`](#find).
|
---|
12 | *
|
---|
13 | * @func
|
---|
14 | * @memberOf R
|
---|
15 | * @since v0.1.1
|
---|
16 | * @category Object
|
---|
17 | * @sig {String: (* -> Boolean)} -> {String: *} -> Boolean
|
---|
18 | * @param {Object} spec
|
---|
19 | * @param {Object} testObj
|
---|
20 | * @return {Boolean}
|
---|
21 | * @see R.propSatisfies, R.whereEq
|
---|
22 | * @example
|
---|
23 | *
|
---|
24 | * // pred :: Object -> Boolean
|
---|
25 | * const pred = R.where({
|
---|
26 | * a: R.equals('foo'),
|
---|
27 | * b: R.complement(R.equals('bar')),
|
---|
28 | * x: R.gt(R.__, 10),
|
---|
29 | * y: R.lt(R.__, 20)
|
---|
30 | * });
|
---|
31 | *
|
---|
32 | * pred({a: 'foo', b: 'xxx', x: 11, y: 19}); //=> true
|
---|
33 | * pred({a: 'xxx', b: 'xxx', x: 11, y: 19}); //=> false
|
---|
34 | * pred({a: 'foo', b: 'bar', x: 11, y: 19}); //=> false
|
---|
35 | * pred({a: 'foo', b: 'xxx', x: 10, y: 19}); //=> false
|
---|
36 | * pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> false
|
---|
37 | */
|
---|
38 |
|
---|
39 | var where =
|
---|
40 | /*#__PURE__*/
|
---|
41 | _curry2(function where(spec, testObj) {
|
---|
42 | for (var prop in spec) {
|
---|
43 | if (_has(prop, spec) && !spec[prop](testObj[prop])) {
|
---|
44 | return false;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | return true;
|
---|
49 | });
|
---|
50 |
|
---|
51 | export default where; |
---|