-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
62 lines (58 loc) · 1.81 KB
/
index.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
// Convert strings like "2023-12-13T18:32:32.080Z" to "Dec 13th 2023, 1:32:32 PM"
export function formatDateString(dateString) {
const monthNames = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
]
function getOrdinalNum(n) {
return (
n +
(n > 0
? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10]
: '')
)
}
const date = new Date(dateString)
const year = date.getFullYear()
const month = monthNames[date.getMonth()]
const day = getOrdinalNum(date.getDate())
const hours = date.getHours()
const minutes = date.getMinutes().toString().padStart(2, '0')
const seconds = date.getSeconds().toString().padStart(2, '0')
const ampm = hours >= 12 ? 'PM' : 'AM'
const formattedHours = hours % 12 || 12
return `${month} ${day} ${year}, ${formattedHours}:${minutes}:${seconds} ${ampm}`
}
/**
* Formats a phone number string into a standardized format.
*
* This function takes a phone number string as input and formats it into
* the standard North American format: (XXX) XXX-XXXX
* If the international code (1) is present, it's formatted as (+1) (XXX) XXX-XXXX.
*
* @param {string} phoneNumberString - The input phone number string.
* @returns {string|null} The formatted phone number string or null if invalid.
*
* Example outputs:
* - With international code: "+1 (555) 123-4567"
* - Without international code: "(555) 123-4567"
*/
export function formatPhoneNumber(phoneNumberString: string) {
const cleaned = ('' + phoneNumberString).replace(/\D/g, '')
const match = cleaned.match(/^(1|)?(\d{3})(\d{3})(\d{4})$/)
if (match) {
const intlCode = match[1] ? '+1 ' : ''
return [intlCode, '(', match[2], ') ', match[3], '-', match[4]].join('')
}
return null
}