Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make handlers await async #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ yarn.lock
.nyc_output
coverage
.DS_Store
dist/
4 changes: 2 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference types="node" />
import { EventEmitter } from 'events';
import { EventEmitter2 } from 'eventemitter2';
import { TextFileDiffOption } from './types';
import stream from 'stream';
export declare class StreamLineReader {
Expand All @@ -14,7 +14,7 @@ export declare class StreamLineReader {
/**
* line by line diff of two files
*/
export default class TextFileDiff extends EventEmitter {
export default class TextFileDiff extends EventEmitter2 {
options: TextFileDiffOption;
constructor(options?: TextFileDiffOption);
/**
Expand Down
30 changes: 14 additions & 16 deletions dist/index.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"npm": ">=6"
},
"scripts": {
"build": "tsc",
"prepare": "mkdir -p dist/ && tsc",
"test": "tsc && cross-env DEBUG=text-file-diff xo && cross-env DEBUG=text-file-diff nyc ava",
"report": "tsc && cross-env DEBUG=text-file-diff nyc report --reporter=html"
},
Expand Down Expand Up @@ -57,5 +57,8 @@
},
"ava": {
"failWithoutAssertions": true
},
"dependencies": {
"eventemitter2": "^6.4.4"
}
}
32 changes: 17 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {EventEmitter} from 'events';
import {EventEmitter2} from 'eventemitter2';
import {TextFileDiffOption} from './types';
import {PathLike, createReadStream} from 'fs';
import {Interface, createInterface} from 'readline';
import {createReadStream} from 'fs';
import {createInterface} from 'readline';
import stream from 'stream';

// import myDebug = require('debug');
Expand Down Expand Up @@ -46,7 +46,7 @@ export class StreamLineReader {
/**
* line by line diff of two files
*/
export default class TextFileDiff extends EventEmitter {
export default class TextFileDiff extends EventEmitter2 {
options: TextFileDiffOption;

constructor(options?: TextFileDiffOption) {
Expand Down Expand Up @@ -101,29 +101,31 @@ export default class TextFileDiff extends EventEmitter {
// debug(line1, line1, cmpar);
// debug(lineReader1.nextValue, lineReader2.nextValue, 'next', lineReader1.eof, lineReader2.eof);
// emit on compared
this.emit('compared', line1, line2, cmpar, lineReader1, lineReader2);
await this.emitAsync('compared', line1, line2, cmpar, lineReader1, lineReader2);

if (cmpar > 0) {
// line1 > line2: new line detected
// if file2 ended before file1, then file2 lost line1
// else file2 has new line
if (lineReader2.eof > lineReader1.eof) {
this.emit('-', line1, lineReader1, lineReader2);
} else {
this.emit('+', line2, lineReader1, lineReader2);
}

/* eslint-disable @typescript-eslint/no-unused-expressions */
lineReader2.eof > lineReader1.eof ?
await this.emitAsync('-', line1, lineReader1, lineReader2) :
await this.emitAsync('+', line2, lineReader1, lineReader2);
/* eslint-enable @typescript-eslint/no-unused-expressions */

// incr File2 to next line
await lineReader2.moveNext();
} else if (cmpar < 0) {
// line1 < line2: deleted line
// if file1 ended before file2, then file2 has new line
// else file1 lost a line
if (lineReader1.eof > lineReader2.eof) {
this.emit('+', line2, lineReader1, lineReader2);
} else {
this.emit('-', line1, lineReader1, lineReader2);
}

/* eslint-disable @typescript-eslint/no-unused-expressions */
lineReader1.eof > lineReader2.eof ?
await this.emitAsync('+', line2, lineReader1, lineReader2) :
await this.emitAsync('-', line1, lineReader1, lineReader2);
/* eslint-enable @typescript-eslint/no-unused-expressions */

// incr File1 to next line
await lineReader1.moveNext();
Expand Down
23 changes: 23 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@ test('test skip header', async t => {
t.is(actual, expected);
});

test('test await listener', async t => {
const m = new TextFileDiff();
const expected = 'compared+compared+comparedcompared+compared-compared-comparedcompared+';

let actual = '';

const getListenerFor = event => async () => new Promise(resolve => {
setTimeout(() => {
actual += event;
resolve();
}, 200);
});

m.on('-', getListenerFor('-'));
m.on('+', getListenerFor('+'));

m.on('compared', getListenerFor('compared'));

await m.diff('tests/file1.txt', 'tests/file2.txt');

t.is(actual, expected);
});

test('test against null or empty file', async t => {
let expected = '-some,csv,data\n';

Expand Down