-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.js
49 lines (43 loc) · 1.29 KB
/
utils.js
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
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'
dayjs.extend(customParseFormat)
const compactNumberFormatter = new Intl.NumberFormat("en", {
notation: "compact"
})
export function formatNumberWithDescription(number) {
if (number >= 1e9) {
return (number / 1e9).toFixed(2) + " billion";
} else if (number >= 1e6) {
return (number / 1e6).toFixed(2) + " million";
} else if (number >= 1e3) {
return (number / 1e3).toFixed(2) + " thousand";
} else {
return number.toString();
}
}
export function formatNumber(number) {
return compactNumberFormatter.format(number)
}
export function parseDate(date_string, default_value) {
const value = dayjs(date_string, "YYYY-MM-DD")
if (!isNaN(value.year()) && !isNaN(value.month()) && !isNaN(value.day())) {
if (value.year() >= 1970 && value.year() < 2030) {
return value.format("YYYY-MM-DD")
}
return default_value
}
return default_value
}
export function toValidStyleName(str) {
return str
.replace(/[^a-zA-Z0-9]/g, ' ')
.trim()
.split(/\s+/)
.map((word, index) => {
if (index === 0) {
return word.toLowerCase();
}
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
})
.join('');
}