Skip to content

Commit 1d5404d

Browse files
committed
first blood
0 parents  commit 1d5404d

12 files changed

+281
-0
lines changed

Diff for: .gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* text=auto
2+
*.js text eol=lf

Diff for: .gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
yarn.lock
3+
.nyc_output
4+
coverage
5+
.DS_Store

Diff for: .npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

Diff for: .travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: node_js
2+
node_js:
3+
- '8'
4+
- '6'

Diff for: LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Diff for: bin/large-file-diff

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env node
2+
3+
const yargs = require('yargs');
4+
const LargeFileDiff = require('../index.js');
5+
6+
const argv = yargs
7+
.usage('$0 file1 file2')
8+
.help('help').alias('help', 'h').describe('h', 'Show help.')
9+
.epilog('Home page and docs: https://github.com/niiknow/large-file-diff')
10+
.demand(2)
11+
.argv;
12+
13+
const file1 = argv._[0];
14+
const file2 = argv._[1];
15+
16+
const lfd = new LargeFileDiff({
17+
skipHeader: argv.skipHeader
18+
});
19+
20+
lfd.on('+', line => {
21+
console.log('+' + line);
22+
});
23+
24+
lfd.on('-', line => {
25+
console.log('-' + line);
26+
});
27+
lfd.diff(file1, file2);

Diff for: index.js

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const EventEmitter = require('events');
2+
const ReadlinesSync = require('n-readlines');
3+
4+
/**
5+
* default line comparer
6+
* @param String line1
7+
* @param String line2
8+
* @return Number 0 for equals, 1 if line1 > line2 or -1
9+
*/
10+
function defaultLineComparer(line1, line2) {
11+
line1 = String(line1).trim();
12+
line2 = String(line2).trim();
13+
return line1 > line2 ? 1 : (line1 < line2 ? -1 : 0);
14+
}
15+
16+
/**
17+
* custom line reader for better control of file
18+
* @param {[type]} file [description]
19+
* @return {[type]} [description]
20+
*/
21+
function myLineReader(file) {
22+
const rst = new ReadlinesSync(file);
23+
rst.val = null;
24+
rst.nextVal = null;
25+
rst.lineNumber = -1;
26+
27+
rst.moveNext = () => {
28+
rst.val = rst.nextVal;
29+
rst.nextVal = rst.next();
30+
rst.lineNumber++;
31+
return rst.val;
32+
};
33+
34+
// set current and next val
35+
rst.moveNext();
36+
rst.moveNext();
37+
38+
return rst;
39+
}
40+
41+
/**
42+
* line by line diff of two files
43+
* 1. foreach line of file1, compare each line of file2
44+
* equal: incr both files to next line
45+
* line1 > line2: must be new line in file2, inc file2 to next line
46+
* line1 < line2: must be deleted line, inc file1 to next line
47+
*/
48+
class FileDiffLine extends EventEmitter {
49+
/**
50+
* initialize FileDiff
51+
* @param {[type]} options [description]
52+
* @return {[type]} [description]
53+
*/
54+
constructor(options) {
55+
super();
56+
Object.assign(this, options);
57+
}
58+
59+
/**
60+
* run diff
61+
* @param String file1 path to file 1
62+
* @param String file2 path to file 2
63+
* @return Object this
64+
*/
65+
diff(file1, file2) {
66+
const lineReader1 = myLineReader(file1);
67+
const lineReader2 = myLineReader(file2);
68+
const compareFn = this.compareFn || defaultLineComparer;
69+
70+
if (this.skipHeader) {
71+
lineReader1.moveNext();
72+
lineReader2.moveNext();
73+
}
74+
75+
// while both files has valid val
76+
while (lineReader1.val || lineReader2.val) {
77+
// foreach line of file1, compare each line of file2
78+
const line1 = lineReader1.val.toString('utf8');
79+
const line2 = lineReader2.val.toString('utf8');
80+
const cmp = compareFn(line1, line2);
81+
82+
// equals: so both inc both lines position
83+
if (cmp === 0) {
84+
lineReader1.moveNext();
85+
lineReader2.moveNext();
86+
} else if (cmp > 0) {
87+
// line1 > line2: must be new line in file2
88+
if (cmp === 1) {
89+
this.emit('+', line2, lineReader1, lineReader2);
90+
}
91+
92+
// inc file2 to next line
93+
lineReader2.moveNext();
94+
} else if (cmp < 0) {
95+
// line1 < line2: must be new line in file2
96+
if (cmp === -1) {
97+
this.emit('-', line1, lineReader1, lineReader2);
98+
}
99+
100+
// inc file1 to next line
101+
lineReader1.moveNext();
102+
}
103+
}
104+
105+
return this;
106+
}
107+
}
108+
109+
module.exports = FileDiffLine;

