forked from AssemblyScript/assemblyscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.js
More file actions
30 lines (26 loc) · 837 Bytes
/
diff.js
File metadata and controls
30 lines (26 loc) · 837 Bytes
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
var JsDiff = require("diff");
var colors = require("../../cli/util/colors");
module.exports = function diff(filename, expected, actual) {
var diff = JsDiff.structuredPatch(filename, filename, expected, actual, "expected", "actual", { context: 5 });
if (!diff.hunks.length)
return null;
var ret = [];
ret.push('--- ' + diff.oldHeader);
ret.push('+++ ' + diff.newHeader);
for (var i = 0; i < diff.hunks.length; i++) {
var hunk = diff.hunks[i];
ret.push(
'@@ -' + hunk.oldStart + ',' + hunk.oldLines
+ ' +' + hunk.newStart + ',' + hunk.newLines
+ ' @@'
);
ret.push.apply(ret, hunk.lines.map(line =>
line.charAt(0) === "+"
? colors.green(line)
: line.charAt(0) === "-"
? line = colors.red(line)
: line
));
}
return ret.join('\n') + '\n';
};