fix(folio): add parseTypedAmount helper matching app's parseCents

Raw user input like "50" must become 5000 cents, not 50. The existing
parseDollarCents helper strips non-digits and so reads "50" as 50 cents,
which is correct for formatted balance text but off by 100x for raw
input from the amount field.
This commit is contained in:
pj committed 2026-05-31 18:56:30 +05:30
1 parent ee1fad1d30
commit d88200f398
2 files changed
+72

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { parseTypedAmount } from "../../../examples/folio/sanderling/predicates.ts";
test("empty string returns 0", () => {
assert.equal(parseTypedAmount(""), 0);
});
test("undefined returns 0", () => {
assert.equal(parseTypedAmount(undefined), 0);
});
test("null returns 0", () => {
assert.equal(parseTypedAmount(null), 0);
});
test("whole number 50 parses as 5000 cents", () => {
assert.equal(parseTypedAmount("50"), 5000);
});
test("decimal 5.50 parses as 550 cents", () => {
assert.equal(parseTypedAmount("5.50"), 550);
});
test("decimal 5.05 parses as 505 cents", () => {
assert.equal(parseTypedAmount("5.05"), 505);
});
test("one-decimal 0.5 parses as 50 cents", () => {
assert.equal(parseTypedAmount("0.5"), 50);
});
test("non-numeric returns 0", () => {
assert.equal(parseTypedAmount("abc"), 0);
});
test("too many decimals returns 0", () => {
assert.equal(parseTypedAmount("5.123"), 0);
});
test("zero returns 0", () => {
assert.equal(parseTypedAmount("0"), 0);
});
test("leading plus sign tolerated as positive", () => {
assert.equal(parseTypedAmount("+50"), 5000);
});
test("leading minus sign tolerated as positive", () => {
assert.equal(parseTypedAmount("-50"), 5000);
});
test("comma-separated thousands accepted", () => {
assert.equal(parseTypedAmount("1,234.56"), 123456);
});