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 b53be2d..c4ba271 100644 --- a/packages/format/src/no.ts +++ b/packages/format/src/no.ts @@ -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'); +} diff --git a/packages/format/src/se.ts b/packages/format/src/se.ts index be33c8c..d10713e 100644 --- a/packages/format/src/se.ts +++ b/packages/format/src/se.ts @@ -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';