ltdtDiffByKey.mjs

  1. import each from 'lodash-es/each.js'
  2. import find from 'lodash-es/find.js'
  3. import isEqual from 'lodash-es/isEqual.js'
  4. import isestr from './isestr.mjs'
  5. import isarr from './isarr.mjs'
  6. import iser from './iser.mjs'
  7. import haskey from './haskey.mjs'
  8. /**
  9. * 比對新舊物件陣列,基於指定key為主鍵,比對vnew對vold中有差異之項目
  10. *
  11. * Unit Test: {@link https://github.com/yuda-lyu/wsemi/blob/master/test/ltdtDiffByKey.test.mjs Github}
  12. * @memberOf wsemi
  13. * @param {Array|Object} ltdtOld 輸入舊的物件陣列或物件
  14. * @param {Array|Object} ltdtNew 輸入新的物件陣列或物件
  15. * @param {String} key 輸入比對的主鍵key值字串
  16. * @returns {Object} 回傳比對結果物件
  17. * @example
  18. *
  19. * let ltdtOld = [{ id: 'id-1', a: 'a1' }, { id: 'id-2', a: 'a2' }, { id: 'id-3', a: 'a3' }]
  20. * let ltdtNew = [{ id: 'id-1', z: 'z3' }, { id: 'id-3', a: 'a3' }, { id: 'id-4', a: 'a4' }]
  21. * console.log(ltdtDiffByKey(ltdtOld, ltdtNew, 'id'))
  22. * // => {
  23. * // infor: { 'id-1': 'diff', 'id-2': 'del', 'id-3': 'same', 'id-4': 'add' },
  24. * // del: [ { id: 'id-2', a: 'a2' } ],
  25. * // same: [ { id: 'id-3', a: 'a3' } ],
  26. * // diff: [ { id: 'id-1', z: 'z3' } ],
  27. * // add: [ { id: 'id-4', a: 'a4' } ]
  28. * // }
  29. *
  30. */
  31. function ltdtDiffByKey(ltdtOld, ltdtNew, key) {
  32. //def
  33. let def = {
  34. infor: {},
  35. del: [],
  36. same: [],
  37. diff: [],
  38. add: [],
  39. }
  40. //check
  41. if (!isestr(key)) {
  42. console.log('invalid key')
  43. return def
  44. }
  45. //to array
  46. if (!isarr(ltdtOld)) { //有可能傳空陣列故使用isarr
  47. ltdtOld = [ltdtOld]
  48. }
  49. if (!isarr(ltdtNew)) { //有可能傳空陣列故使用isarr
  50. ltdtNew = [ltdtNew]
  51. }
  52. // //check obj and key, 可能有空陣列情形
  53. // let haveKey = 0
  54. // each(ltdtOld, function(v) {
  55. // if (haskey(v, key)) {
  56. // haveKey = 1
  57. // }
  58. // })
  59. // if (haveKey === 0) {
  60. // return def
  61. // }
  62. // each(ltdtNew, function(v) {
  63. // if (haskey(v, key)) {
  64. // haveKey = 2
  65. // }
  66. // })
  67. // if (haveKey === 1) {
  68. // return def
  69. // }
  70. //tInfor, tDel, tDiff, tSame
  71. let tInfor = {}
  72. let tDel = []
  73. let tDiff = []
  74. let tSame = []
  75. each(ltdtOld, function(v) {
  76. if (haskey(v, key)) { //v需存在key
  77. let q = {
  78. [key]: v[key]
  79. }
  80. let o = find(ltdtNew, q)
  81. if (iser(o)) {
  82. tDel.push(v) //vold需刪去之項目
  83. tInfor[v[key]] = 'del'
  84. }
  85. else {
  86. if (isEqual(v, o)) {
  87. tSame.push(o) //vsame與vold有同樣之項目
  88. tInfor[o[key]] = 'same'
  89. }
  90. else {
  91. tDiff.push(o) //vsame對vold有變更之項目
  92. tInfor[o[key]] = 'diff'
  93. }
  94. }
  95. }
  96. })
  97. //vadd
  98. let vadd = []
  99. each(ltdtNew, function(v) {
  100. if (haskey(v, key)) { //v需存在key
  101. let q = {
  102. [key]: v[key]
  103. }
  104. let o = find(ltdtOld, q)
  105. if (iser(o)) {
  106. vadd.push(v) //vsame對vold有新增之項目
  107. tInfor[v[key]] = 'add'
  108. }
  109. }
  110. })
  111. return {
  112. infor: tInfor,
  113. del: tDel,
  114. same: tSame,
  115. diff: tDiff,
  116. add: vadd,
  117. }
  118. }
  119. export default ltdtDiffByKey