From 9717acb7bb0f6254f50e24a0023add016db9a40b Mon Sep 17 00:00:00 2001 From: fudom <143608856+fudom@users.noreply.github.com> Date: Tue, 10 Dec 2024 12:19:11 +0100 Subject: [PATCH 1/3] Create type-guards.ts --- core/src/utils/type-guards.ts | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 core/src/utils/type-guards.ts diff --git a/core/src/utils/type-guards.ts b/core/src/utils/type-guards.ts new file mode 100644 index 00000000000..e5ea629954d --- /dev/null +++ b/core/src/utils/type-guards.ts @@ -0,0 +1,6 @@ +/** + * Checks input for usable number. Not NaN and not Infinite. + */ +export const isSafeNumber = (input: unknown): input is number => { + return typeof input === 'number' && !isNaN(input) && isFinite(input); +}; From 4b41bb9ce32068293edaa7f06d33fd100231f986 Mon Sep 17 00:00:00 2001 From: fudom <143608856+fudom@users.noreply.github.com> Date: Tue, 10 Dec 2024 12:29:38 +0100 Subject: [PATCH 2/3] Check for safe number --- core/src/utils/floating-point/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/src/utils/floating-point/index.ts b/core/src/utils/floating-point/index.ts index d8ed8570741..1e25ea37197 100644 --- a/core/src/utils/floating-point/index.ts +++ b/core/src/utils/floating-point/index.ts @@ -1,4 +1,7 @@ +import { isSafeNumber } from '../type-guards.ts'; + export function getDecimalPlaces(n: number) { + if (!isSafeNumber(n)) return 0; if (n % 1 === 0) return 0; return n.toString().split('.')[1].length; } @@ -36,6 +39,7 @@ export function getDecimalPlaces(n: number) { * be used as a reference for the desired specificity. */ export function roundToMaxDecimalPlaces(n: number, ...references: number[]) { + if (!isSafeNumber(n)) return 0; const maxPlaces = Math.max(...references.map((r) => getDecimalPlaces(r))); return Number(n.toFixed(maxPlaces)); } From 21e0f607ef67f1aa912d1f00bc67c48592101393 Mon Sep 17 00:00:00 2001 From: fudom <143608856+fudom@users.noreply.github.com> Date: Tue, 10 Dec 2024 12:43:12 +0100 Subject: [PATCH 3/3] Update floating-point.spec.ts --- core/src/utils/floating-point/floating-point.spec.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/src/utils/floating-point/floating-point.spec.ts b/core/src/utils/floating-point/floating-point.spec.ts index 80db4138b5b..d52fbd0f9ca 100644 --- a/core/src/utils/floating-point/floating-point.spec.ts +++ b/core/src/utils/floating-point/floating-point.spec.ts @@ -11,6 +11,12 @@ describe('floating point utils', () => { const n = getDecimalPlaces(5); expect(n).toBe(0); }); + + it('should handle nullish values', () => { + expect(getDecimalPlaces(undefined as any)).toBe(0); + expect(getDecimalPlaces(null as any)).toBe(0); + expect(getDecimalPlaces(NaN as any)).toBe(0); + }); }); describe('roundToMaxDecimalPlaces', () => { @@ -18,5 +24,11 @@ describe('floating point utils', () => { const n = roundToMaxDecimalPlaces(5.12345, 1.12, 2.123); expect(n).toBe(5.123); }); + + it('should handle nullish values', () => { + expect(roundToMaxDecimalPlaces(undefined as any)).toBe(0); + expect(roundToMaxDecimalPlaces(null as any)).toBe(0); + expect(roundToMaxDecimalPlaces(NaN as any)).toBe(0); + }) }); });