Skip to content

Commit 3615c35

Browse files
authored
fix(lines): toLine is exclusive (#20)
1 parent 4fba77f commit 3615c35

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

reader.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,9 @@ export class CSVReader {
319319
continue;
320320
}
321321

322+
// linesProcessed start at 1 and toLine at 0
322323
// stop reading if toLine is reached
323-
if (!this.inColumn && this.linesProcessed > this.toLine) {
324+
if (!this.inColumn && this.linesProcessed >= this.toLine) {
324325
this.debug("eof");
325326
this.onEnd();
326327
return;

reader_test.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,49 @@ g,h`,
361361
);
362362

363363
const rows = await asyncArrayFrom(
364-
readCSVRows(reader, { fromLine: 1, toLine: 2 }),
364+
readCSVRows(reader, { fromLine: 1, toLine: 3 }),
365365
);
366366

367367
assertEquals(rows, [["c", "d"], ["e", "f"]]);
368368
},
369369
});
370+
371+
Deno.test({
372+
name: "readCSVRows options.toLine should be exclusive",
373+
async fn() {
374+
const reader = new MyReader(
375+
`a,b
376+
c,d
377+
e,f
378+
g,h`,
379+
);
380+
381+
const rows = await asyncArrayFrom(
382+
readCSVRows(reader, { fromLine: 1, toLine: 2 }),
383+
);
384+
385+
assertEquals(rows, [["c", "d"]]);
386+
},
387+
});
388+
389+
Deno.test({
390+
name: "readCSVRows can read only the first line",
391+
async fn() {
392+
const reader = new MyReader(
393+
`1,2,3
394+
a,b,c
395+
!,@,#`,
396+
);
397+
398+
const rows = await asyncArrayFrom(
399+
readCSVRows(reader, {
400+
fromLine: 0,
401+
toLine: 1,
402+
}),
403+
);
404+
405+
assertEquals(rows, [
406+
["1", "2", "3"],
407+
]);
408+
},
409+
});

0 commit comments

Comments
 (0)