FIX: Slightly better behaviour for increase/decrease decimal places

The general solution must be done in Rust and it is a bit more complex.
This commit is contained in:
Nicolás Hatcher
2024-10-25 18:58:03 +02:00
committed by Nicolás Hatcher Andrés
parent f78027247b
commit 75d8a5282e
2 changed files with 30 additions and 3 deletions

View File

@@ -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 { 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 // Increase decimal places for existing numbers with decimals
if (numberFormat === "general") {
return "#,##0.000";
}
const newNumberFormat = numberFormat.replace(/\.0/g, ".00"); const newNumberFormat = numberFormat.replace(/\.0/g, ".00");
// If no decimal places declared, add 0.0 // If no decimal places declared, add 0.0
if (!newNumberFormat.includes(".")) { if (!newNumberFormat.includes(".")) {
@@ -10,13 +16,15 @@ export function increaseDecimalPlaces(numberFormat: string): string {
if (newNumberFormat.includes("#")) { if (newNumberFormat.includes("#")) {
return newNumberFormat.replace(/#([^#,]|$)/g, "0.0$1"); return newNumberFormat.replace(/#([^#,]|$)/g, "0.0$1");
} }
return "0.0"; return numberFormat;
} }
return newNumberFormat; return newNumberFormat;
} }
export function decreaseDecimalPlaces(numberFormat: string): string { 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 // Decrease decimal places for existing numbers with decimals
let newNumberFormat = numberFormat.replace(/\.0/g, "."); let newNumberFormat = numberFormat.replace(/\.0/g, ".");
// Fix leftover dots // Fix leftover dots

View File

@@ -1,7 +1,26 @@
import { expect, test } from "vitest"; import { expect, test } from "vitest";
import { decreaseDecimalPlaces, increaseDecimalPlaces } from "../formatUtil";
import { isNavigationKey } from "../util"; import { isNavigationKey } from "../util";
test("checks arrow left is a navigation key", () => { test("checks arrow left is a navigation key", () => {
expect(isNavigationKey("ArrowLeft")).toBe(true); expect(isNavigationKey("ArrowLeft")).toBe(true);
expect(isNavigationKey("Arrow")).toBe(false); 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',
);
});