Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ formatOrganizationNumber('0000000000') // => '000000-0000'
## Methods

* formatOrganizationNumber
* formatObosMembershipNumber
21 changes: 19 additions & 2 deletions packages/format/src/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { describe, expect, test } from 'vitest';
import { formatOrganizationNumber as formatOrganizationNumberNo } from './no';
import { formatOrganizationNumber as formatOrganizationNumberSe } from './se';
import {
formatObosMembershipNumber as formatObosMembershipNumberNo,
formatOrganizationNumber as formatOrganizationNumberNo,
} from './no';
import {
formatObosMembershipNumber as formatObosMembershipNumberSe,
formatOrganizationNumber as formatOrganizationNumberSe,
} from './se';

describe('no', () => {
test.each([
Expand All @@ -25,3 +31,14 @@ describe('se', () => {
expect(formatOrganizationNumberSe(input)).toBe(expected);
});
});

test.each([
['0000000', '000 00 00'],
['000 00 00', '000 00 00'],
['000-00-00', '000 00 00'],
])('formatObosMembershipNumber(%s) -> %s', (input, expected) => {
// don't split these by country, since they're essentially the same method
// we still test both though, to ensure there's no difference between them
expect(formatObosMembershipNumberNo(input)).toBe(expected);
expect(formatObosMembershipNumberSe(input)).toBe(expected);
});
13 changes: 13 additions & 0 deletions packages/format/src/no.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,16 @@ const ORG_NUMBER_FORMAT = /^(\d{3})(\d{3})(\d{3})$/;
export function formatOrganizationNumber(input: string): string {
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1 $2 $3');
}

const OBOS_MEMBERSHIP_NUMBER_FORMAT = /^(\d{3})(\d{2})(\d{2})$/;

/**
* Format an OBOS membership number
* @example
* ```
* formatObosMembershipNumber('0000000') // => '000 00 00'
* ```
*/
export function formatObosMembershipNumber(input: string): string {
return replaceIfMatch(input, OBOS_MEMBERSHIP_NUMBER_FORMAT, '$1 $2 $3');
}
3 changes: 3 additions & 0 deletions packages/format/src/se.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ const ORG_NUMBER_FORMAT = /^(\d{6})(\d{4})$/;
export function formatOrganizationNumber(input: string): string {
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1-$2');
}

// just reexport the no method for API feature parity
export { formatObosMembershipNumber } from './no';
Loading