From 3502eaf3bd44a9a2ec22a17e99238afee16dd3f8 Mon Sep 17 00:00:00 2001 From: Mischa Holz Date: Fri, 26 Sep 2025 21:52:02 +0200 Subject: [PATCH 1/4] Make new BigDecimal produce better errors --- src/BigDecimal.test.ts | 22 ++++++++++++++++++ src/BigDecimal.test.ts.md | 44 +++++++++++++++++++++++++++++++++++ src/BigDecimal.test.ts.snap | Bin 243 -> 443 bytes src/BigDecimal.ts | 15 ++++++++++-- src/collections.test.ts.snap | Bin 642 -> 644 bytes 5 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/BigDecimal.test.ts b/src/BigDecimal.test.ts index bb2b35f..7f00726 100644 --- a/src/BigDecimal.test.ts +++ b/src/BigDecimal.test.ts @@ -32,6 +32,28 @@ function testUnnecessary( } } +test("BigDecimal parsing", (t) => { + const passes = new BigDecimal("1.1") + t.is(passes.toString(), "1.1") + t.is(passes.scale(), 1) + t.is(passes.unscaledValue(), 11n) + + let e = t.throws(() => new BigDecimal("1.1.1")) + t.snapshot(e, "too many decimal places") + e = t.throws(() => new BigDecimal("1.1e1")) + t.snapshot(e, "weird exponential notation") + e = t.throws(() => new BigDecimal("1.1e10")) + t.snapshot(e, "normal exponential notation") + e = t.throws(() => new BigDecimal("1.1e")) + t.snapshot(e, "other weird exponential notation") + e = t.throws(() => new BigDecimal("1.1e1.1")) + t.snapshot(e, "super weird exponential notation") + e = t.throws(() => new BigDecimal("abcd")) + t.snapshot(e, "not a number") + e = t.throws(() => new BigDecimal("zxy")) + t.snapshot(e, "another not a number") +}) + test("BigDecimal rounding", (t) => { testRounding(t, "5.5", "6", { precision: 0, diff --git a/src/BigDecimal.test.ts.md b/src/BigDecimal.test.ts.md index 1cc2e4f..9825cb6 100644 --- a/src/BigDecimal.test.ts.md +++ b/src/BigDecimal.test.ts.md @@ -4,6 +4,50 @@ The actual snapshot is saved in `BigDecimal.test.ts.snap`. Generated by [AVA](https://avajs.dev). +## BigDecimal parsing + +> too many decimal places + + Error { + message: 'BigDecimal: too many decimal places', + } + +> weird exponential notation + + Error { + message: 'BigDecimal: exponential notation is not supported', + } + +> normal exponential notation + + Error { + message: 'BigDecimal: exponential notation is not supported', + } + +> other weird exponential notation + + Error { + message: 'BigDecimal: exponential notation is not supported', + } + +> super weird exponential notation + + Error { + message: 'BigDecimal: exponential notation is not supported', + } + +> not a number + + Error { + message: 'BigDecimal: not a number', + } + +> another not a number + + Error { + message: 'BigDecimal: not a number', + } + ## BigDecimal.compareTo > Snapshot 1 diff --git a/src/BigDecimal.test.ts.snap b/src/BigDecimal.test.ts.snap index 8719ba4885b41ec273c07d37413c804b9da7932f..846b317f39597decae8eab1fdb7097893fce8f50 100644 GIT binary patch literal 443 zcmV;s0Yv^mRzVqiuE$FCmqD8UESDR&6q7(k738&o_uMw=R~gp?>`)oK;A z4kA#+7jIG?RNSCaBCO?I5!O3ngCpj&W(ijhEDBCB?+7bm-WKuQAG<@#w|QE&CN0H% zs+CYq@|Y>@xRY8jX?@OYFHN;^BFdcax9)^A(aa+Y?Y*tE@grV3ai-mkFlGfbx5dgI|G$Tz>=x002$w%tHVG literal 243 zcmV@RzV)Mq4ZGkz;8mVRG!wuy9E z|4x&)eIJVm00000000A9n3j~2pPXIXxG1$Gvm__A#3?h~B{exSH!(*qIX|}`u_!ep zKeIS5v7k63zofWvVM 0) { + error("BigDecimal: too many decimal places") + } // The unscaled value is the integer part followed by the decimal part - this.#value = BigInt(`${integer}${decimal ?? ""}`) + try { + this.#value = BigInt(`${integer}${decimal ?? ""}`) + } catch (e) { + if (e instanceof Error && e.message.includes("Cannot convert")) { + error("BigDecimal: not a number") + } + + throw e + } // The initial scale is either given (needed for certain math operations) or the length of the decimal part this.#scale = scale ?? decimal?.length ?? 0 diff --git a/src/collections.test.ts.snap b/src/collections.test.ts.snap index d8a29a9ee20920d002deaf9155420d8c2bb8bec6..815ea5e8ef735edee4fdbe9aefcc78927cc68a19 100644 GIT binary patch literal 644 zcmV-~0(<>IRzV5g+Ym*Oe~8taAr?5$nP1KvQ_P$65H zrr-Z=kROW(00000000B6R6UQ=KoEVic0Ncrk`M|`LdXJyfQCXL`6*rc5KJl*bWD{uy%HycHet5J4gGv?HEt| z*B?w_3EO;j&L2Bdd!%vScMOXQ^VUo=9M3ZiVb3go!d-vt2><2LXv78IeWRd);G-(3 z*8$uFum|7}K%2@fDi8I?rXh%uWwY4?ZGaR|18rhwf>o(SrLN`+FN{8E05Cbe80zM5rC!WaPV}RMD%WD)vW4kw`?HBrM1LnmU%3V# zkZ?DqUfT9NJ`0%7SB*vkR71e;WIzK8gpN0f2|=JXh9v9v3Cz zohm}i?~&(B-NLuI$8Kl&J_Mluk?*I7rT~)uJ7QAv0jRhiW emTWFOTRXh}-!NhFFj3`UQvLQCXL`6->*s-0>#&$R$(s*ZQUe<5kj^}94V!r(( zdh^lYE@#g3bJ4y(WfPM|k#D<{JGW!g_QTMzIg8!MqX7bV^y+9bp`7l#RY{X1KWKq= z1J_$Sg;nVxOv-mCRr1xRX{od<22D+&x<^p7s)8UPlLL$m{`_zTyLU{&>pSQTwSGkekmDm z;SzFwk3)YN)P9@0%AGRZ$7t3c>3#}eiol{jX&QPjhO9L7MGV<#`~ASsPqbf>6v~1W zEl^Y-%w#K6;8s@T7SOdI7hdS5}(L|xPb zVc!t)U505(Fl0iSiUeyc!7d=AC&OAx7a=z=Q1L?k5n!zX*mB<;(qQDfUfp+L7Hy}K zE}ZW5uUs{IgXA%sdHI=_vNIW7m?z1$?)(pBZM}o`;`rl4OI}tnsoQg26$oRFD4Qjl cbIn!`@BcSU6nU6v@-S)q2H?jrKK2Iy04vBYIRF3v From f02f30f3aec887e829d65f69806bf6f84a5d0bbd Mon Sep 17 00:00:00 2001 From: Mischa Holz Date: Fri, 26 Sep 2025 21:57:47 +0200 Subject: [PATCH 2/4] Update src/BigDecimal.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/BigDecimal.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BigDecimal.ts b/src/BigDecimal.ts index d761adf..c67451d 100644 --- a/src/BigDecimal.ts +++ b/src/BigDecimal.ts @@ -102,7 +102,7 @@ export class BigDecimal { try { this.#value = BigInt(`${integer}${decimal ?? ""}`) } catch (e) { - if (e instanceof Error && e.message.includes("Cannot convert")) { + if (e instanceof SyntaxError) { error("BigDecimal: not a number") } From 2cb8fa3e581e6ec2b84a2d8664ad80b9381b2509 Mon Sep 17 00:00:00 2001 From: Mischa Holz Date: Fri, 26 Sep 2025 22:07:59 +0200 Subject: [PATCH 3/4] Update src/BigDecimal.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/BigDecimal.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BigDecimal.ts b/src/BigDecimal.ts index c67451d..33c7f62 100644 --- a/src/BigDecimal.ts +++ b/src/BigDecimal.ts @@ -95,7 +95,7 @@ export class BigDecimal { // split into integer and decimal parts const [integer, decimal, ...rest] = str.split(".") if (rest.length > 0) { - error("BigDecimal: too many decimal places") + error("BigDecimal: multiple decimal points found") } // The unscaled value is the integer part followed by the decimal part From 7637f16ee4386915d5847438d4cf881debbefb74 Mon Sep 17 00:00:00 2001 From: Mischa Holz Date: Fri, 26 Sep 2025 22:09:10 +0200 Subject: [PATCH 4/4] Update snapshots --- src/BigDecimal.test.ts.md | 2 +- src/BigDecimal.test.ts.snap | Bin 443 -> 454 bytes 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BigDecimal.test.ts.md b/src/BigDecimal.test.ts.md index 9825cb6..9f3754c 100644 --- a/src/BigDecimal.test.ts.md +++ b/src/BigDecimal.test.ts.md @@ -9,7 +9,7 @@ Generated by [AVA](https://avajs.dev). > too many decimal places Error { - message: 'BigDecimal: too many decimal places', + message: 'BigDecimal: multiple decimal points found', } > weird exponential notation diff --git a/src/BigDecimal.test.ts.snap b/src/BigDecimal.test.ts.snap index 846b317f39597decae8eab1fdb7097893fce8f50..a94aae41ab5de896d74f141b66101c7b27e63700 100644 GIT binary patch literal 454 zcmV;%0XhCbRzVY8di-oUQF-bO8S&9(**O<& znR3N4Zk^N}=`_#Wt?};lBT2aD*Dpns5f*Jx;RwJ9fEpDxsB~eiu{BzbB~h%Z)hcKm zM4(CtZ&DFd+MseOoZ}r4_q$_*W0vO1%S;KDL?&Iqm2zCUN~`P z{H?HTLB}4zU+nXF+HXzT2TRU$p0l zu{Y5F8R@Uk$~k~b02;tEfOi0&0KNcx_uC4+oLb8t`n~k4N~Ho?1(iW-p%+B?+FrAM w(3o1yVTLG%mO?1f!c1D6q@x>_A}#EVSSd1UM6?`Hald}}3z`Pg!U_fe0P2&~RsaA1 literal 443 zcmV;s0Yv^mRzVqiuE$FCmqD8UESDR&6q7(k738&o_uMw=R~gp?>`)oK;A z4kA#+7jIG?RNSCaBCO?I5!O3ngCpj&W(ijhEDBCB?+7bm-WKuQAG<@#w|QE&CN0H% zs+CYq@|Y>@xRY8jX?@OYFHN;^BFdcax9)^A(aa+Y?Y*tE@grV3ai-mkFlGfbx5dgI|G$Tz>=x002$w%tHVG