From 884790a97865b132cba0c821f61bdc68e2dc7585 Mon Sep 17 00:00:00 2001 From: Alexander Bjerkan Date: Mon, 27 Jan 2025 14:26:18 +0100 Subject: [PATCH 1/2] formatpostalcode --- packages/format/src/no.ts | 11 +++++++++++ packages/format/src/se.ts | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/format/src/no.ts b/packages/format/src/no.ts index b53be2d..1e1ee99 100644 --- a/packages/format/src/no.ts +++ b/packages/format/src/no.ts @@ -56,3 +56,14 @@ 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'); } + +/** + * Format a postal code + * @example + * ``` + * format('0000') // => '0000' + * ``` + */ +export function formatPostalCode(input: string): string { + return input; +} diff --git a/packages/format/src/se.ts b/packages/format/src/se.ts index be33c8c..e24f765 100644 --- a/packages/format/src/se.ts +++ b/packages/format/src/se.ts @@ -68,5 +68,16 @@ export function formatOrganizationNumber(input: string): string { return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1-$2'); } +/** + * Format a postal code + * @example + * ``` + * format('00000') // => '000 00' + * ``` + */ +export function formatPostalCode(input: string): string { + return input; +} + // just reexport the no method for API feature parity export { formatObosMembershipNumber } from './no'; From b4d222882049c8140bb668443701dfdcddfc3982 Mon Sep 17 00:00:00 2001 From: Alexander Bjerkan Date: Tue, 28 Jan 2025 12:22:21 +0100 Subject: [PATCH 2/2] format: add formatPostalCode method --- .changeset/wicked-tools-provide.md | 15 +++++++++++++++ packages/format/README.md | 1 + packages/format/src/format.test.ts | 16 ++++++++++++++++ packages/format/src/no.ts | 5 ++++- packages/format/src/se.ts | 4 +++- 5 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 .changeset/wicked-tools-provide.md diff --git a/.changeset/wicked-tools-provide.md b/.changeset/wicked-tools-provide.md new file mode 100644 index 0000000..1da9346 --- /dev/null +++ b/.changeset/wicked-tools-provide.md @@ -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' +``` diff --git a/packages/format/README.md b/packages/format/README.md index aefd172..e2df9ee 100644 --- a/packages/format/README.md +++ b/packages/format/README.md @@ -39,3 +39,4 @@ formatOrganizationNumber('0000000000') // => '000000-0000' * formatPhoneNumber * formatOrganizationNumber * formatObosMembershipNumber +* formatPostalCode diff --git a/packages/format/src/format.test.ts b/packages/format/src/format.test.ts index c5650d8..aa15f0c 100644 --- a/packages/format/src/format.test.ts +++ b/packages/format/src/format.test.ts @@ -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', () => { @@ -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', () => { @@ -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([ diff --git a/packages/format/src/no.ts b/packages/format/src/no.ts index 1e1ee99..c4ba271 100644 --- a/packages/format/src/no.ts +++ b/packages/format/src/no.ts @@ -57,13 +57,16 @@ 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 input; + return replaceIfMatch(input, POSTAL_CODE_FORMAT, '$1'); } diff --git a/packages/format/src/se.ts b/packages/format/src/se.ts index e24f765..d10713e 100644 --- a/packages/format/src/se.ts +++ b/packages/format/src/se.ts @@ -68,6 +68,8 @@ 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 @@ -76,7 +78,7 @@ export function formatOrganizationNumber(input: string): string { * ``` */ export function formatPostalCode(input: string): string { - return input; + return replaceIfMatch(input, POSTAL_CODE_FORMAT, '$1 $2'); } // just reexport the no method for API feature parity