forked from bertspaan/node-diff-utility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff2js.js
66 lines (53 loc) · 1.26 KB
/
diff2js.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
var through = require('through2');
function transform(line, enc, cb) {
var match = this._reChangeCommand.exec(line);
if (match) {
this._lastLineOut = parseInt(match[1]);
this._lastLineIn = parseInt(match[3]);
this._lastChange = this._changes[match[2]];
} else {
var first = line.charAt(0);
if (first === '<') {
push(this, {
change: this._lastChange,
line: this._lastLineOut,
type: 'out',
str: line.substring(2)
});
this._lastLineOut += 1;
} else if (first === '>') {
push(this, {
change: this._lastChange,
line: this._lastLineIn,
type: 'in',
str: line.substring(2)
});
this._lastLineIn += 1;
}
}
cb();
}
function flush(cb) {
cb();
}
function push(self, val) {
if (val !== undefined) {
self.push(val);
}
}
function diff2js() {
var stream = through({}, transform, flush);
stream._writableState.objectMode = true;
stream._readableState.objectMode = true;
stream._lastLineIn = 0;
stream._lastLineOut = 0;
stream._lastChange = null;
stream._reChangeCommand = /^(\d+)(?:\,\d+)?([a,c,d])(\d+)(?:\,\d+)?/;
stream._changes = {
a: 'add',
c: 'change',
d: 'delete'
};
return stream;
}
module.exports = diff2js;