Skip to content

Commit 73acbf5

Browse files
committed
update doc
1 parent 5900b67 commit 73acbf5

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

bin/text-file-diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ const lfd = new LargeFileDiff({
1818
});
1919

2020
lfd.on('+', line => {
21-
console.log('+' + line);
21+
console.log('+|' + line);
2222
});
2323

2424
lfd.on('-', line => {
25-
console.log('-' + line);
25+
console.log('-|' + line);
2626
});
2727
lfd.diff(file1, file2);

index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function defaultLineComparer(line1, line2) {
1515

1616
/**
1717
* custom line reader for better control of file
18-
* @param {[type]} file [description]
19-
* @return {[type]} [description]
18+
* @param String file path of file
19+
* @return Object custom linereader
2020
*/
2121
function myLineReader(file) {
2222
const rst = new ReadlinesSync(file);
@@ -40,10 +40,6 @@ function myLineReader(file) {
4040

4141
/**
4242
* 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
4743
*/
4844
class FileDiffLine extends EventEmitter {
4945
/**
@@ -66,6 +62,7 @@ class FileDiffLine extends EventEmitter {
6662
const lineReader1 = myLineReader(file1);
6763
const lineReader2 = myLineReader(file2);
6864
const compareFn = this.compareFn || defaultLineComparer;
65+
const charset = this.charset || 'utf8';
6966

7067
if (this.skipHeader) {
7168
lineReader1.moveNext();
@@ -75,8 +72,8 @@ class FileDiffLine extends EventEmitter {
7572
// while both files has valid val
7673
while (lineReader1.val || lineReader2.val) {
7774
// foreach line of file1, compare each line of file2
78-
const line1 = lineReader1.val.toString('utf8');
79-
const line2 = lineReader2.val.toString('utf8');
75+
const line1 = lineReader1.val.toString(charset);
76+
const line2 = lineReader2.val.toString(charset);
8077
const cmp = compareFn(line1, line2);
8178

8279
// emit on compared

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "text-file-diff",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "line by line diff of two large files",
55
"license": "MIT",
66
"repository": "niiknow/text-file-diff",

readme.md

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
[![Build Status](https://travis-ci.org/niiknow/text-file-diff.svg?branch=master)](https://travis-ci.org/niiknow/text-file-diff)
22
# text-file-diff
3-
> line by line diff of two large text files
3+
> line by line diff of two large (sorted) text files
44
5-
You need a nodejs tool to diff two large text files. This is also useful with csv files:
5+
This is especially useful with csv/tsv/psv files:
66
- compare products file for changes to import
77
- compare log files
8-
- compare database of employee/users
8+
- compare database export of large organization employee/users
99

1010
## NOTE
1111

12-
This script expect input of two `sorted` text files. If the file is not sorted, the unix `sort` command may be of help: https://en.wikipedia.org/wiki/Sort_(Unix)
12+
This script expect input of two `sorted` text files. If the files are not sorted, the unix `sort` command may be of help: https://en.wikipedia.org/wiki/Sort_(Unix)
13+
14+
> ForEach line in File1, compare to line in File2
15+
> equal: incr both files to next line
16+
> line1 > line2: new line detected, incr File2 to next line
17+
> line1 < line2: deleted line, incr File1 to next line
18+
19+
Since the list will be `sorted`, the performance of this script is expected to be approximately `O(|A| log |A| + |B| log |B|)`, where A is File1 and B is File2.
1320

1421
## Install
1522

@@ -19,24 +26,28 @@ $ npm install text-file-diff
1926

2027
## Usage
2128
```js
22-
import TextFileDiff from 'text-file-diff';
23-
const m = new TextFileDiff();
29+
import TextFileDiff from 'text-file-diff';
30+
const m = new TextFileDiff();
31+
m.on('compared', (line1, line2, compareResult, lineReader1, lineReader2) => {
32+
// event triggered immediately after line comparison
33+
// but before +- event
34+
});
2435

25-
m.on('-', line => {
36+
m.on('-', line => {
2637
// when a line is in file1 but not in file2
2738
});
2839

29-
m.on('+', line => {
40+
m.on('+', line => {
3041
// when a line is in file2 but not in file1
3142
});
3243

33-
// run the diff
34-
m.diff('tests/file1.txt', 'tests/file2.txt');
44+
// run the diff
45+
m.diff('tests/file1.txt', 'tests/file2.txt');
3546
```
3647

3748
## Example
3849
```bash
39-
$ ./bin/text-file-diff tests/file1.txt tests/file2.txt | sed 's/^\(.\{1\}\)/\1|/'
50+
$ ./bin/text-file-diff tests/file1.txt tests/file2.txt
4051
```
4152

4253
## Point of Interest

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava';
22
import TextFileDiff from '.';
33

4-
test('test with headers', t => {
4+
test('test with header', t => {
55
const m = new TextFileDiff();
66
const expected = '+Additional\n+Another\n+Lines\n-Some\n-Simple\n+With\n';
77

0 commit comments

Comments
 (0)