Add optional room areas
This commit is contained in:
parent
a6500e2b26
commit
dce31ee143
26 changed files with 3669 additions and 78 deletions
30
src/frontend/utils/room-area.ts
Normal file
30
src/frontend/utils/room-area.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
export type RoomAreaParseResult =
|
||||
| { ok: true; value: number | null }
|
||||
| { ok: false; error: string };
|
||||
|
||||
/** Parses an optional German decimal without rounding the stored value. */
|
||||
export function parseRoomAreaInput(text: string): RoomAreaParseResult {
|
||||
const normalized = text.trim();
|
||||
if (!normalized) return { ok: true, value: null };
|
||||
if (!/^(?:\d+(?:,\d*)?|,\d+)$/.test(normalized)) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "Bitte eine nicht negative Zahl mit Komma eingeben.",
|
||||
};
|
||||
}
|
||||
const value = Number(normalized.replace(",", "."));
|
||||
return Number.isFinite(value) && value >= 0
|
||||
? { ok: true, value }
|
||||
: { ok: false, error: "Die Fläche muss mindestens 0 m² betragen." };
|
||||
}
|
||||
|
||||
export function formatRoomArea(areaM2: number): string {
|
||||
return areaM2.toLocaleString("de-DE", {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2,
|
||||
});
|
||||
}
|
||||
|
||||
export function formatOptionalRoomAreaInput(areaM2: number | null | undefined): string {
|
||||
return areaM2 === null || areaM2 === undefined ? "" : formatRoomArea(areaM2);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue