Skip to content

Commit 21b73be

Browse files
committed
format: add support for country code in phone number input
1 parent 30bdd44 commit 21b73be

File tree

5 files changed

+41
-3
lines changed

5 files changed

+41
-3
lines changed

.changeset/sour-plums-look.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@obosbbl/format": patch
3+
---
4+
5+
allow country code for phone number input in `formatPhoneNumber()`.
6+
7+
Previously it was unable to format phone numbers with country codes, now it can. Note that the country code will not be part of the formatted output.
8+
9+
```js
10+
// 🇳🇴 example
11+
formatPhoneNumber('+4700000000') // => '00 00 000'
12+
13+
// 🇸🇪 example
14+
formatPhoneNumber('+46303123456') // => '0303-12 34 56'
15+
```

packages/format/src/format.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ describe('no', () => {
1616
test.each([
1717
['22865500', '22 86 55 00'],
1818
['80000000', '800 00 000'],
19+
// with country code
20+
['+4722865500', '22 86 55 00'],
1921
])('formatPhoneNumber(%s) -> %s', (input, expected) => {
2022
expect(formatPhoneNumberNo(input)).toBe(expected);
2123
});
@@ -57,6 +59,8 @@ describe('se', () => {
5759
['0303123456', '0303-12 34 56'],
5860
['03031234567', '0303-123 45 67'],
5961
['030312345678', '0303-123 456 78'],
62+
// with country code
63+
['+46303123456', '0303-12 34 56'],
6064
// invalid, too long a number
6165
['0303123456789', '0303123456789'],
6266
])('formatPhoneNumber(%s) -> %s', (input, expected) => {

packages/format/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ type Options = {
1717
};
1818

1919
/**
20-
* Format a phone number
20+
* Format a phone number.
21+
*
22+
* Country code can be present in the input, but it will be removed in the formatted output.
23+
*
2124
* @example
2225
* ```
2326
* formatPhoneNumber('00000000', { locale: 'no' }) // => '00 00 00 00'
2427
* formatPhoneNumber('07012345678', { locale: 'se' }) // => '070-123 45 678'
28+
* formatPhoneNumber('+4700000000', { locale: 'no' }) // => '00 00 00 00'
2529
* ```
2630
*/
2731
export function formatPhoneNumber(input: string, options: Options): string {

packages/format/src/no.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ const REGULAR_PHONE_NUMBER_FORMAT = /^(\d{2})(\d{2})(\d{2})(\d{2})$/;
77
const EIGHT_HUNDRED_SERIES_PHONE_NUMBER_FORMAT = /^(\d{3})(\d{2})(\d{3})$/;
88

99
/**
10-
* Format a phone number
10+
* Format a phone number.
11+
*
12+
* Country code can be present in the input, but it will be removed in the formatted output.
13+
*
1114
* @example
1215
* ```
1316
* formatPhoneNumber('00000000') // => '00 00 00 00'
1417
* formatPhoneNumber('80000000') // => '800 00 000'
18+
* formatPhoneNumber('+4700000000') // => '00 00 00 00'
1519
* ```
1620
*/
1721
export function formatPhoneNumber(input: string): string {
22+
// remove country code
23+
// biome-ignore lint/style/noParameterAssign:
24+
input = input.replace(/^\+47/, '');
25+
1826
const number = replaceIfMatch(
1927
input,
2028
REGULAR_PHONE_NUMBER_FORMAT,

packages/format/src/se.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ const THREE_DIGIT_AREA_CODE =
1414
/^0(11|13|16|18|19|21|23|26|31|33|35|36|40|42|44|46|54|60|63|90)/;
1515

1616
/**
17-
* Format a phone number
17+
* Format a phone number.
18+
*
19+
* Country code can be present in the input, but it will be removed in the formatted output.
20+
*
1821
* @example
1922
* ```
2023
* formatPhoneNumber('07012345678') // => '070-123 45 678'
2124
* formatPhoneNumber('0812345') // => '08-123 45'
2225
* formatPhoneNumber('0311234567') // => '031-123 45 67'
2326
* formatPhoneNumber('0303123456') // => '0303-12 34 56'
27+
* formatPhoneNumber('+46303123456') // => '0303-12 34 56'
2428
* ```
2529
*/
2630
export function formatPhoneNumber(input: string): string {
31+
// biome-ignore lint/style/noParameterAssign:
32+
input = input.replace(/^\+46/, '0');
33+
2734
const normalizedInput = cleanInput(input);
2835

2936
if (MOBILE_PHONE_NUMBER_FORMAT.test(normalizedInput)) {

0 commit comments

Comments
 (0)