-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
126 lines (98 loc) · 3 KB
/
test.js
File metadata and controls
126 lines (98 loc) · 3 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
var changes = diffChars("aaaa", "caab")
var changes = diffChars("lsjflasfj", "that jaguar")
// console.log(changes);
/*
function chng(old, nww) {
var changes = diffChars(old,nww);
lst = [];
index = 0;
for (const change of changes) {
var added = "";
//console.log("> "+index)
var isAdded = !!change["added"]?1:0;
var isRemoved = !!change["removed"]?1:0;
var isNotChanged = !isAdded && !isRemoved?1:0;
if (isRemoved || isNotChanged) { index += change["count"]; }
if (isAdded) { added += change["value"]; }
// If both values are undefined, then it was not changed and we can ignore it
// Else set status to true=added & false=removed
if (isNotChanged) { continue; }
else { var status = isAdded?1:0; }
if (isAdded) {
lst.push([1, index, change["value"]]);
}
if (isRemoved) {
lst.push([0, index]);
}
return lst;
// console.log(status)
//console.log(change["count"])// + ": " + isAdded + isRemoved + isNotChanged);
}
}
*/
function diffEncode(old, nww) {
var changes = diffChars(old,nww);
lst = "";
var removed = "";
index = 0;
for (const change of changes) {
//console.log("> "+index)
var isAdded = !!change["added"]?1:0;
var isRemoved = !!change["removed"]?1:0;
var isNotChanged = !isAdded && !isRemoved?1:0;
if (isRemoved) { removed += change["value"]; }
if (isAdded) { lst += "A"; }
if (isRemoved) { lst += "D"; }
if (isNotChanged) { lst += "O"; }
lst += change["count"];
// console.log(status)
//console.log(change["count"])// + ": " + isAdded + isRemoved + isNotChanged);
}
return [lst, removed];
}
/*
D2 A5 O1 D2 O1 D3 A4
*/
function parse(lst) {
var arr = [];
var count = "";
var kind = lst[0];
// console.log(lst[0]);
for (const v of lst.slice(1)) {
if ("ADO".includes(v)) {
arr.push([kind, count]);
kind = v;
count = "";
}
else {
count += v;
}
}
arr.push([kind, count]);
return arr;
}
var a = "aaaa"; b = "bbbb";
function pushPop(here, there, count) {
here = here.concat(there.slice(0, count))
there = there.slice(count);
return [here, there];
}
function diffDecode(newString, lst, removed) {
var oldString = "";
for (let elem of parse(lst)) {
var [kind, count] = elem;
if (kind === "A") {
newString = pushPop(oldString, newString, count)[1];
}
if (kind === "D") {
[oldString, removed] = pushPop(oldString, removed, count);
}
if (kind === "O") {
[oldString, newString] = pushPop(oldString, newString, count);
}
}
return oldString;
}
// console.log(diffEncode("lsjflasfj", "that jaguar"));
var [lst, removed] = diffEncode("lsjflasfj", "that jaguar");
// console.log(diffDecode(lst, removed));