Spaces:
Running
Running
Download frontend/src/shared/lib/format.ts from Shadowfilebot/Bot: direct link, hf CLI and curl.
- Browser
- Download file 1.89 kB
-
https://huggingface.co/spaces/Shadowfilebot/Bot/resolve/main/frontend/src/shared/lib/format.ts
- Command line
-
hf download hf://spaces/Shadowfilebot/Bot/frontend/src/shared/lib/format.ts
-
curl -L -o format.ts https://huggingface.co/spaces/Shadowfilebot/Bot/resolve/main/frontend/src/shared/lib/format.ts
1.89 kB
| export function formatBytes(bytes: number): string { | |
| if (!Number.isFinite(bytes) || bytes <= 0) return '0 B'; | |
| const units = ['B', 'KB', 'MB', 'GB', 'TB']; | |
| const idx = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1); | |
| const val = bytes / Math.pow(1024, idx); | |
| return `${val >= 10 || idx === 0 ? val.toFixed(0) : val.toFixed(1)} ${units[idx]}`; | |
| } | |
| export function formatDateVi(iso?: string | null): string { | |
| if (!iso) return '—'; | |
| const d = new Date(iso); | |
| if (Number.isNaN(d.getTime())) return iso.slice(0, 10); | |
| return d.toLocaleDateString('vi-VN', { | |
| day: '2-digit', | |
| month: '2-digit', | |
| year: 'numeric', | |
| }); | |
| } | |
| export function normalizePath(path: string): string { | |
| if (!path || path === '/') return '/'; | |
| return path.replace(/\/+$/, ''); | |
| } | |
| export function joinPath(base: string, name: string): string { | |
| const cleanBase = base.replace(/^\/+|\/+$/g, ''); | |
| const cleanName = name.replace(/^\/+/, ''); | |
| return cleanBase ? `${cleanBase}/${cleanName}` : cleanName; | |
| } | |
| export type FileCategory = 'dir' | 'image' | 'code' | 'markdown' | 'archive' | 'text'; | |
| const IMAGE_EXTS = new Set(['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'ico']); | |
| const CODE_EXTS = new Set([ | |
| 'ts', | |
| 'tsx', | |
| 'js', | |
| 'jsx', | |
| 'mjs', | |
| 'cjs', | |
| 'py', | |
| 'go', | |
| 'rs', | |
| 'json', | |
| 'yaml', | |
| 'yml', | |
| 'toml', | |
| 'html', | |
| 'css', | |
| 'scss', | |
| 'sh', | |
| 'bash', | |
| 'sql', | |
| 'env', | |
| ]); | |
| const MD_EXTS = new Set(['md', 'mdx', 'txt', 'log']); | |
| const ARCHIVE_EXTS = new Set(['zip', 'tar', 'gz', 'tgz', '7z', 'rar']); | |
| export function getFileCategory(name: string, isDir: boolean): FileCategory { | |
| if (isDir) return 'dir'; | |
| const ext = name.split('.').pop()?.toLowerCase() || ''; | |
| if (IMAGE_EXTS.has(ext)) return 'image'; | |
| if (CODE_EXTS.has(ext)) return 'code'; | |
| if (MD_EXTS.has(ext)) return 'markdown'; | |
| if (ARCHIVE_EXTS.has(ext)) return 'archive'; | |
| return 'text'; | |
| } | |