helperEqualCompare.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var isNumber = require('./isNumber')
  2. var isArray = require('./isArray')
  3. var isString = require('./isString')
  4. var isRegExp = require('./isRegExp')
  5. var isDate = require('./isDate')
  6. var isBoolean = require('./isBoolean')
  7. var isUndefined = require('./isUndefined')
  8. var keys = require('./keys')
  9. var every = require('./every')
  10. function helperEqualCompare (val1, val2, compare, func, key, obj1, obj2) {
  11. if (val1 === val2) {
  12. return true
  13. }
  14. if (val1 && val2 && !isNumber(val1) && !isNumber(val2) && !isString(val1) && !isString(val2)) {
  15. if (isRegExp(val1)) {
  16. return compare('' + val1, '' + val2, key, obj1, obj2)
  17. } if (isDate(val1) || isBoolean(val1)) {
  18. return compare(+val1, +val2, key, obj1, obj2)
  19. } else {
  20. var result, val1Keys, val2Keys
  21. var isObj1Arr = isArray(val1)
  22. var isObj2Arr = isArray(val2)
  23. if (isObj1Arr || isObj2Arr ? isObj1Arr && isObj2Arr : val1.constructor === val2.constructor) {
  24. val1Keys = keys(val1)
  25. val2Keys = keys(val2)
  26. if (func) {
  27. result = func(val1, val2, key)
  28. }
  29. if (val1Keys.length === val2Keys.length) {
  30. return isUndefined(result) ? every(val1Keys, function (key, index) {
  31. return key === val2Keys[index] && helperEqualCompare(val1[key], val2[val2Keys[index]], compare, func, isObj1Arr || isObj2Arr ? index : key, val1, val2)
  32. }) : !!result
  33. }
  34. return false
  35. }
  36. }
  37. }
  38. return compare(val1, val2, key, obj1, obj2)
  39. }
  40. module.exports = helperEqualCompare