You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

4 years ago
  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. var where =
  39. /*#__PURE__*/
  40. _curry2(function where(spec, testObj) {
  41. for (var prop in spec) {
  42. if (_has(prop, spec) && !spec[prop](testObj[prop])) {
  43. return false;
  44. }
  45. }
  46. return true;
  47. });
  48. export default where;