Skip to content

Commit 2ef141f

Browse files
committed
bug: fix bug in BufferedFormatReader
the undefined state of peekedRow was conflicting with the fact the readRow might return undefined.
1 parent 097a56d commit 2ef141f

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tabular-data-differ",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "A very efficient library for diffing two sorted streams of tabular data, such as CSV files.",
55
"keywords": [
66
"table",

src/differ.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,44 @@ describe('differ', () => {
17741774
same: 5
17751775
});
17761776
});
1777+
test('should work with real source files (CSV) and support duplicate key handling', async () => {
1778+
const output = new FakeFormatWriter();
1779+
const differ = diff({
1780+
oldSource: './tests/a2.csv',
1781+
newSource: './tests/b2.csv',
1782+
keys: ['id'],
1783+
duplicateKeyHandling: 'keepFirstRow',
1784+
});
1785+
await differ.to({
1786+
destination: {
1787+
format: 'custom',
1788+
writer: output,
1789+
}
1790+
});
1791+
expect(output.header?.columns).toEqual([ 'id', 'a', 'b', 'c' ]);
1792+
expect(output.diffs).toEqual([
1793+
{ delta: -1, status: 'deleted', oldRow: [ '01', 'a1', 'b1', 'c1' ] },
1794+
{
1795+
delta: 0,
1796+
status: 'modified',
1797+
oldRow: [ '04', 'a4', 'b4', 'c4' ],
1798+
newRow: [ '04', 'aa4', 'bb4', 'cc4' ]
1799+
},
1800+
{ delta: -1, status: 'deleted', oldRow: [ '05', 'a5', 'b5', 'c5' ] },
1801+
{ delta: -1, status: 'deleted', oldRow: [ '06', 'a6', 'b6', 'c6' ] },
1802+
{ delta: 1, status: 'added', newRow: [ '10', 'a10', 'b10', 'c10' ] },
1803+
]);
1804+
console.log(output.footer?.stats);
1805+
expect(output.footer?.stats).toEqual({
1806+
totalComparisons: 10,
1807+
totalChanges: 5,
1808+
changePercent: 50,
1809+
added: 1,
1810+
deleted: 3,
1811+
modified: 1,
1812+
same: 5
1813+
});
1814+
});
17771815
test('should work with http streams (CSV)', async () => {
17781816
const currentDir = process.cwd().replaceAll('\\', '/');
17791817
const stats = await diff({

src/formats.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,13 @@ export type FormatWriterFactory = (options: FormatWriterOptions) => FormatWriter
127127

128128
export class BufferedFormatReader implements FormatReader {
129129
private peekedRow: Row | undefined;
130+
private hasPeekedRow = false;
130131

131132
constructor(private reader: FormatReader) {}
132133

133134
open(): Promise<void> {
135+
this.hasPeekedRow = false;
136+
this.peekedRow = undefined;
134137
return this.reader.open();
135138
}
136139

@@ -139,17 +142,19 @@ export class BufferedFormatReader implements FormatReader {
139142
}
140143

141144
async peekRow(): Promise<Row | undefined> {
142-
if (this.peekedRow) {
145+
if (this.hasPeekedRow) {
143146
return this.peekedRow;
144147
}
145148
this.peekedRow = await this.reader.readRow();
149+
this.hasPeekedRow = true;
146150
return this.peekedRow;
147151
}
148152

149153
async readRow(): Promise<Row | undefined> {
150-
if (this.peekedRow) {
154+
if (this.hasPeekedRow) {
151155
const result = this.peekedRow;
152156
this.peekedRow = undefined;
157+
this.hasPeekedRow = false;
153158
return result;
154159
}
155160
return await this.reader.readRow();

tests/a2.csv

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
id,a,b,c
2+
01,a1,b1,c1
3+
02,a2,b2,c2
4+
03,a3,b3,c3
5+
03,a3,b3,c3
6+
04,a4,b4,c4
7+
05,a5,b5,c5
8+
06,a6,b6,c6
9+
07,a7,b7,c7
10+
07,a77,b77,c77
11+
08,a8,b8,c8
12+
09,a9,b9,c9
13+
09,a9,b9,c9

tests/b2.csv

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id,a,b,c
2+
02,a2,b2,c2
3+
03,a3,b3,c3
4+
03,a3,b3,c3
5+
03,a3,b3,c3
6+
04,aa4,bb4,cc4
7+
07,a7,b7,c7
8+
07,a7b,b7b,c7b
9+
08,a8,b8,c8
10+
09,a9,b9,c9
11+
10,a10,b10,c10

0 commit comments

Comments
 (0)