-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathformat.ts
More file actions
68 lines (62 loc) · 2.02 KB
/
Copy pathformat.ts
File metadata and controls
68 lines (62 loc) · 2.02 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { formatDuration } from 'date-fns'
import enUS from 'date-fns/locale/en-US'
import ja from 'date-fns/locale/ja'
import zhCN from 'date-fns/locale/zh-CN'
import languageIdentifiers from './LanguageIdentifiers.json'
export function getDurationData(ms: number): { hour: number, minute: number, second: number } {
const MS_OF_HOUR = 3600000
const MS_OF_MINUTE = 60000
const MS_OF_SECOND = 1000
const hour = Math.floor(ms / MS_OF_HOUR)
ms %= MS_OF_HOUR
const minute = Math.floor(ms / MS_OF_MINUTE)
ms %= MS_OF_MINUTE
const second = Math.floor(ms / MS_OF_SECOND)
return { hour, minute, second }
}
export function getDurationString(ms: number, format = ['hours', 'minutes', 'seconds']): string {
const route = useRoute()
const locale = route.params.locale as string
const { hour, minute, second } = getDurationData(ms)
const localeMap = new Map<string, Locale>([
['en', enUS],
['zh-CN', zhCN],
['ja', ja],
])
return formatDuration({ hours: hour, minutes: minute, seconds: second }, {
locale: localeMap.get(locale) ?? localeMap.get('en'),
format,
}).replace('hour', 'hr').replace('minute', 'min').replace('second', 'sec')
}
export function formateDays(days: number) {
const route = useRoute()
const locale = route.params.locale as string
const localeMap = new Map<string, Locale>([
['en', enUS],
['zh-CN', zhCN],
['ja', ja],
])
return formatDuration({ days }, {
locale: localeMap.get(locale) ?? localeMap.get('en'),
format: ['days'],
})
}
export function capitalizeFirstLetter(str: string) {
if (str.length === 0) {
return str
}
return str.charAt(0).toUpperCase() + str.slice(1)
}
const entries = Object.entries(languageIdentifiers)
const languageIdentifiersMap = new Map(entries)
export function getLanguageName(languageIdentifier: string): string {
const res = languageIdentifiersMap.get(languageIdentifier) ?? languageIdentifier
switch (res) {
case '':
return 'Unknown'
case 'plain_text':
return 'Plain Text'
default:
return res
}
}