-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffIt.js
More file actions
52 lines (45 loc) · 1.28 KB
/
DiffIt.js
File metadata and controls
52 lines (45 loc) · 1.28 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
function tokenize(content, delimiter)
{
replaced = content.replace(/[\r\n]/g, '')
modified = replaced.split(delimiter || ' ').map(s => (s).trim());
console.log(modified)
return modified;
}
function tsv()
{
text = $("#TSV").val();
console.log(text)
text = text.replace(/[\r\n]/g, '')
textArr = text.split('\t');
console.log(textArr)
$("#leftContent").val(textArr[1]);
$("#rightContent").val(textArr[3]);
}
function addToDOM(symbol, content)
{
html = `<tr>
<th scope="row">${symbol}</th>
<td>${content}</td>
</tr>`;
$("#list").append(html);
}
function diffIt() {
$("#diffPane").hide()
$("#list").html("");
left = tokenize($("#leftContent").val(), $("#delimit").val())
right = tokenize($("#rightContent").val(), $("#delimit").val())
removed = left.filter(x => right.indexOf(x) < 0)
added = right.filter(x => left.indexOf(x) < 0)
merged = removed.map(x => ['-', x]).concat(added.map(x => ['+', x]));
console.log(merged);
merged.sort(function(a,b) {
if (a[1] === b[1]) {
return 0;
}
else {
return (a[1] < b[1]) ? -1 : 1;
}
});
merged.forEach(x => addToDOM(x[0], x[1]))
$("#diffPane").show()
}