forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilters.ts
More file actions
105 lines (90 loc) · 2.77 KB
/
Copy pathfilters.ts
File metadata and controls
105 lines (90 loc) · 2.77 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { intervalToDuration, formatDuration } from 'date-fns'
import { parsePhoneNumber, isValidPhoneNumber } from 'libphonenumber-js'
export function formatPhoneNumber(value: string): string {
if (!value || typeof value !== 'string') {
return value ?? ''
}
if (!isValidPhoneNumber(value)) {
return value
}
const phoneNumber = parsePhoneNumber(value)
if (phoneNumber) {
return phoneNumber.formatInternational()
}
return value
}
export function phoneCountry(value: string): string {
const phoneNumber = parsePhoneNumber(value)
if (phoneNumber && phoneNumber.country) {
const regionNames = new Intl.DisplayNames(undefined, { type: 'region' })
return regionNames.of(phoneNumber.country) ?? 'Earth'
}
return 'Earth'
}
export function formatTimestamp(value: string): string {
return new Date(value).toLocaleString()
}
export function formatMoney(value: string | number): string {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
}).format(typeof value === 'string' ? parseInt(value) : value)
}
export function formatDecimal(value: string | number): string {
return new Intl.NumberFormat('en-US', {
style: 'decimal',
}).format(typeof value === 'string' ? parseInt(value) : value)
}
export function formatBillingPeriod(value: string): string {
return new Date(value).toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
})
}
export function formatBillingPeriodDateOrdinal(value: string): string {
const date = new Date(value)
const day = date.getDate()
const month = date.toLocaleDateString('en-US', { month: 'long' })
const year = date.getFullYear()
const suffix =
day % 10 === 1 && day !== 11
? 'st'
: day % 10 === 2 && day !== 12
? 'nd'
: day % 10 === 3 && day !== 13
? 'rd'
: 'th'
return `${month} ${day}<sup>${suffix}</sup> ${year}`
}
export interface BillingPeriodDateOrdinalParts {
leading: string
suffix: string
trailing: string
}
export function formatBillingPeriodDateOrdinalParts(
value: string,
): BillingPeriodDateOrdinalParts {
const date = new Date(value)
const day = date.getDate()
const month = date.toLocaleDateString('en-US', { month: 'long' })
const year = date.getFullYear()
const suffix =
day % 10 === 1 && day !== 11
? 'st'
: day % 10 === 2 && day !== 12
? 'nd'
: day % 10 === 3 && day !== 13
? 'rd'
: 'th'
return { leading: `${month} ${day}`, suffix, trailing: ` ${year}` }
}
export function humanizeTime(value: string): string {
const durations = intervalToDuration({
start: new Date(value),
end: new Date(),
})
return formatDuration(durations)
}
export function startsWithLetter(value: string): boolean {
return /^[a-zA-Z]/.test(value)
}