Files
token-circles/shared/importRowLabel.ts
KomediruzeckiandGitHub 302f25453e fix(import): stop dropping rows that have no description (#392)
A Google Sheet sync rejected 479 of 5,194 rows. Replaying the sheet through the
real import path reproduced the reported rows exactly and pinned down three
defects; two more were found reviewing the fix.

1. A blank description dropped the row, and only locally. The IndexedDB path
   required a description; the Worker never has, so the same sheet imported
   clean in the cloud and silently lost 291 rows in local mode. Those rows
   carry a date, an amount, a category and both accounts — a missing
   description is not a reason to discard one. The message was wrong twice
   over: description is not a number, and it named a field the numeric parser
   never validates. A Worker test now pins that side of the contract.

2. The rejection label was cut mid-token. importRowLabel sliced the date to ten
   characters to drop a trailing time, but the frontend reads sheets through
   the Google Visualization API, which delivers `Date(2026,1,7)` — sliced to
   `Date(2026,`. It now trims only when the value leads with a calendar date,
   and the local path labels from the normalized date.

3. One CSV parser instead of two. Both runtimes split the text into lines
   before tracking quotes, so a quoted cell containing a newline — how Sheets
   exports every multi-line cell — was torn into fragments: the real row
   vanished and short, column-shifted junk rows took its place, silently. The
   parser now carries quote state across newlines, handles the RFC 4180 `""`
   escape the old copies dropped, and lives in shared/ so the two cannot drift.

4. A byte-order mark ahead of a quoted header left the header as `"description"`
   with the quotes attached, which silently unmaps a saved header→column
   mapping. Unquoted headers survived by accident, since trim() drops U+FEFF.

5. Padding before an opening quote (`a, "b"`) is accepted again, as the parsers
   this replaced did — the Worker ingests uploaded and emailed CSVs from
   arbitrary producers.

A missing date still rejects the row, now saying "No date — every transaction
needs one" rather than blaming the number format.
2026-08-09 01:51:19 +02:00

42 lines
1.7 KiB
TypeScript

/**
* Human identifier for a spreadsheet row rejected during import.
*
* A rejection used to be reported as a bare row number ("Row 285: …"), which meant scrolling a
* 4,000-line sheet and counting — and the count is off by the header row, so it often landed on
* the wrong line. Quoting the row's own date and description lets the user find it with a search
* instead. Shared so the Worker and the IndexedDB import path label rows identically.
*/
const MAX_DESCRIPTION = 60;
const MAX_DATE = 24;
// A leading calendar date, ISO or day/month/year, with or without a time after it.
const LEADING_DATE = /^(\d{4}-\d{1,2}-\d{1,2}|\d{1,2}[/.-]\d{1,2}[/.-]\d{4})/;
function text(value: unknown): string {
if (value === null || value === undefined) return '';
return String(value).trim();
}
/**
* Trim a date cell down to its calendar day — imported sheets often carry a time or a full
* timestamp. Only trims when the value actually LEADS with a date: an unconditional slice(0, 10)
* cut unrecognised values mid-token, which is how a Google Visualization date reached the user
* as `Date(2026,` instead of `Date(2026,1,7)`.
*/
function dayOf(value: unknown): string {
const raw = text(value);
const match = raw.match(LEADING_DATE);
if (match) return match[0];
return raw.length > MAX_DATE ? `${raw.slice(0, MAX_DATE - 1)}…` : raw;
}
/** `2026-05-04 · Top-up by *1111`, or as much of it as the row actually has. */
export function importRowLabel(date: unknown, description: unknown): string {
const day = dayOf(date);
const what = text(description);
const trimmed = what.length > MAX_DESCRIPTION ? `${what.slice(0, MAX_DESCRIPTION - 1)}…` : what;
if (day && trimmed) return `${day} · ${trimmed}`;
return day || trimmed;
}