Diff for: package.json

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "large-file-diff",
3+
"version": "1.0.0",
4+
"description": "line by line diff of two large files",
5+
"license": "MIT",
6+
"repository": "niiknow/large-file-diff",
7+
"author": {
8+
"name": "noogen",
9+
"email": "[email protected]",
10+
"url": "niiknow.org"
11+
},
12+
"engines": {
13+
"node": ">=6"
14+
},
15+
"scripts": {
16+
"test": "xo && nyc ava",
17+
"report": "nyc report --reporter=html"
18+
},
19+
"bin": {
20+
"large-file-diff": "./bin/large-file-diff"
21+
},
22+
"files": [
23+
"index.js"
24+
],
25+
"keywords": [
26+
""
27+
],
28+
"dependencies": {
29+
"n-readlines": "^0.2.8",
30+
"yargs": "^9.0.1"
31+
},
32+
"devDependencies": {
33+
"ava": "^0.20.0",
34+
"nyc": "^11.0.0",
35+
"xo": "^0.18.2"
36+
},
37+
"xo": {
38+
"semicolon": true,
39+
"prefer-const": false,
40+
"space": 2,
41+
"rules": {
42+
"object-shorthand": [
43+
0,
44+
"consistent"
45+
],
46+
"capitalized-comments": [
47+
"error",
48+
"never"
49+
],
50+
"ava/prefer-async-await": 0
51+
}
52+
},
53+
"ava": {
54+
"failWithoutAssertions": true
55+
}
56+
}

Diff for: readme.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# large-file-diff
2+
> line by line diff or two large files
3+
4+
## Install
5+
6+
```
7+
$ npm install large-file-diff
8+
```
9+
10+
## Preview
11+
```
12+
./bin/large-file-diff tests/file1.txt tests/file2.txt
13+
```
14+
15+
## Point of Interest
16+
17+
Alternatively, you can accomplish this in shell as:
18+
```
19+
diff -u file1.txt file2.txt | sed -n '1,2d;/^[-+|]/p' | sed 's/^\(.\{1\}\)/\1|/'
20+
```
21+
22+
## MIT

Diff for: test.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import test from 'ava';
2+
import LargeFileDiff from '.';
3+
4+
test('test with headers', t => {
5+
const m = new LargeFileDiff();
6+
const expected = '+Additional\n+Another\n+Lines\n-Some\n-Simple\n+With\n';
7+
8+
let actual = '';
9+
m.on('-', line => {
10+
actual += '-' + line + '\n';
11+
});
12+
13+
m.on('+', line => {
14+
actual += '+' + line + '\n';
15+
});
16+
m.diff('tests/file1.txt', 'tests/file2.txt');
17+
t.is(actual, expected);
18+
});
19+
20+
test('test skip header', t => {
21+
const expected = '+Another\n+File\n+Lines\n-Some\n-Simple\n+With\n';
22+
23+
const m = new LargeFileDiff({
24+
skipHeader: true
25+
});
26+
let actual = '';
27+
m.on('-', line => {
28+
actual += '-' + line + '\n';
29+
});
30+
31+
m.on('+', line => {
32+
actual += '+' + line + '\n';
33+
});
34+
m.diff('tests/file1.txt', 'tests/file2.txt');
35+
t.is(actual, expected);
36+
});

Diff for: tests/file1.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
File
2+
Some
3+
Simple
4+
Text

Diff for: tests/file2.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Additional
2+
Another
3+
File
4+
Lines
5+
Text
6+
With

0 commit comments

Comments
 (0)