whereAny.js 1.5 KB

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