马宇豪
2024-07-16 f591c27b57e2418c9495bc02ae8cfff84d35bc18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
module.exports = {
  diff: diff,
  jsonPatchPathConverter: jsonPatchPathConverter,
};
 
/*
  const obj1 = {a: 4, b: 5};
  const obj2 = {a: 3, b: 5};
  const obj3 = {a: 4, c: 5};
 
  diff(obj1, obj2);
  [
    { "op": "replace", "path": ['a'], "value": 3 }
  ]
 
  diff(obj2, obj3);
  [
    { "op": "remove", "path": ['b'] },
    { "op": "replace", "path": ['a'], "value": 4 }
    { "op": "add", "path": ['c'], "value": 5 }
  ]
 
  // using converter to generate jsPatch standard paths
  // see http://jsonpatch.com
  import {diff, jsonPatchPathConverter} from 'just-diff'
  diff(obj1, obj2, jsonPatchPathConverter);
  [
    { "op": "replace", "path": '/a', "value": 3 }
  ]
 
  diff(obj2, obj3, jsonPatchPathConverter);
  [
    { "op": "remove", "path": '/b' },
    { "op": "replace", "path": '/a', "value": 4 }
    { "op": "add", "path": '/c', "value": 5 }
  ]
 
  // arrays
  const obj4 = {a: 4, b: [1, 2, 3]};
  const obj5 = {a: 3, b: [1, 2, 4]};
  const obj6 = {a: 3, b: [1, 2, 4, 5]};
 
  diff(obj4, obj5);
  [
    { "op": "replace", "path": ['a'], "value": 3 }
    { "op": "replace", "path": ['b', 2], "value": 4 }
  ]
 
  diff(obj5, obj6);
  [
    { "op": "add", "path": ['b', 3], "value": 5 }
  ]
 
  // nested paths
  const obj7 = {a: 4, b: {c: 3}};
  const obj8 = {a: 4, b: {c: 4}};
  const obj9 = {a: 5, b: {d: 4}};
 
  diff(obj7, obj8);
  [
    { "op": "replace", "path": ['b', 'c'], "value": 4 }
  ]
 
  diff(obj8, obj9);
  [
    { "op": "replace", "path": ['a'], "value": 5 }
    { "op": "remove", "path": ['b', 'c']}
    { "op": "add", "path": ['b', 'd'], "value": 4 }
  ]
*/
 
function diff(obj1, obj2, pathConverter) {
  if (!obj1 || typeof obj1 != 'object' || !obj2 || typeof obj2 != 'object') {
    throw new Error('both arguments must be objects or arrays');
  }
 
  pathConverter ||
    (pathConverter = function(arr) {
      return arr;
    });
 
  function getDiff({obj1, obj2, basePath, basePathForRemoves, diffs}) {
    var obj1Keys = Object.keys(obj1);
    var obj1KeysLength = obj1Keys.length;
    var obj2Keys = Object.keys(obj2);
    var obj2KeysLength = obj2Keys.length;
    var path;
 
    var lengthDelta = obj1.length - obj2.length;
 
    if (trimFromRight(obj1, obj2)) {
      for (var i = 0; i < obj1KeysLength; i++) {
        var key = Array.isArray(obj1) ? Number(obj1Keys[i]) : obj1Keys[i];
        if (!(key in obj2)) {
          path = basePathForRemoves.concat(key);
          diffs.remove.push({
            op: 'remove',
            path: pathConverter(path),
          });
        }
      }
 
      for (var i = 0; i < obj2KeysLength; i++) {
        var key = Array.isArray(obj2) ? Number(obj2Keys[i]) : obj2Keys[i];
        pushReplaces({
          key,
          obj1,
          obj2,
          path: basePath.concat(key),
          pathForRemoves: basePath.concat(key),
          diffs,
        });
      }
    } else {
      // trim from left, objects are both arrays
      for (var i = 0; i < lengthDelta; i++) {
        path = basePathForRemoves.concat(i);
        diffs.remove.push({
          op: 'remove',
          path: pathConverter(path),
        });
      }
 
      // now make a copy of obj1 with excess elements left trimmed and see if there any replaces
      var obj1Trimmed = obj1.slice(lengthDelta);;
      for (var i = 0; i < obj2KeysLength; i++) {
        pushReplaces({
          key: i,
          obj1: obj1Trimmed,
          obj2,
          path: basePath.concat(i),
          // since list of removes are reversed before presenting result,
          // we need to ignore existing parent removes when doing nested removes
          pathForRemoves: basePath.concat(i + lengthDelta),
          diffs,
        });
      }
    }
  }
 
  var diffs = {remove: [], replace: [], add: []};
  getDiff({
    obj1,
    obj2,
    basePath: [],
    basePathForRemoves: [],
    diffs,
  });
 
  // reverse removes since we want to maintain indexes
  return diffs.remove
    .reverse()
    .concat(diffs.replace)
    .concat(diffs.add);
 
  function pushReplaces({key, obj1, obj2, path, pathForRemoves, diffs}) {
    var obj1AtKey = obj1[key];
    var obj2AtKey = obj2[key];
 
    if(!(key in obj1) && (key in obj2)) {
      var obj2Value = obj2AtKey;
      diffs.add.push({
        op: 'add',
        path: pathConverter(path),
        value: obj2Value,
      });
    } else if(obj1AtKey !== obj2AtKey) {
      if(Object(obj1AtKey) !== obj1AtKey ||
        Object(obj2AtKey) !== obj2AtKey || differentTypes(obj1AtKey, obj2AtKey)
      ) {
        pushReplace(path, diffs, obj2AtKey);
      } else {
        if(!Object.keys(obj1AtKey).length &&
          !Object.keys(obj2AtKey).length &&
          String(obj1AtKey) != String(obj2AtKey)) {
          pushReplace(path, diffs, obj2AtKey);
        } else {
          getDiff({
            obj1: obj1[key],
            obj2: obj2[key],
            basePath: path,
            basePathForRemoves: pathForRemoves,
            diffs});
        }
      }
    }
  }
 
  function pushReplace(path, diffs, newValue) {
    diffs.replace.push({
      op: 'replace',
      path: pathConverter(path),
      value: newValue,
    });
  }
}
 
function jsonPatchPathConverter(arrayPath) {
  return [''].concat(arrayPath).join('/');
}
 
function differentTypes(a, b) {
  return Object.prototype.toString.call(a) != Object.prototype.toString.call(b);
}
 
function trimFromRight(obj1, obj2) {
  var lengthDelta = obj1.length - obj2.length;
  if (Array.isArray(obj1) && Array.isArray(obj2) && lengthDelta > 0) {
    var leftMatches = 0;
    var rightMatches = 0;
    for (var i = 0; i < obj2.length; i++) {
      if (String(obj1[i]) === String(obj2[i])) {
        leftMatches++;
      } else {
        break;
      }
    }
    for (var j = obj2.length; j > 0; j--) {
      if (String(obj1[j + lengthDelta]) === String(obj2[j])) {
        rightMatches++;
      } else {
        break;
      }
    }
 
    // bias to trim right becase it requires less index shifting
    return leftMatches >= rightMatches;
  }
  return true;
}