diff --git a/webapp/src/components/formatUtil.ts b/webapp/src/components/formatUtil.ts index c865401..e32d0b8 100644 --- a/webapp/src/components/formatUtil.ts +++ b/webapp/src/components/formatUtil.ts @@ -1,6 +1,12 @@ +// FIXME: These two should be done in the back end and thoroughly tested +// * Dates shouldn't change +// * General depends on the value. Increase(General, 0.5) => 0.50 and so on + export function increaseDecimalPlaces(numberFormat: string): string { - // FIXME: Should it be done in the Rust? How should it work? // Increase decimal places for existing numbers with decimals + if (numberFormat === "general") { + return "#,##0.000"; + } const newNumberFormat = numberFormat.replace(/\.0/g, ".00"); // If no decimal places declared, add 0.0 if (!newNumberFormat.includes(".")) { @@ -10,13 +16,15 @@ export function increaseDecimalPlaces(numberFormat: string): string { if (newNumberFormat.includes("#")) { return newNumberFormat.replace(/#([^#,]|$)/g, "0.0$1"); } - return "0.0"; + return numberFormat; } return newNumberFormat; } export function decreaseDecimalPlaces(numberFormat: string): string { - // FIXME: Should it be done in the Rust? How should it work? + if (numberFormat === "general") { + return "#,##0.0"; + } // Decrease decimal places for existing numbers with decimals let newNumberFormat = numberFormat.replace(/\.0/g, "."); // Fix leftover dots diff --git a/webapp/src/components/tests/util.test.ts b/webapp/src/components/tests/util.test.ts index 1b37902..0669d88 100644 --- a/webapp/src/components/tests/util.test.ts +++ b/webapp/src/components/tests/util.test.ts @@ -1,7 +1,26 @@ import { expect, test } from "vitest"; +import { decreaseDecimalPlaces, increaseDecimalPlaces } from "../formatUtil"; import { isNavigationKey } from "../util"; test("checks arrow left is a navigation key", () => { expect(isNavigationKey("ArrowLeft")).toBe(true); expect(isNavigationKey("Arrow")).toBe(false); }); + +test("increase decimals", () => { + expect(increaseDecimalPlaces('"€"#,##0.00'), '"€"#,##0.000'); + expect(increaseDecimalPlaces("general"), "#,##0.000"); + expect( + increaseDecimalPlaces('dddd"," mmmm dd"," yyyy'), + 'dddd"," mmmm dd"," yyyy', + ); +}); + +test("decrease decimals", () => { + expect(decreaseDecimalPlaces('"€"#,##0.00'), '"€"#,##0.0'); + expect(decreaseDecimalPlaces("general"), "#,##0.0"); + expect( + decreaseDecimalPlaces('dddd"," mmmm dd"," yyyy'), + 'dddd"," mmmm dd"," yyyy', + ); +});