File size: 1,414 Bytes
94b4ce6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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.')
}