Skip to content

Commit 944ee1c

Browse files
committed
format package
1 parent d5011bd commit 944ee1c

File tree

5 files changed

+61
-48
lines changed

5 files changed

+61
-48
lines changed

packages/format/README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# @obosbbl/format
22

3-
[![npm canary version](https://img.shields.io/npm/v/@obosbbl%2Fformat/canary.svg)](https://www.npmjs.com/package/@obosbbl/format)
3+
[![NPM Version](https://img.shields.io/npm/v/%40obosbbl%2Fformat)](https://www.npmjs.com/package/@obosbbl/format)
44

5-
Formatting functions for both 🇳🇴 and 🇸🇪 with zero dependencies.
5+
6+
A collection of formatting functions for both 🇳🇴 and 🇸🇪 with zero dependencies.
67

78
## Install
89

@@ -14,7 +15,25 @@ npm install @obosbbl/format
1415
pnpm add @obosbbl/format
1516
```
1617

18+
## Usage
19+
20+
The package has two entrypoints, one for `no` and one for `se`. That allows you to import for only the locale you need.
21+
22+
Note that the methods are very lenient when attempting to format the input. It will strip all characters that aren't digits or letters before it applies the formatting.
23+
That way any existing format of the input won't affect the formatted output
24+
25+
If unable to format the input, the method will return the (cleaned) input as is.
26+
27+
```js
28+
// 🇳🇴 example
29+
import { formatOrganizationNumber } from '@obosbbl/format/no';
30+
formatOrganizationNumber('000000000') // => '000 000 000'
31+
32+
// 🇸🇪 example
33+
import { formatOrganizationNumber } from '@obosbbl/format/se';
34+
formatOrganizationNumber('0000000000') // => '000000-0000'
35+
```
36+
1737
## Methods
1838

19-
* organizationNumberFormatter
20-
* obosMembershipNumberFormatter
39+
* formatOrganizationNumber

packages/format/src/format.test.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
import { describe, expect, test } from 'vitest';
2-
import { formatOrganizationNumber, formatPhoneNumber } from '.';
2+
import { formatOrganizationNumber as formatOrganizationNumberNo } from './no';
3+
import { formatOrganizationNumber as formatOrganizationNumberSe } from './se';
34

45
describe('no', () => {
56
test.each([
6-
['22865500', '22 86 55 00'],
7-
['80000000', '800 00 000'],
8-
])('formatPhoneNumber(%s) -> %s', (input, expected) => {
9-
expect(formatPhoneNumber(input, 'no')).toBe(expected);
7+
['000000000', '000 000 000'],
8+
['000 000 000', '000 000 000'],
9+
['000-000-000', '000 000 000'],
10+
['abc', 'abc'],
11+
])('formatOrganizationNumber(%s) -> %s', (input, expected) => {
12+
expect(formatOrganizationNumberNo(input)).toBe(expected);
1013
});
11-
12-
test.each([['000000000', '000 000 000']])(
13-
'formatOrganizationNumber(%s) -> %s',
14-
(input, expected) => {
15-
expect(formatOrganizationNumber(input, 'no')).toBe(expected);
16-
},
17-
);
1814
});
1915

2016
describe('se', () => {
21-
test.each([['0701234567', '070-123 45 67']])(
22-
'formatPhoneNumber(%s) -> %s',
23-
(input, expected) => {
24-
expect(formatPhoneNumber(input, 'se')).toBe(expected);
25-
},
26-
);
27-
28-
test.each([['0000000000', '000000-0000']])(
29-
'formatOrganizationNumber(%s) -> %s',
30-
(input, expected) => {
31-
expect(formatOrganizationNumber(input, 'se')).toBe(expected);
32-
},
33-
);
17+
test.each([
18+
['0000000000', '000000-0000'],
19+
['000000-0000', '000000-0000'],
20+
['000000 0000', '000000-0000'],
21+
[' 000000 0000 ', '000000-0000'],
22+
['000', '000'],
23+
['abc', 'abc'],
24+
])('formatOrganizationNumber(%s) -> %s', (input, expected) => {
25+
expect(formatOrganizationNumberSe(input)).toBe(expected);
26+
});
3427
});

packages/format/src/no.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { replaceIfMatch } from './utils';
2+
13
const ORG_NUMBER_FORMAT = /^(\d{3})(\d{3})(\d{3})$/;
24

35
/**
@@ -7,19 +9,6 @@ const ORG_NUMBER_FORMAT = /^(\d{3})(\d{3})(\d{3})$/;
79
* formatOrganizationNumber('000000000') // => '000 000 000'
810
* ```
911
*/
10-
export function formatOrganizationNumber(number: string): string {
11-
return number.replace(ORG_NUMBER_FORMAT, '$1 $2 $3');
12-
}
13-
14-
const OBOS_MEMBERSHIP_NUMBER_FORMAT = /^(\d{3})(\d{2})(\d{2})$/;
15-
16-
/**
17-
* Format an OBOS membership number
18-
* @example
19-
* ```
20-
* formatObosMembershipNumber('0000000') // => '000 00 00'
21-
* ```
22-
*/
23-
export function formatObosMembershipNumber(number: string): string {
24-
return number.replace(OBOS_MEMBERSHIP_NUMBER_FORMAT, '$1 $2 $3');
12+
export function formatOrganizationNumber(input: string): string {
13+
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1 $2 $3');
2514
}

packages/format/src/se.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
// Ensures feature parity with the Norwegian format module
2-
export { formatObosMembershipNumber } from './no';
1+
import { replaceIfMatch } from './utils';
32

43
const ORG_NUMBER_FORMAT = /^(\d{6})(\d{4})$/;
4+
55
/**
66
* Format an organization number
77
* @example
88
* ```
99
* formatOrganizationNumber('0000000000') // => '000000-0000'
1010
* ```
1111
*/
12-
export function formatOrganizationNumber(number: string): string {
13-
return number.replace(ORG_NUMBER_FORMAT, '$1-$2');
12+
export function formatOrganizationNumber(input: string): string {
13+
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1-$2');
1414
}

packages/format/src/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export function replaceIfMatch(
2+
input: string,
3+
regex: RegExp,
4+
replacerPattern: string,
5+
): string {
6+
// We're extremely lenient when attemtping to format the input.
7+
// We remove everything that isn't a letter or a number, that way we can get rid of any
8+
// formatting that might already be present in the input, eg spaces, hyphens or dots
9+
const cleaned = input.replace(/[^a-zA-Z0-9]/g, '');
10+
11+
return cleaned.replace(regex, replacerPattern);
12+
}

0 commit comments

Comments
 (0)