-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
53 lines (46 loc) · 1.32 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
var JsDiff = require('diff');
module.exports = function(a, b, opts){
opts = opts || {};
var n_surrounding = opts.n_surrounding >= 0 ? opts.n_surrounding : -1;
var diffs = JsDiff.diffLines(a, b, {ignoreWhitespace: false});
var out = [];
var lines_with_change = [];
diffs.forEach(function(d){
var mod = d.removed && d.added ? '!' : (d.removed ? '-' : (d.added ? '+' : ' '));
var lines = d.value.split('\n');
if(lines.length > 0 && lines[lines.length - 1] === ''){
lines = lines.slice(0, lines.length - 1);
}
lines.forEach(function(line){
if(mod !== ' ' && n_surrounding >= 0){
lines_with_change.push(out.length);
}
out.push(mod + line);
});
});
if(n_surrounding >= 0){
var short_out = {};
lines_with_change.forEach(function(line_i){
var i, j;
for(i = -n_surrounding; i < n_surrounding + 1; i++){
j = line_i + i;
if(j >= 0 && j < out.length){
short_out[j] = out[j];
}
}
});
out = [];
var last_key;
var key;
for(key in short_out){
if(short_out.hasOwnProperty(key)){
if(last_key !== undefined && parseInt(key) !== (parseInt(last_key) + 1)){
out.push('@@');
}
out.push(short_out[key]);
last_key = key;
}
}
}
return out.join('\n');
};