CoompanionApp/server/util.ts

42 lines
987 B
TypeScript
Raw Normal View History

2026-02-03 13:48:56 +01:00
export const DEFAULT_START_BALANCE = 1500;
const CODE_CHARS = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
export function createId(): string {
return crypto.randomUUID();
}
export function createSessionCode(existing: Set<string>): string {
for (let i = 0; i < 1000; i += 1) {
let code = "";
for (let j = 0; j < 5; j += 1) {
code += CODE_CHARS[Math.floor(Math.random() * CODE_CHARS.length)];
}
if (!existing.has(code)) {
return code;
}
}
throw new Error("Unable to allocate session code");
}
export function now(): number {
return Date.now();
}
export function parseAmount(value: unknown): number | null {
if (typeof value === "number" && Number.isFinite(value)) {
return value;
}
if (typeof value === "string") {
const parsed = Number.parseFloat(value);
if (Number.isFinite(parsed)) {
return parsed;
}
}
return null;
}
export function clampBalance(value: number): number {
return Math.round(value * 100) / 100;
}