-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
141 lines (141 loc) · 5.44 KB
/
core.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
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Differing.
*/
class Core {
/**
* Get the comparison table based on the specified base and input array.
* @param base Base array.
* @param input Input array.
* @param comparator Comparator callback.
* @returns Returns the comparison table.
*/
static getTable(base, input, comparator) {
var _a;
const table = [];
let rows = new Uint32Array(input.length);
for (let baseIndex = 0; baseIndex < base.length; baseIndex++) {
let diagonal = 0;
table[baseIndex] = rows = rows.slice();
for (let inputIndex = 0; inputIndex < input.length; inputIndex++) {
const latch = rows[inputIndex];
if (comparator(base[baseIndex], input[inputIndex])) {
rows[inputIndex] = diagonal + 1;
}
else {
const previous = (_a = rows[inputIndex - 1]) !== null && _a !== void 0 ? _a : 0;
if (previous > rows[inputIndex]) {
rows[inputIndex] = previous;
}
}
diagonal = latch;
}
}
return table;
}
/**
* Get the comparison changes based on the specified base and input array.
* @param base Base array.
* @param input Input array.
* @param comparator Comparator callback.
* @param table Comparison table.
* @returns Returns the comparison patches.
*/
static getChanges(base, input, comparator, table) {
const changes = [];
const values = [];
let previous, action, value;
for (let baseIndex = base.length - 1, inputIndex = input.length - 1; baseIndex > -1 || inputIndex > -1;) {
if (baseIndex < 0) {
action = 1 /* Insert */;
value = input[inputIndex--];
}
else if (inputIndex < 0) {
action = 2 /* Remove */;
value = base[baseIndex--];
}
else if (comparator(base[baseIndex], input[inputIndex])) {
action = 0 /* Keep */;
value = base[baseIndex--];
inputIndex--;
}
else {
const topResult = baseIndex > 0 ? table[baseIndex - 1][inputIndex] : -1;
const leftResult = inputIndex > 0 ? table[baseIndex][inputIndex - 1] : -1;
if (topResult < leftResult) {
action = 1 /* Insert */;
value = input[inputIndex--];
}
else {
action = 2 /* Remove */;
value = base[baseIndex--];
}
}
if (previous === void 0) {
changes.push((previous = { action: action }));
}
else if (previous.action !== action) {
previous.values = values.splice(0, values.length).reverse();
changes.push((previous = { action: action }));
}
values.push(value);
}
if (previous !== void 0) {
previous.values = values.reverse();
}
return changes.reverse();
}
/**
* Get the comparison patches based on the specified change lists.
* @param changes Change list.
* @returns Returns the comparison patches.
*/
static getPatches(changes) {
const patches = [];
let previous;
for (const change of changes) {
switch (change.action) {
case 0 /* Keep */:
if (previous === void 0 || previous.action !== 0 /* Keep */) {
patches.push((previous = { action: change.action, selection: change.values.slice() }));
}
else {
previous.selection.push(...change.values);
}
break;
case 1 /* Insert */:
if (previous === void 0 || previous.action === 0 /* Keep */) {
patches.push((previous = { action: change.action, selection: change.values.slice() }));
}
else if (previous.action === 2 /* Remove */) {
previous.replacement = change.values;
previous.action = 3 /* Change */;
}
else if (previous.action === 3 /* Change */) {
previous.replacement.push(...change.values);
}
else {
previous.selection.push(...change.values);
}
break;
case 2 /* Remove */:
if (previous === void 0 || previous.action === 0 /* Keep */) {
patches.push((previous = { action: change.action, selection: change.values.slice() }));
}
else if (previous.action === 1 /* Insert */) {
previous.replacement = previous.selection;
previous.selection = change.values;
previous.action = 3 /* Change */;
}
else {
previous.selection.push(...change.values);
}
break;
}
}
return patches;
}
}
exports.Core = Core;
//# sourceMappingURL=core.js.map