Fix value parser (Hz vs Henry), accept uF/uH, redirect to parts list after create
Deploy to LXC / deploy (push) Failing after 13m21s
Deploy to LXC / deploy (push) Failing after 13m21s
- 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) <noreply@anthropic.com>
This commit is contained in:
@@ -11,15 +11,18 @@ const PREFIX: Record<string, number> = {
|
||||
};
|
||||
|
||||
// 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]);
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user