baseMatchesProperty.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var baseGet = require('./baseGet'),
  2. baseIsEqual = require('./baseIsEqual'),
  3. baseSlice = require('./baseSlice'),
  4. isArray = require('../lang/isArray'),
  5. isKey = require('./isKey'),
  6. isStrictComparable = require('./isStrictComparable'),
  7. last = require('../array/last'),
  8. toObject = require('./toObject'),
  9. toPath = require('./toPath');
  10. /**
  11. * The base implementation of `_.matchesProperty` which does not clone `srcValue`.
  12. *
  13. * @private
  14. * @param {string} path The path of the property to get.
  15. * @param {*} srcValue The value to compare.
  16. * @returns {Function} Returns the new function.
  17. */
  18. function baseMatchesProperty(path, srcValue) {
  19. var isArr = isArray(path),
  20. isCommon = isKey(path) && isStrictComparable(srcValue),
  21. pathKey = (path + '');
  22. path = toPath(path);
  23. return function(object) {
  24. if (object == null) {
  25. return false;
  26. }
  27. var key = pathKey;
  28. object = toObject(object);
  29. if ((isArr || !isCommon) && !(key in object)) {
  30. object = path.length == 1 ? object : baseGet(object, baseSlice(path, 0, -1));
  31. if (object == null) {
  32. return false;
  33. }
  34. key = last(path);
  35. object = toObject(object);
  36. }
  37. return object[key] === srcValue
  38. ? (srcValue !== undefined || (key in object))
  39. : baseIsEqual(srcValue, object[key], undefined, true);
  40. };
  41. }
  42. module.exports = baseMatchesProperty;