From dd2440db9f33165266da8475d95786481249d624 Mon Sep 17 00:00:00 2001 From: grabowski Date: Mon, 29 Jun 2026 16:58:05 +0700 Subject: [PATCH] Fix value parser (Hz vs Henry), accept uF/uH, redirect to parts list after create MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Drop /i flag: m (milli) vs M (mega) are now case-sensitive - Put Hz before H in unit alternation so kHz isn't parsed as kH (kiloHenry) - u already accepted as micro alongside µ; unit suffix optional (100u, 10k work) - After adding a part, redirect to /parts overview instead of detail page Co-Authored-By: Claude Sonnet 4.6 (1M context) --- src/lib/utils/valueParser.ts | 9 ++++++--- src/routes/(app)/parts/new/+page.server.ts | 7 +++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lib/utils/valueParser.ts b/src/lib/utils/valueParser.ts index 3b8fcf5..389f3ec 100644 --- a/src/lib/utils/valueParser.ts +++ b/src/lib/utils/valueParser.ts @@ -11,15 +11,18 @@ const PREFIX: Record = { }; // Parses a human value string into a base-SI number (Farads, Ohms, Henrys, Hz). +// - u and µ are both accepted as micro (1e-6) +// - Hz must come before H in the alternation to avoid "kHz" being parsed as kH (kiloHenry) +// - No /i flag: m (milli) and M (mega) are case-sensitive +// - Unit is optional: "10k" and "100u" parse correctly without a trailing letter // Returns null if unparseable (e.g. part codes like "BC547", "1N4148"). export function parseValueNumeric(value: string | null | undefined): number | null { if (!value) return null; const s = value.trim(); - // Match: digits, optional decimal, optional SI prefix, optional unit letters - // Handles: "100µF", "10kΩ", "4.7uH", "32.768kHz", "47R", "1M", "220" - const match = s.match(/^([0-9]+(?:\.[0-9]+)?)\s*([pnuµmkKMG]?)\s*([FHhzΩRω]|Hz|hz|Ohm|ohm)?/i); + // Match: number + optional SI prefix + optional unit (Hz before H to avoid early match) + const match = s.match(/^(\d+(?:\.\d+)?)\s*([pnuµmkKMG]?)\s*(Hz|Ohm|[FHΩRf])?/); if (!match) return null; const num = parseFloat(match[1]); diff --git a/src/routes/(app)/parts/new/+page.server.ts b/src/routes/(app)/parts/new/+page.server.ts index 6130c1b..b4a7631 100644 --- a/src/routes/(app)/parts/new/+page.server.ts +++ b/src/routes/(app)/parts/new/+page.server.ts @@ -41,7 +41,7 @@ export const actions: Actions = { const data = result.data; - const [part] = await db + await db .insert(parts) .values({ name: data.name || null, @@ -58,9 +58,8 @@ export const actions: Actions = { unit: data.unit, locationId: data.locationId || null, notes: data.notes || null - }) - .returning({ id: parts.id }); + }); - redirect(303, `/parts/${part!.id}`); + redirect(303, '/parts'); } };