-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
43 lines (32 loc) · 1.09 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
export interface CivicAddressPieces {
civicNumber: string
streetName: string
unitNumber?: string
qualifier?: string
}
const letterRegex = /^[A-Z]$/i
function isLetter(possibleLetter: string): boolean {
return possibleLetter.length === 1 && letterRegex.test(possibleLetter)
}
/**
* Formats a civic address from its pieces using Canada Post guidelines.
* @param civicAddressPieces - The pieces of the civic address.
* @returns the civic address as a formatted string.
*/
export default function formatCivicAddress(
civicAddressPieces: CivicAddressPieces
): string {
let civicAddress = civicAddressPieces.civicNumber.trim()
// Unit Number (If Available)
const unitNumber = (civicAddressPieces.unitNumber ?? '').trim()
if (unitNumber !== '') {
civicAddress = `${unitNumber}-${civicAddress}`
}
// Qualifier (If Available)
const qualifier = (civicAddressPieces.qualifier ?? '').trim()
if (qualifier !== '') {
civicAddress = civicAddress + (isLetter(qualifier) ? '' : ' ') + qualifier
}
civicAddress += ` ${civicAddressPieces.streetName.trim()}`
return civicAddress
}