Skip to content

Commit

Permalink
refactor!: next() should return Buffer | null
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelovers committed Nov 2, 2022
1 parent 0e25536 commit 57d4305
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ export class LineByLine {
}

/**
* Returns buffer with the line data without the `newLineCharacter` or `false` if end of file is reached.
* Returns buffer with the line data without the `newLineCharacter` or `null` if end of file is reached.
*/
next() {
if (this.fd === null) return false;
next(): Buffer | null {
if (this.fd === null) return null as null;

let line: Buffer = false as any;
let line: Buffer = null as null;

if (this.eofReached && this.linesCache.length === 0) {
this.close();
Expand Down
22 changes: 11 additions & 11 deletions test/readlines.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ test('should get all lines', () => {

expect(liner.next().toString('ascii')).toBe('hello');
expect(liner.next().toString('ascii')).toBe('hello2');
expect(liner.next()).toBe(false);
expect(liner.next()).toBe(false);
expect(liner.next()).toBe(null);
expect(liner.next()).toBe(null);
expect(liner.fd).toBe(null);
});

Expand All @@ -19,22 +19,22 @@ test('should get all lines even if the file doesnt end with new line', () => {

expect(liner.next().toString('ascii')).toBe('google.com');
expect(liner.next().toString('ascii')).toBe('yahoo.com');
expect(liner.next()).toBe(false);
expect(liner.next()).toBe(null);
expect(liner.fd).toBe(null);
});

test('should get all lines if there is no new lines', () => {
const liner = new lineByLine(resolve(__dirname, 'fixtures/noNewLinesFile.txt'));

expect(liner.next().toString('ascii')).toBe('no new line');
expect(liner.next()).toBe(false);
expect(liner.next()).toBe(null);
expect(liner.fd).toBe(null);
});

test('should handle empty files', () => {
const liner = new lineByLine(resolve(__dirname, 'fixtures/emptyFile.txt'));

expect(liner.next()).toBe(false);
expect(liner.next()).toBe(null);
expect(liner.fd).toBe(null);
});

Expand All @@ -46,7 +46,7 @@ test('should read right between two chunks', () => {
expect(liner.next().toString('ascii')).toBe('google.com');
expect(liner.next().toString('ascii')).toBe('yahoo.com');
expect(liner.next().toString('ascii')).toBe('yandex.ru');
expect(liner.next()).toBe(false);
expect(liner.next()).toBe(null);
expect(liner.fd).toBe(null);
});

Expand All @@ -58,7 +58,7 @@ test('should read empty lines', () => {
expect(liner.next().toString('ascii')).toBe('');
expect(liner.next().toString('ascii')).toBe('hello2');
expect(liner.next().toString('ascii')).toBe('hello3');
expect(liner.next()).toBe(false);
expect(liner.next()).toBe(null);
expect(liner.fd).toBe(null);
});

Expand All @@ -75,7 +75,7 @@ test('should reset and start from the beginning', () => {
expect(liner.next().toString('ascii')).toBe('google.com');
expect(liner.next().toString('ascii')).toBe('yahoo.com');
expect(liner.next().toString('ascii')).toBe('yandex.ru');
expect(liner.next()).toBe(false);
expect(liner.next()).toBe(null);
expect(liner.fd).toBe(null);
});

Expand All @@ -94,7 +94,7 @@ test('should read big lines', () => {
expect(JSON.parse(liner.next() as any)).toMatchObject(expected);
expect(JSON.parse(liner.next() as any)).toMatchObject(expected);

expect(liner.next()).toBe(false);
expect(liner.next()).toBe(null);
expect(liner.fd).toBe(null);
});

Expand All @@ -114,7 +114,7 @@ test('Manually Close', () => {
liner.close();
expect(liner.fd).toBe(null);

expect(liner.next()).toBe(false);
expect(liner.next()).toBe(null);
});

test('should correctly processes NULL character in lines', () => {
Expand All @@ -124,6 +124,6 @@ test('should correctly processes NULL character in lines', () => {
expect(liner.next().toString()).toBe('line wi'+String.fromCharCode(0)+'th null');
expect(liner.next().toString()).toBe('another line without null');

expect(liner.next()).toBe(false);
expect(liner.next()).toBe(null);
expect(liner.fd).toBe(null);
})

0 comments on commit 57d4305

Please sign in to comment.