-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.js
50 lines (44 loc) · 905 Bytes
/
diff.js
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
'use strict';
module.exports = {
reverse: function (diff) {
var copyDiff = diff.slice();
for (var i = 0; i < copyDiff.length; i++) {
var math = copyDiff[i][0] === '-' ? '+' : '-';
copyDiff[i] = math + copyDiff[i].slice(1);
}
return copyDiff;
},
merge: function (source, diff) {
var res = source ? source.slice() : [];
var index;
for (var i = 0; i < diff.length; i++) {
var math = diff[i][0];
var publicKey = diff[i].slice(1);
if (math === '+') {
res = res || [];
index = -1;
if (res) {
index = res.indexOf(publicKey);
}
if (index !== -1) {
return false;
}
res.push(publicKey);
}
if (math === '-') {
index = -1;
if (res) {
index = res.indexOf(publicKey);
}
if (index === -1) {
return false;
}
res.splice(index, 1);
if (!res.length) {
res = null;
}
}
}
return res;
}
};