-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·139 lines (112 loc) · 3.64 KB
/
index.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
var SequenceMatcher = require('./SequenceMatcher');
var stripText = function (text) {
return text.replace(/\t/g, "\u00a0\u00a0\u00a0\u00a0")
};
var stripLinebreaks = function (str) {
return str.replace(/^[\n\r]*|[\n\r]*$/g, "");
};
var stringAsLines = function (str) {
var lfpos = str.indexOf("\n");
var crpos = str.indexOf("\r");
var linebreak = ((lfpos > -1 && crpos > -1) || crpos < 0) ? "\n" : "\r";
var lines = str.split(linebreak);
for (var i = 0; i < lines.length; i++) {
lines[i] = stripLinebreaks(lines[i]);
}
return lines;
};
var getCellsInline = function (tidx, tidx2, textLines, change) {
return {
'oldLineNumber': tidx == null ? "" : (tidx + 1).toString(),
'newLineNumber': tidx2 == null ? "" : (tidx2 + 1).toString(),
'action': change,
'text': stripText(textLines[tidx != null ? tidx : tidx2])
}
};
var getCells = function (tidx, tend, textLines, change) {
if (tidx < tend) {
return {
'index': (tidx + 1).toString(),
'action': change,
'text': stripText(textLines[tidx])
};
} else {
return {
'index': null,
'action': 'empty'
};
}
};
var getCellsIndex = function (tidx, tend) {
return (tidx < tend) ? tidx + 1 : tidx;
};
module.exports = {
getInlineDiffsArray: function (first, second) {
first = stringAsLines(first);
second = stringAsLines(second);
if (first == null) {
throw "Cannot build diff view, first text is not defined.";
}
if (second == null) {
throw "Cannot build diff view; newTextLines is not defined.";
}
var opcodes = (new SequenceMatcher(first, second)).get_opcodes();
var rows = [];
for (var idx = 0; idx < opcodes.length; idx++) {
var code = opcodes[idx];
var change = code[0];
var b = code[1];
var be = code[2];
var n = code[3];
var ne = code[4];
var rowcnt = Math.max(be - b, ne - n);
var toprows = [];
var botrows = [];
for (var i = 0; i < rowcnt; i++) {
if (change == "insert") {
toprows.push(getCellsInline(null, n++, second, change));
} else if (change == "replace") {
if (b < be) toprows.push(getCellsInline(b++, null, first, "delete"));
if (n < ne) botrows.push(getCellsInline(null, n++, second, "insert"));
} else if (change == "delete") {
toprows.push(getCellsInline(b++, null, first, change));
} else {
// equal
toprows.push(getCellsInline(b++, n++, first, change));
}
}
for (i = 0; i < toprows.length; i++) rows.push(toprows[i]);
for (i = 0; i < botrows.length; i++) rows.push(botrows[i]);
}
return rows;
},
getSideBySideDiffsArray: function (first, second) {
first = stringAsLines(first);
second = stringAsLines(second);
if (first == null) {
throw "Cannot build diff view, first text is not defined.";
}
if (second == null) {
throw "Cannot build diff view; newTextLines is not defined.";
}
var opcodes = (new SequenceMatcher(first, second)).get_opcodes();
var rows = [];
for (var idx = 0; idx < opcodes.length; idx++) {
var code = opcodes[idx];
var change = code[0];
var b = code[1];
var be = code[2];
var n = code[3];
var ne = code[4];
var rowcnt = Math.max(be - b, ne - n);
var toprows = [];
for (var i = 0; i < rowcnt; i++) {
toprows.push([getCells(b, be, first, change), getCells(n, ne, second, change)]);
b = getCellsIndex(b, be, first, change);
n = getCellsIndex(n, ne, second, change)
}
for (i = 0; i < toprows.length; i++) rows.push(toprows[i]);
}
return rows;
}
};