-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cast): cast to phone in human words
- Loading branch information
1 parent
34b2491
commit 9f2eaea
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { asUniPhoneInHumanWords } from './asUniPhoneInHumanWords'; | ||
import { isUniPhoneNumber } from './isUniPhoneNumber'; | ||
|
||
describe('asUniPhoneInHumanWords', () => { | ||
it('should look right', () => { | ||
const result = asUniPhoneInHumanWords({ | ||
number: isUniPhoneNumber.assure('+13175557777'), | ||
}); | ||
expect(result).toEqual('(317) 555-7777'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { UniPhone } from './UniPhone'; | ||
|
||
/** | ||
* .what = casts a uni phone into human words | ||
* .example | ||
* - +13335557777 => (333) 555-7777 | ||
*/ | ||
export const asUniPhoneInHumanWords = (input: UniPhone): string => { | ||
// Validate input: must start with '+' followed by digits | ||
const phoneRegex = /^\+(\d{1,3})(\d{3})(\d{3})(\d{4})$/; | ||
const match = input.number.match(phoneRegex); | ||
|
||
if (!match) { | ||
throw new Error( | ||
'Invalid UniPhone format. Expected format: +<country-code><10-digit-phone>', | ||
); | ||
} | ||
|
||
// Extract groups | ||
const [, , areaCode, centralOfficeCode, lineNumber] = match; | ||
|
||
// Format the phone number | ||
return `(${areaCode}) ${centralOfficeCode}-${lineNumber}`; | ||
}; |