Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

matchesProperty.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var baseClone = require('./_baseClone'),
  2. baseMatchesProperty = require('./_baseMatchesProperty');
  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 the
  7. * value at `path` of a given object to `srcValue`, returning `true` if the
  8. * object value is equivalent, else `false`.
  9. *
  10. * **Note:** Partial comparisons will match empty array and empty object
  11. * `srcValue` values against any array or object value, respectively. See
  12. * `_.isEqual` for a list of supported value comparisons.
  13. *
  14. * **Note:** Multiple values can be checked by combining several matchers
  15. * using `_.overSome`
  16. *
  17. * @static
  18. * @memberOf _
  19. * @since 3.2.0
  20. * @category Util
  21. * @param {Array|string} path The path of the property to get.
  22. * @param {*} srcValue The value to match.
  23. * @returns {Function} Returns the new spec function.
  24. * @example
  25. *
  26. * var objects = [
  27. * { 'a': 1, 'b': 2, 'c': 3 },
  28. * { 'a': 4, 'b': 5, 'c': 6 }
  29. * ];
  30. *
  31. * _.find(objects, _.matchesProperty('a', 4));
  32. * // => { 'a': 4, 'b': 5, 'c': 6 }
  33. *
  34. * // Checking for several possible values
  35. * _.filter(objects, _.overSome([_.matchesProperty('a', 1), _.matchesProperty('a', 4)]));
  36. * // => [{ 'a': 1, 'b': 2, 'c': 3 }, { 'a': 4, 'b': 5, 'c': 6 }]
  37. */
  38. function matchesProperty(path, srcValue) {
  39. return baseMatchesProperty(path, baseClone(srcValue, CLONE_DEEP_FLAG));
  40. }
  41. module.exports = matchesProperty;