diff --git a/packages/format/README.md b/packages/format/README.md index 05d87b2..e7f2cb4 100644 --- a/packages/format/README.md +++ b/packages/format/README.md @@ -37,3 +37,4 @@ formatOrganizationNumber('0000000000') // => '000000-0000' ## Methods * formatOrganizationNumber +* formatObosMembershipNumber diff --git a/packages/format/src/format.test.ts b/packages/format/src/format.test.ts index 919cbd9..b470766 100644 --- a/packages/format/src/format.test.ts +++ b/packages/format/src/format.test.ts @@ -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([ @@ -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); +}); diff --git a/packages/format/src/no.ts b/packages/format/src/no.ts index c2e823f..bd59b6f 100644 --- a/packages/format/src/no.ts +++ b/packages/format/src/no.ts @@ -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'); +} diff --git a/packages/format/src/se.ts b/packages/format/src/se.ts index 363ce12..be1f380 100644 --- a/packages/format/src/se.ts +++ b/packages/format/src/se.ts @@ -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';