Skip to content

Commit cd1dbba

Browse files
committed
formatphonenumber
1 parent 489b275 commit cd1dbba

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

packages/format/src/no.ts

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

3+
// See https://sprakradet.no/godt-og-korrekt-sprak/rettskriving-og-grammatikk/tall-tid-dato/
4+
const REGULAR_PHONE_NUMBER_FORMAT = /^(\d{2})(\d{2})(\d{2})(\d{2})$/;
5+
const EIGHT_HUNDRED_SERIES_PHONE_NUMBER_FORMAT = /^(\d{3})(\d{2})(\d{3})$/;
6+
7+
/**
8+
* Format a phone number
9+
* @example
10+
* ```
11+
* formatPhoneNumber('00000000') // => '00 00 00 00'
12+
* formatPhoneNumber('80000000') // => '800 00 000'
13+
* ```
14+
*/
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+
}
19+
20+
return number.replace(DEFAULT_PHONE_NUMBER_FORMAT, '$1 $2 $3 $4');
21+
}
22+
323
const ORG_NUMBER_FORMAT = /^(\d{3})(\d{3})(\d{3})$/;
424

525
/**

0 commit comments

Comments
 (0)