30 lines
1 KiB
TypeScript
30 lines
1 KiB
TypeScript
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);
|
|
}
|