isMatch.js 791 B

1234567891011121314151617181920212223242526272829303132
  1. var keys = require('./keys')
  2. var findIndexOf = require('./findIndexOf')
  3. var isEqual = require('./isEqual')
  4. var some = require('./some')
  5. var includeArrays = require('./includeArrays')
  6. /**
  7. * 判断属性中的键和值是否包含在对象中
  8. *
  9. * @param {Object/Array} obj 对象
  10. * @param {Object} source 值
  11. * @return {Boolean}
  12. */
  13. function isMatch (obj, source) {
  14. var objKeys = keys(obj)
  15. var sourceKeys = keys(source)
  16. if (sourceKeys.length) {
  17. if (includeArrays(objKeys, sourceKeys)) {
  18. return some(sourceKeys, function (key2) {
  19. return findIndexOf(objKeys, function (key1) {
  20. return key1 === key2 && isEqual(obj[key1], source[key2])
  21. }) > -1
  22. })
  23. }
  24. } else {
  25. return true
  26. }
  27. return isEqual(obj, source)
  28. }
  29. module.exports = isMatch