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
15 changes: 15 additions & 0 deletions .changeset/wicked-tools-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@obosbbl/format": minor
---

add `formatPostalCode` method.

```js
import { formatPostalCode} from '@obosbbl/format/no';

formatPostalCode('0000') // => '0000'

import { formatPostalCode } from '@obosbbl/format/se';

formatPhoneNumber('00000') // => '000 00'
```
1 change: 1 addition & 0 deletions packages/format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ formatOrganizationNumber('0000000000') // => '000000-0000'
* formatPhoneNumber
* formatOrganizationNumber
* formatObosMembershipNumber
* formatPostalCode
16 changes: 16 additions & 0 deletions packages/format/src/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import {
formatObosMembershipNumber as formatObosMembershipNumberNo,
formatOrganizationNumber as formatOrganizationNumberNo,
formatPhoneNumber as formatPhoneNumberNo,
formatPostalCode as formatPostalCodeNo,
} from './no';
import {
formatObosMembershipNumber as formatObosMembershipNumberSe,
formatOrganizationNumber as formatOrganizationNumberSe,
formatPhoneNumber as formatPhoneNumberSe,
formatPostalCode as formatPostalCodeSe,
} from './se';

describe('no', () => {
Expand All @@ -26,6 +28,13 @@ describe('no', () => {
])('formatOrganizationNumber(%s) -> %s', (input, expected) => {
expect(formatOrganizationNumberNo(input)).toBe(expected);
});

test.each([['0000', '0000']])(
'formatPostalCode(%s) -> %s',
(input, expected) => {
expect(formatPostalCodeNo(input)).toBe(expected);
},
);
});

describe('se', () => {
Expand Down Expand Up @@ -64,6 +73,13 @@ describe('se', () => {
])('formatOrganizationNumber(%s) -> %s', (input, expected) => {
expect(formatOrganizationNumberSe(input)).toBe(expected);
});

test.each([
['00000', '000 00'],
['000 00', '000 00'],
])('formatPostalCode(%s) -> %s', (input, expected) => {
expect(formatPostalCodeSe(input)).toBe(expected);
});
});

test.each([
Expand Down
14 changes: 14 additions & 0 deletions packages/format/src/no.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,17 @@ const OBOS_MEMBERSHIP_NUMBER_FORMAT = /^(\d{3})(\d{2})(\d{2})$/;
export function formatObosMembershipNumber(input: string): string {
return replaceIfMatch(input, OBOS_MEMBERSHIP_NUMBER_FORMAT, '$1 $2 $3');
}

const POSTAL_CODE_FORMAT = /^(\d{4})$/;

/**
* Format a postal code
*
* @example
* ```
* format('0000') // => '0000'
* ```
*/
export function formatPostalCode(input: string): string {
return replaceIfMatch(input, POSTAL_CODE_FORMAT, '$1');
}
13 changes: 13 additions & 0 deletions packages/format/src/se.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,18 @@ export function formatOrganizationNumber(input: string): string {
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1-$2');
}

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

/**
* Format a postal code
* @example
* ```
* format('00000') // => '000 00'
* ```
*/
export function formatPostalCode(input: string): string {
return replaceIfMatch(input, POSTAL_CODE_FORMAT, '$1 $2');
}

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