File tree Expand file tree Collapse file tree 5 files changed +41
-3
lines changed Expand file tree Collapse file tree 5 files changed +41
-3
lines changed Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff 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 ) => {
Original file line number Diff line number Diff 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 */
2731export function formatPhoneNumber ( input : string , options : Options ) : string {
Original file line number Diff line number Diff line change @@ -7,14 +7,22 @@ const REGULAR_PHONE_NUMBER_FORMAT = /^(\d{2})(\d{2})(\d{2})(\d{2})$/;
77const 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 */
1721export function formatPhoneNumber ( input : string ) : string {
22+ // remove country code
23+ // biome-ignore lint/style/noParameterAssign:
24+ input = input . replace ( / ^ \+ 4 7 / , '' ) ;
25+
1826 const number = replaceIfMatch (
1927 input ,
2028 REGULAR_PHONE_NUMBER_FORMAT ,
Original file line number Diff line number Diff line change @@ -14,16 +14,23 @@ const THREE_DIGIT_AREA_CODE =
1414 / ^ 0 ( 1 1 | 1 3 | 1 6 | 1 8 | 1 9 | 2 1 | 2 3 | 2 6 | 3 1 | 3 3 | 3 5 | 3 6 | 4 0 | 4 2 | 4 4 | 4 6 | 5 4 | 6 0 | 6 3 | 9 0 ) / ;
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 */
2630export function formatPhoneNumber ( input : string ) : string {
31+ // biome-ignore lint/style/noParameterAssign:
32+ input = input . replace ( / ^ \+ 4 6 / , '0' ) ;
33+
2734 const normalizedInput = cleanInput ( input ) ;
2835
2936 if ( MOBILE_PHONE_NUMBER_FORMAT . test ( normalizedInput ) ) {
You can’t perform that action at this time.
0 commit comments