testsAndMisc/bucket_catch/packages/frontend/src/lib/fileIcon.ts
Krzysztof kuhy Rudnicki 5a9296d8aa Add bucket_catch: osu!catch browser game with 100% frontend test coverage
Frontend (React 19 + Vite 6 + TypeScript strict):
- DropZone, ModeSelect, GameCanvas, PuzzleCanvas, ScoreScreen, PuzzleResult
- File-drop game with AABB collision; download (JSZip) and upload (NestJS) modes
- Puzzle mode: NxN image slice via OffscreenCanvas; Union-Find spatial clustering
  guarantees 100% catch rate is always achievable regardless of piece speeds
- ESLint typescript-eslint strict-type-checked (zero errors)
- 145 Vitest tests; 100% coverage on statements/branches/functions/lines

Backend (NestJS 11):
- POST /files/upload (multer disk storage) and GET /health

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YZ8QTmreFcaqrsvVb38Grd
2026-06-27 12:21:35 +02:00

47 lines
1.4 KiB
TypeScript

/** Returns an emoji representing the file type based on MIME or extension. */
export function fileIcon(file: File): string {
const mime = file.type;
if (mime.startsWith("image/")) return "🖼️";
if (mime.startsWith("video/")) return "🎬";
if (mime.startsWith("audio/")) return "🎵";
if (mime === "application/pdf") return "📄";
if (
mime === "application/zip" ||
mime === "application/x-zip-compressed" ||
file.name.endsWith(".zip") ||
file.name.endsWith(".tar.gz") ||
file.name.endsWith(".rar")
)
return "📦";
if (
mime.includes("javascript") ||
mime.includes("typescript") ||
(/\.(ts|tsx|js|jsx|py|go|rs|c|cpp|java|sh)$/.exec(file.name))
)
return "💻";
if (
mime.includes("text") ||
(/\.(txt|md|csv|log)$/.exec(file.name))
)
return "📝";
return "📎";
}
/** Truncate a filename to at most `max` characters, keeping extension. */
export function truncateFilename(name: string, max = 18): string {
if (name.length <= max) return name;
const dot = name.lastIndexOf(".");
if (dot > 0) {
const ext = name.slice(dot);
return name.slice(0, max - ext.length - 1) + "…" + ext;
}
return name.slice(0, max - 1) + "…";
}
/** Human-readable file size. */
export function formatSize(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}