Skip to content

Commit 489b275

Browse files
authored
format: add formatObosMembershipNumber (#3)
1 parent a4938b2 commit 489b275

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

packages/format/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ formatOrganizationNumber('0000000000') // => '000000-0000'
3737
## Methods
3838

3939
* formatOrganizationNumber
40+
* formatObosMembershipNumber

packages/format/src/format.test.ts

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

511
describe('no', () => {
612
test.each([
@@ -25,3 +31,14 @@ describe('se', () => {
2531
expect(formatOrganizationNumberSe(input)).toBe(expected);
2632
});
2733
});
34+
35+
test.each([
36+
['0000000', '000 00 00'],
37+
['000 00 00', '000 00 00'],
38+
['000-00-00', '000 00 00'],
39+
])('formatObosMembershipNumber(%s) -> %s', (input, expected) => {
40+
// don't split these by country, since they're essentially the same method
41+
// we still test both though, to ensure there's no difference between them
42+
expect(formatObosMembershipNumberNo(input)).toBe(expected);
43+
expect(formatObosMembershipNumberSe(input)).toBe(expected);
44+
});

packages/format/src/no.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,16 @@ const ORG_NUMBER_FORMAT = /^(\d{3})(\d{3})(\d{3})$/;
1212
export function formatOrganizationNumber(input: string): string {
1313
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1 $2 $3');
1414
}
15+
16+
const OBOS_MEMBERSHIP_NUMBER_FORMAT = /^(\d{3})(\d{2})(\d{2})$/;
17+
18+
/**
19+
* Format an OBOS membership number
20+
* @example
21+
* ```
22+
* formatObosMembershipNumber('0000000') // => '000 00 00'
23+
* ```
24+
*/
25+
export function formatObosMembershipNumber(input: string): string {
26+
return replaceIfMatch(input, OBOS_MEMBERSHIP_NUMBER_FORMAT, '$1 $2 $3');
27+
}

packages/format/src/se.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ const ORG_NUMBER_FORMAT = /^(\d{6})(\d{4})$/;
1212
export function formatOrganizationNumber(input: string): string {
1313
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1-$2');
1414
}
15+
16+
// just reexport the no method for API feature parity
17+
export { formatObosMembershipNumber } from './no';

0 commit comments

Comments
 (0)