forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.ts
92 lines (82 loc) · 2.82 KB
/
format.ts
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Format a percent that has already been converted to percentage units
// (i.e. it is not in decimal form; passing in 5 outputs 5%, not 500%).
// Examples:
// 5 --> 5%
// 5.12345 --> 5.1234%
// 0.15 --> 0.15%
// 23.4852 --> 23.49%
// 23.4952 --> 23.5%
export const formatPercentOf100 = (
percentOf100: number,
maximumSignificantDigits = 4
) =>
percentOf100.toLocaleString(undefined, {
maximumSignificantDigits,
}) + '%'
export const dateFormatterNoDay = new Intl.DateTimeFormat('default', {
month: 'long',
day: undefined,
year: 'numeric',
})
export const dateFormatterNoYear = new Intl.DateTimeFormat('default', {
month: 'long',
day: 'numeric',
year: undefined,
})
export const dateFormatterDayAndYear = new Intl.DateTimeFormat('default', {
month: 'long',
day: 'numeric',
year: 'numeric',
})
// If this year, add date in month and ignore year. Otherwise, add year and
// ignore date in month. If both is true, add both day and year.
export const formatDate = (date: Date, both = false) =>
(both
? dateFormatterDayAndYear
: date.getFullYear() === new Date().getFullYear()
? dateFormatterNoYear
: dateFormatterNoDay
).format(date)
// Shows month and date. Adds year if not this year.
export const formatDateWithDayAndMaybeYear = (date: Date) =>
(date.getFullYear() === new Date().getFullYear()
? dateFormatterNoYear
: dateFormatterDayAndYear
).format(date)
export const dateTimeFormatter = new Intl.DateTimeFormat('default', {
dateStyle: 'short',
timeStyle: 'short',
})
export const formatDateTime = (date: Date) => dateTimeFormatter.format(date)
export const longDateTimeFormatter = new Intl.DateTimeFormat('default', {
dateStyle: 'long',
timeStyle: 'short',
})
export const formatLongDateTime = (date: Date) =>
longDateTimeFormatter.format(date)
export const dateTimeTzFormatter = new Intl.DateTimeFormat('default', {
month: '2-digit',
day: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
timeZoneName: 'short',
})
export const formatDateTimeTz = (date: Date) => dateTimeTzFormatter.format(date)
export const timeFormatter = new Intl.DateTimeFormat('default', {
timeStyle: 'short',
})
export const formatTime = (date: Date) => timeFormatter.format(date)
export const isoStringForLocalDateString = (dateString: string) =>
new Date(dateString).toISOString()
// Select number of decimal digits, rounding down / truncating.
export const toFixedDown = (value: number, digits: number) => {
// If contains scientific notation, truncate and use BigInt to get rid of it.
let stringifiedValue = value.toString()
if (stringifiedValue.includes('e')) {
stringifiedValue = BigInt(Math.floor(value)).toString()
}
const re = new RegExp('(\\d+\\.\\d{' + digits + '})(\\d)')
const matches = stringifiedValue.match(re)
return matches ? parseFloat(matches[1]) : value
}