Skip to content

Commit 4e05eff

Browse files
committed
formatPhoneNumber
1 parent 0a9a69d commit 4e05eff

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

packages/format/src/format.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import { describe, expect, test } from 'vitest';
2-
import { formatOrganizationNumber as formatOrganizationNumberNo } from './no';
3-
import { formatOrganizationNumber as formatOrganizationNumberSe } from './se';
2+
import {
3+
formatOrganizationNumber as formatOrganizationNumberNo,
4+
formatPhoneNumber as formatPhoneNumberNo,
5+
} from './no';
6+
import {
7+
formatOrganizationNumber as formatOrganizationNumberSe,
8+
formatPhoneNumber as formatPhoneNumberSe,
9+
} from './se';
410

511
describe('no', () => {
12+
test.each([
13+
['22865500', '22 86 55 00'],
14+
['80000000', '800 00 000'],
15+
])('formatPhoneNumber(%s) -> %s', (input, expected) => {
16+
expect(formatPhoneNumberNo(input)).toBe(expected);
17+
});
18+
619
test.each([
720
['000000000', '000 000 000'],
821
['000 000 000', '000 000 000'],
@@ -14,6 +27,13 @@ describe('no', () => {
1427
});
1528

1629
describe('se', () => {
30+
test.each([
31+
['0701234567', '070-123 45 67'],
32+
['0801234567', '070-123 45 67'],
33+
])('formatPhoneNumber(%s) -> %s', (input, expected) => {
34+
expect(formatPhoneNumberSe(input)).toBe(expected);
35+
});
36+
1737
test.each([
1838
['0000000000', '000000-0000'],
1939
['000000-0000', '000000-0000'],

packages/format/src/no.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { replaceIfMatch } from './utils';
22

3+
// Regular phone number format is: 00 00 00 00
4+
// if the number starts with 8, it's an 800-series number, with the format: 800 00 000
35
// See https://sprakradet.no/godt-og-korrekt-sprak/rettskriving-og-grammatikk/tall-tid-dato/
46
const REGULAR_PHONE_NUMBER_FORMAT = /^(\d{2})(\d{2})(\d{2})(\d{2})$/;
57
const EIGHT_HUNDRED_SERIES_PHONE_NUMBER_FORMAT = /^(\d{3})(\d{2})(\d{3})$/;
@@ -12,12 +14,20 @@ const EIGHT_HUNDRED_SERIES_PHONE_NUMBER_FORMAT = /^(\d{3})(\d{2})(\d{3})$/;
1214
* formatPhoneNumber('80000000') // => '800 00 000'
1315
* ```
1416
*/
15-
export function formatPhoneNumber(number: string): string {
16-
if (number.startsWith('8')) {
17-
return number.replace(EIGHT_HUNDRED_SERIES_PHONE_NUMBER_FORMAT, '$1 $2 $3');
18-
}
17+
export function formatPhoneNumber(input: string): string {
18+
const number = replaceIfMatch(
19+
input,
20+
REGULAR_PHONE_NUMBER_FORMAT,
21+
'$1 $2 $3 $4',
22+
);
1923

20-
return number.replace(DEFAULT_PHONE_NUMBER_FORMAT, '$1 $2 $3 $4');
24+
return number.startsWith('8')
25+
? replaceIfMatch(
26+
number,
27+
EIGHT_HUNDRED_SERIES_PHONE_NUMBER_FORMAT,
28+
'$1 $2 $3',
29+
)
30+
: number;
2131
}
2232

2333
const ORG_NUMBER_FORMAT = /^(\d{3})(\d{3})(\d{3})$/;

packages/format/src/se.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
import { replaceIfMatch } from './utils';
22

3+
const PHONE_NUMBER_FORMAT = /^(\d{3})(\d{3})(\d{2,3})$/;
4+
const MOBILE_PHONE_NUMBER_FORMAT = /^(07\d)(\d{3})(\d{2})(\d{2})$/;
5+
/**
6+
* Format a phone number
7+
* @example
8+
* ```
9+
* formatPhoneNumber('07012345678') // => '070-123 45 678'
10+
* ```
11+
*/
12+
export function formatPhoneNumber(input: string): string {
13+
return replaceIfMatch(input, MOBILE_PHONE_NUMBER_FORMAT, '$1-$2 $3 $4');
14+
15+
if (number.length === 10) {
16+
return number.replace(PHONE_NUMBER_FORMAT_LONG, '$1 $2 $3 $4');
17+
}
18+
19+
return number.replace(PHONE_NUMBER_FORMAT, '$1 $2 $3');
20+
}
21+
322
const ORG_NUMBER_FORMAT = /^(\d{6})(\d{4})$/;
423

524
/**

0 commit comments

Comments
 (0)