Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/fresh-otters-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@obosbbl/format': minor
---

Add Norwegian account number formatting.
1 change: 1 addition & 0 deletions packages/format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ formatOrganizationNumber('0000000000') // => '000000-0000'

* formatPhoneNumber
* formatOrganizationNumber
* formatAccountNumber
* formatObosMembershipNumber
* formatPostalCode
10 changes: 10 additions & 0 deletions packages/format/src/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, test } from 'vitest';
import {
formatAccountNumber as formatAccountNumberNo,
formatObosMembershipNumber as formatObosMembershipNumberNo,
formatOrganizationNumber as formatOrganizationNumberNo,
formatPhoneNumber as formatPhoneNumberNo,
Expand Down Expand Up @@ -31,6 +32,15 @@ describe('no', () => {
expect(formatOrganizationNumberNo(input)).toBe(expected);
});

test.each([
['00000000000', '0000 00 00000'],
['0000 00 00000', '0000 00 00000'],
['0000.00.00000', '0000 00 00000'],
['abc', 'abc'],
])('formatAccountNumber(%s) -> %s', (input, expected) => {
expect(formatAccountNumberNo(input)).toBe(expected);
});

test.each([['0000', '0000']])(
'formatPostalCode(%s) -> %s',
(input, expected) => {
Expand Down
20 changes: 20 additions & 0 deletions packages/format/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
formatAccountNumber as _formatAccountNumber,
formatObosMembershipNumber as _formatObosMembershipNumber,
formatOrganizationNumber as formatOrganizationNumberNo,
formatPhoneNumber as formatPhoneNumberNo,
Expand All @@ -16,6 +17,10 @@ type Options = {
locale: Locale;
};

type NorwegianOptions = {
locale: 'no';
};

/**
* Format a phone number.
*
Expand Down Expand Up @@ -51,6 +56,21 @@ export function formatOrganizationNumber(
: formatOrganizationNumberSe(input);
}

/**
* Format a Norwegian account number
* @example
* ```
* formatAccountNumber('00000000000', { locale: 'no' }) // => '0000 00 00000'
* ```
*/
export function formatAccountNumber(
input: string,
options: NorwegianOptions,
): string {
// This is currently a Norwegian-only formatter. Keep the options argument for API consistency.
return _formatAccountNumber(input);
}

/**
* Format a postal code
* @example
Expand Down
13 changes: 13 additions & 0 deletions packages/format/src/no.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ export function formatOrganizationNumber(input: string): string {
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1 $2 $3');
}

const ACCOUNT_NUMBER_FORMAT = /^(\d{4})(\d{2})(\d{5})$/;

/**
* Format a Norwegian account number
* @example
* ```
* formatAccountNumber('00000000000') // => '0000 00 00000'
* ```
*/
export function formatAccountNumber(input: string): string {
return replaceIfMatch(input, ACCOUNT_NUMBER_FORMAT, '$1 $2 $3');
}

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

/**
Expand Down