forked from commaai/new-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.ts
More file actions
71 lines (59 loc) · 2.16 KB
/
format.ts
File metadata and controls
71 lines (59 loc) · 2.16 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
69
70
71
import dayjs from 'dayjs'
import advanced from 'dayjs/plugin/advancedFormat'
import customParseFormat from 'dayjs/plugin/customParseFormat'
import duration, { type Duration } from 'dayjs/plugin/duration'
import type { Route } from '~/types'
dayjs.extend(advanced)
dayjs.extend(customParseFormat)
dayjs.extend(duration)
export { dayjs }
const MI_TO_KM = 1.609344
const isImperial = (): boolean => {
if (typeof navigator === 'undefined') return true
const locale = navigator.language.toLowerCase()
return locale.startsWith('en-us') || locale.startsWith('en-gb')
}
export const formatDistance = (miles: number | undefined): string | undefined => {
if (miles === undefined) return undefined
if (isImperial()) return `${miles.toFixed(1)} mi`
return `${(miles * MI_TO_KM).toFixed(1)} km`
}
const _formatDuration = (duration: Duration): string => {
if (duration.hours() > 0) {
return duration.format('H[h] m[m]')
} else {
return duration.format('m[m]')
}
}
export const formatDuration = (minutes: number | undefined): string | undefined => {
if (minutes === undefined) return undefined
const duration = dayjs.duration({
hours: Math.floor(minutes / 60),
minutes: Math.round(minutes % 60),
})
return _formatDuration(duration)
}
export const getRouteDuration = (route: Route): Duration | undefined => {
if (!route || !route.end_time) return undefined
const startTime = dayjs(route.start_time)
const endTime = dayjs(route.end_time)
return dayjs.duration(endTime.diff(startTime))
}
export const formatRouteDuration = (route: Route | undefined): string => {
if (!route) return ''
const duration = getRouteDuration(route)
return duration ? _formatDuration(duration) : ''
}
const parseTimestamp = (input: dayjs.ConfigType): dayjs.Dayjs => {
if (typeof input === 'number') {
// Assume number is unix timestamp, convert to seconds
return dayjs.unix(input >= 1e11 ? input / 1000 : input)
}
return dayjs(input)
}
export const formatDate = (input: dayjs.ConfigType): string => {
const date = parseTimestamp(input)
// Hide current year
const yearStr = date.year() === dayjs().year() ? '' : ', YYYY'
return date.format('MMMM Do' + yearStr)
}