Consolidate server utilities into shared module

- Add lib/server/utils/normalize.ts with normalizeTicker, normalizeTagsOrNull, nowIso, todayIso
- Add lib/server/utils/validation.ts with asRecord, asBoolean, asStringArray, asEnum
- Add lib/server/utils/index.ts re-exporting all utilities
- Remove duplicate lib/server/utils.ts (old file)
- Update all repos and files to use shared utilities
- Remove redundant ?? '' from normalizeTicker calls
- Update watchlist.ts to use normalizeTagsOrNull for null-return tags
This commit is contained in:
2026-03-15 15:56:16 -04:00
parent edf1cfb421
commit 5f0abbb007
14 changed files with 193 additions and 127 deletions

View File

@@ -0,0 +1,51 @@
export function normalizeTicker(ticker: string): string {
return ticker.trim().toUpperCase();
}
export function normalizeTickerOrNull(ticker: unknown): string | null {
if (typeof ticker !== 'string') return null;
const normalized = ticker.trim().toUpperCase();
return normalized || null;
}
export function normalizeTags(tags?: unknown): string[] {
if (!Array.isArray(tags)) return [];
const unique = new Set<string>();
for (const entry of tags) {
if (typeof entry !== 'string') continue;
const tag = entry.trim();
if (tag) unique.add(tag);
}
return [...unique];
}
export function normalizeTagsOrNull(tags?: unknown): string[] | null {
const result = normalizeTags(tags);
return result.length > 0 ? result : null;
}
export function normalizeOptionalString(value?: unknown): string | null {
if (typeof value !== 'string') return null;
const normalized = value.trim();
return normalized || null;
}
export function normalizeRecord(value?: unknown): Record<string, unknown> | null {
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
return value as Record<string, unknown>;
}
export function normalizePositiveInteger(value?: unknown): number | null {
if (value === null || value === undefined || !Number.isFinite(value as number)) return null;
const normalized = Math.trunc(value as number);
return normalized > 0 ? normalized : null;
}
export function nowIso(): string {
return new Date().toISOString();
}
export function todayIso(): string {
return new Date().toISOString().slice(0, 10);
}