123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const ARRAYTYPE = '[object Array]'
  2. const OBJECTTYPE = '[object Object]'
  3. const FUNCTIONTYPE = '[object Function]'
  4. export default function diff(current, pre) {
  5. const result = {}
  6. syncKeys(current, pre)
  7. _diff(current, pre, '', result)
  8. return result
  9. }
  10. function syncKeys(current, pre) {
  11. if (current === pre) return
  12. const rootCurrentType = type(current)
  13. const rootPreType = type(pre)
  14. if (rootCurrentType == OBJECTTYPE && rootPreType == OBJECTTYPE) {
  15. if(Object.keys(current).length >= Object.keys(pre).length){
  16. for (let key in pre) {
  17. const currentValue = current[key]
  18. if (currentValue === undefined) {
  19. current[key] = null
  20. } else {
  21. syncKeys(currentValue, pre[key])
  22. }
  23. }
  24. }
  25. } else if (rootCurrentType == ARRAYTYPE && rootPreType == ARRAYTYPE) {
  26. if (current.length >= pre.length) {
  27. pre.forEach((item, index) => {
  28. syncKeys(current[index], item)
  29. })
  30. }
  31. }
  32. }
  33. function _diff(current, pre, path, result) {
  34. if (current === pre) return
  35. const rootCurrentType = type(current)
  36. const rootPreType = type(pre)
  37. if (rootCurrentType == OBJECTTYPE) {
  38. if (rootPreType != OBJECTTYPE || Object.keys(current).length < Object.keys(pre).length && path !== '') {
  39. setResult(result, path, current)
  40. } else {
  41. for (let key in current) {
  42. const currentValue = current[key]
  43. const preValue = pre[key]
  44. const currentType = type(currentValue)
  45. const preType = type(preValue)
  46. if (currentType != ARRAYTYPE && currentType != OBJECTTYPE) {
  47. if (currentValue != pre[key]) {
  48. setResult(result, (path == '' ? '' : path + ".") + key, currentValue)
  49. }
  50. } else if (currentType == ARRAYTYPE) {
  51. if (preType != ARRAYTYPE) {
  52. setResult(result, (path == '' ? '' : path + ".") + key, currentValue)
  53. } else {
  54. if (currentValue.length < preValue.length) {
  55. setResult(result, (path == '' ? '' : path + ".") + key, currentValue)
  56. } else {
  57. currentValue.forEach((item, index) => {
  58. _diff(item, preValue[index], (path == '' ? '' : path + ".") + key + '[' + index + ']', result)
  59. })
  60. }
  61. }
  62. } else if (currentType == OBJECTTYPE) {
  63. if (preType != OBJECTTYPE || Object.keys(currentValue).length < Object.keys(preValue).length) {
  64. setResult(result, (path == '' ? '' : path + ".") + key, currentValue)
  65. } else {
  66. for (let subKey in currentValue) {
  67. _diff(currentValue[subKey], preValue[subKey], (path == '' ? '' : path + ".") + key + '.' + subKey, result)
  68. }
  69. }
  70. }
  71. }
  72. }
  73. } else if (rootCurrentType == ARRAYTYPE) {
  74. if (rootPreType != ARRAYTYPE) {
  75. setResult(result, path, current)
  76. } else {
  77. if (current.length < pre.length) {
  78. setResult(result, path, current)
  79. } else {
  80. current.forEach((item, index) => {
  81. _diff(item, pre[index], path + '[' + index + ']', result)
  82. })
  83. }
  84. }
  85. } else {
  86. setResult(result, path, current)
  87. }
  88. }
  89. function setResult(result, k, v) {
  90. const t = type(v)
  91. if (t != FUNCTIONTYPE) {
  92. //if (t != OBJECTTYPE && t != ARRAYTYPE) {
  93. result[k] = v
  94. // } else {
  95. // result[k] = JSON.parse(JSON.stringify(v))
  96. // }
  97. }
  98. }
  99. function type(obj) {
  100. return Object.prototype.toString.call(obj)
  101. }