matches.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var baseClone = require('./_baseClone'),
  2. baseMatches = require('./_baseMatches');
  3. /** Used to compose bitmasks for cloning. */
  4. var CLONE_DEEP_FLAG = 1;
  5. /**
  6. * Creates a function that performs a partial deep comparison between a given
  7. * object and `source`, returning `true` if the given object has equivalent
  8. * property values, else `false`.
  9. *
  10. * **Note:** The created function is equivalent to `_.isMatch` with `source`
  11. * partially applied.
  12. *
  13. * Partial comparisons will match empty array and empty object `source`
  14. * values against any array or object value, respectively. See `_.isEqual`
  15. * for a list of supported value comparisons.
  16. *
  17. * **Note:** Multiple values can be checked by combining several matchers
  18. * using `_.overSome`
  19. *
  20. * @static
  21. * @memberOf _
  22. * @since 3.0.0
  23. * @category Util
  24. * @param {Object} source The object of property values to match.
  25. * @returns {Function} Returns the new spec function.
  26. * @example
  27. *
  28. * var objects = [
  29. * { 'a': 1, 'b': 2, 'c': 3 },
  30. * { 'a': 4, 'b': 5, 'c': 6 }
  31. * ];
  32. *
  33. * _.filter(objects, _.matches({ 'a': 4, 'c': 6 }));
  34. * // => [{ 'a': 4, 'b': 5, 'c': 6 }]
  35. *
  36. * // Checking for several possible values
  37. * _.filter(objects, _.overSome([_.matches({ 'a': 1 }), _.matches({ 'a': 4 })]));
  38. * // => [{ 'a': 1, 'b': 2, 'c': 3 }, { 'a': 4, 'b': 5, 'c': 6 }]
  39. */
  40. function matches(source) {
  41. return baseMatches(baseClone(source, CLONE_DEEP_FLAG));
  42. }
  43. module.exports = matches;