This repository was archived by the owner on Mar 6, 2018. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdiff.js
More file actions
159 lines (135 loc) · 3.64 KB
/
Copy pathdiff.js
File metadata and controls
159 lines (135 loc) · 3.64 KB
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
'use strict'
var encode = require('json8-pointer').encode
var equal = require('json8/lib/equal')
var type = require('json8/lib/type')
var ARRAY = 'array'
var OBJECT = 'object'
module.exports = function diff(a, b, pre) {
var patches = []
var prefix = pre || []
var at = type(a)
var bt = type(b)
if (bt !== at) {
if (at === undefined)
patches.push({"op": "add", "path": encode(prefix), "value": b})
else
patches.push({"op": "replace", "path": encode(prefix), "value": b})
return patches
}
else if (bt !== ARRAY && bt !== OBJECT) {
if (!equal(a, b))
patches.push({"op": "replace", "path": encode(prefix), "value": b})
return patches
}
if (a === b)
return patches
// both are arrays
if (Array.isArray(b)) {
// FIXME let's be smarter about array diffing
if (a.length === 0 && b.length === 0)
return patches
if (equal(a, b))
return patches
patches.push({"op": "replace", "path": encode(prefix), "value": b})
}
// both are objects
else if (bt === OBJECT) {
var i, l, keys, k
keys = Object.keys(b)
for (i = 0, l = keys.length; i < l; i++) {
k = keys[i]
patches = patches.concat(diff(a[k], b[k], prefix.concat([k])))
}
keys = Object.keys(a)
for (i = 0, l = keys.length; i < l; i++) {
k = keys[i]
if (b[k] !== undefined)
continue
patches.push({"op": "remove", "path": encode(prefix.concat([k]))})
}
}
return patches
}
var index = require('json8-pointer').index
var equal = require('json8/lib/equal')
function getValuePath (obj, value) {
for (var i in obj) {
if (equal(obj[i], value)) return i
}
return undefined
}
// function diffdiff (A, B, pre) {
// var patches = []
// var prefix = pre || []
// var idxA = index(A)
// var idxB = index(B)
// Object.keys(B).forEach(function (k) {
// var v = B[k]
// if (A[k] !== undefined)
// return
// var path = getValuePath(idxA, v)
// if (!path) {
// patches.push({op: })
// }
// if (path && !idxB[path]) { // FIXME ''
// patches.push({"op": "move", "from": path, "path": encode(prefix.concat([k]))})
// }
// })
// return patches
// }
function diffdiff (A, B) {
var patches = []
// var prefix = pre || []
var indexA = index(A)
var indexB = index(B)
for (var kB in indexB) {
if (kB === '') continue
var vB = indexB[kB]
var vA = indexA[kB]
if (!equal(vA, vB)) {
var patch
for (var kA in indexA) {
if (equal(indexA[kA], vB)) {
if (indexB[kA]) {
patch = {"op": "copy", "from": kA, "path": kB}
} else {
patch = {"op": "move", "from": kA, "path": kB}
console.log(indexA[kA])
delete indexA[kA]
// indexA[kA] = undefined
console.log(indexA[kA])
}
break
}
}
if (!patch) {
patch ={"op": "add", "path": kB, "value": vB}
}
patches.push(patch)
}
// if (index)
// if (equal(indexA[k], v)) {
// } else
}
// Object.keys(B).forEach(function (k) {
// var v = B[k]
// if (A[k] !== undefined)
// return
// var path = getValuePath(idxA, v)
// if (!path) {
// patches.push({op: })
// }
// if (path && !idxB[path]) { // FIXME ''
// patches.push({"op": "move", "from": path, "path": encode(prefix.concat([k]))})
// }
// })
return patches
}
var obj1 = {foo: "much"}
// var obj2 = {foo: "muchsdf"}
// var obj2 = {truc: "much"}
var obj2 = {truc: "much", machin: "much"}
// var obj2 = {truc: "much", machin: "much", foo: "much"}
console.log(obj1)
console.log(obj2)
console.log(diffdiff(obj1, obj2))