Spaces:
Sleeping
Sleeping
Download frontend/src/lib/format.ts from Ethscriptions/cut: direct link, hf CLI and curl.
- Browser
- Download file 1.41 kB
-
https://huggingface.co/spaces/Ethscriptions/cut/resolve/main/frontend/src/lib/format.ts
- Command line
-
hf download hf://spaces/Ethscriptions/cut/frontend/src/lib/format.ts
-
curl -L -o format.ts https://huggingface.co/spaces/Ethscriptions/cut/resolve/main/frontend/src/lib/format.ts
1.41 kB
| export function formatCount(value: number | null | undefined): string { | |
| const n = Number(value || 0) | |
| if (!Number.isFinite(n)) return '0' | |
| if (n >= 100_000_000) return `${trimZero(n / 100_000_000)}亿` | |
| if (n >= 10_000) return `${trimZero(n / 10_000)}万` | |
| return n.toLocaleString('en-US') | |
| } | |
| function trimZero(n: number): string { | |
| return n.toFixed(1).replace(/\.0$/, '') | |
| } | |
| export function timeAgo(input: number | string | null | undefined): string { | |
| if (input === null || input === undefined) return '—' | |
| const ms = typeof input === 'number' ? input : Date.parse(input) | |
| if (!Number.isFinite(ms)) return '—' | |
| const diff = Date.now() - ms | |
| if (diff < 60_000) return '刚刚' | |
| const minutes = Math.floor(diff / 60_000) | |
| if (minutes < 60) return `${minutes} 分钟前` | |
| const hours = Math.floor(minutes / 60) | |
| if (hours < 24) return `${hours} 小时前` | |
| const days = Math.floor(hours / 24) | |
| if (days < 30) return `${days} 天前` | |
| const date = new Date(ms) | |
| return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}` | |
| } | |
| export function shortUrl(url: string, max = 46): string { | |
| const clean = url.replace(/^https?:\/\//, '').replace(/[.,。,]+$/, '') | |
| return clean.length > max ? `${clean.slice(0, max - 1)}…` : clean | |
| } | |
| export function avatarLarge(url: string | null | undefined): string | null { | |
| if (!url) return null | |
| return url.replace('_normal.', '_bigger.') | |
| } | |