Skip to content

Commit 85736e7

Browse files
committed
improve row number estimation + ignore partial first row + give space to the row number
1 parent a5bf4b9 commit 85736e7

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/csv.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,10 @@ function findParsedRow({ cache, row }: { cache: Cache; row: number }):
395395
...serialParsedRow,
396396
};
397397
}
398-
// TODO(SL): we know the index of the last row in the serial range, we can be more precise
399-
const estimatedStart = cache.header.bytes + row * cache.averageRowBytes;
398+
const estimatedStart =
399+
cache.header.bytes +
400+
cache.serial.end +
401+
(row - cache.serial.validRows.length) * cache.averageRowBytes;
400402
// find the range containing this row
401403
const range = cache.random.find(
402404
(r) => estimatedStart >= r.start && estimatedStart < r.end
@@ -466,6 +468,7 @@ function fetchRange({
466468
checkSignal(signal);
467469

468470
let cursor = start;
471+
let isFirstStep = true;
469472

470473
return new Promise<void>((resolve, reject) => {
471474
Papa.parse<string[]>(cache.url, {
@@ -503,7 +506,7 @@ function fetchRange({
503506
}
504507

505508
// add the row to the cache
506-
if (addParsedRowToCache({ cache, parsedRow })) {
509+
if (addParsedRowToCache({ cache, parsedRow, isFirstStep })) {
507510
// send an event for the new row
508511
eventTarget.dispatchEvent(new CustomEvent("resolve"));
509512
}
@@ -513,6 +516,8 @@ function fetchRange({
513516
parser.abort();
514517
return;
515518
}
519+
520+
isFirstStep = false;
516521
},
517522
complete: () => {
518523
resolve();
@@ -531,11 +536,16 @@ function isEmpty(data: string[]): boolean {
531536
function addParsedRowToCache({
532537
cache,
533538
parsedRow,
539+
isFirstStep,
534540
}: {
535541
cache: Cache;
536542
parsedRow: ParsedRow;
543+
isFirstStep: boolean; // to handle the case where we start in the middle of a row
537544
}): boolean {
538-
console.debug({ cache, parsedRow });
545+
if (isFirstStep && parsedRow.data.length < cache.header.data.length) {
546+
// the first parsed row is partial, we ignore it, it must be part of the previous row
547+
return false;
548+
}
539549

540550
// TODO(SL): optimize
541551
const inserted = !isEmpty(parsedRow.data);

src/styles/hightable.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
tbody {
2222
[role="rowheader"] {
2323
text-align: right;
24-
padding-right: 8px;
24+
padding-right: 2px;
2525
font-size: 0.625rem; /* column width computation is not optimal in hightable, reducing the font size to avoid cutting the numbers */
2626
font-family: var(--code-font-family);
2727
}

0 commit comments

Comments
 (0)