diff --git a/.changeset/witty-items-drive.md b/.changeset/witty-items-drive.md new file mode 100644 index 0000000..f42fb0f --- /dev/null +++ b/.changeset/witty-items-drive.md @@ -0,0 +1,24 @@ +--- +"@obosbbl/format": minor +--- + +add default entrypoint for formatting functions. + +This makes it easier to support both Norwegian and Swedish formatting in the same project, +but it requires you to specify the wanted locale as an options argument to the method. + +Example: +```js +// Combined πŸ‡³πŸ‡΄πŸ‡ΈπŸ‡ͺ example +import { formatOrganizationNumber } from '@obosbbl/format'; +formatOrganizationNumber('000000000', { locale: 'no' }) // => '000 000 000' +formatOrganizationNumber('0000000000', { locale: 'se' }) // => '000000-0000' + +// πŸ‡³πŸ‡΄ only example +import { formatOrganizationNumber } from '@obosbbl/format/no'; +formatOrganizationNumber('000000000') // => '000 000 000' + +// πŸ‡ΈπŸ‡ͺ only example +import { formatOrganizationNumber } from '@obosbbl/format/se'; +formatOrganizationNumber('0000000000') // => '000000-0000' +``` diff --git a/packages/format/README.md b/packages/format/README.md index e2df9ee..9b35131 100644 --- a/packages/format/README.md +++ b/packages/format/README.md @@ -17,7 +17,7 @@ pnpm add @obosbbl/format ## Usage -The package has two entrypoints, one for `no` and one for `se`. That allows you to import for only the locale you need. +The package has three entrypoints. The default entrypoints requires you to specify the locale as an options argument to the method. The package also exports entrypoints for `no` and `se` only methods. That allows you to import for only the locale you support if need be. Note that the methods are very lenient when attempting to format the input. It will strip all characters that aren't digits or letters before it applies the formatting. That way any existing format of the input won't affect the formatted output @@ -25,11 +25,16 @@ That way any existing format of the input won't affect the formatted output If unable to format the input, the method will return the (cleaned) input as is. ```js -// πŸ‡³πŸ‡΄ example +// Combined πŸ‡³πŸ‡΄πŸ‡ΈπŸ‡ͺ example +import { formatOrganizationNumber } from '@obosbbl/format'; +formatOrganizationNumber('000000000', { locale: 'no' }) // => '000 000 000' +formatOrganizationNumber('0000000000', { locale: 'se' }) // => '000000-0000' + +// πŸ‡³πŸ‡΄ only example import { formatOrganizationNumber } from '@obosbbl/format/no'; formatOrganizationNumber('000000000') // => '000 000 000' -// πŸ‡ΈπŸ‡ͺ example +// πŸ‡ΈπŸ‡ͺ only example import { formatOrganizationNumber } from '@obosbbl/format/se'; formatOrganizationNumber('0000000000') // => '000000-0000' ``` diff --git a/packages/format/package.json b/packages/format/package.json index f00ddd7..d504431 100644 --- a/packages/format/package.json +++ b/packages/format/package.json @@ -14,6 +14,10 @@ "sideEffects": false, "type": "module", "exports": { + ".": { + "types": "./dist/index.d.mts", + "default": "./dist/index.mjs" + }, "./no": { "types": "./dist/no.d.mts", "default": "./dist/no.mjs" diff --git a/packages/format/src/index.ts b/packages/format/src/index.ts new file mode 100644 index 0000000..b7f6187 --- /dev/null +++ b/packages/format/src/index.ts @@ -0,0 +1,78 @@ +import { + formatObosMembershipNumber as _formatObosMembershipNumber, + formatOrganizationNumber as formatOrganizationNumberNo, + formatPhoneNumber as formatPhoneNumberNo, + formatPostalCode as formatPostalCodeNo, +} from './no'; +import { + formatOrganizationNumber as formatOrganizationNumberSe, + formatPhoneNumber as formatPhoneNumberSe, + formatPostalCode as formatPostalCodeSe, +} from './se'; + +export type Locale = 'no' | 'se'; + +type Options = { + locale: Locale; +}; + +/** + * Format a phone number + * @example + * ``` + * formatPhoneNumber('00000000', { locale: 'no' }) // => '00 00 00 00' + * formatPhoneNumber('07012345678', { locale: 'se' }) // => '070-123 45 678' + * ``` + */ +export function formatPhoneNumber(input: string, options: Options): string { + return options.locale === 'no' + ? formatPhoneNumberNo(input) + : formatPhoneNumberSe(input); +} + +/** + * Format an organization number + * @example + * ``` + * formatOrganizationNumber('000000000', { locale: 'no' }) // => '000 000 000' + * formatOrganizationNumber('0000000000', { locale: 'se' }) // => '000000-0000' + * ``` + */ +export function formatOrganizationNumber( + input: string, + options: Options, +): string { + return options.locale === 'no' + ? formatOrganizationNumberNo(input) + : formatOrganizationNumberSe(input); +} + +/** + * Format a postal code + * @example + * ``` + * formatPostalCode('0000', { locale: 'no' }) // => '0000' + * formatPostalCode('00000', { locale: 'se' }) // => '000 00' + * ``` + */ +export function formatPostalCode(input: string, options: Options): string { + return options.locale === 'no' + ? formatPostalCodeNo(input) + : formatPostalCodeSe(input); +} + +/** + * Format an OBOS membership number + * @example + * ``` + * formatObosMembershipNumber('0000000', { locale: 'no' }) // => '000 00 00' + * formatObosMembershipNumber('0000000', { locale: 'se' }) // => '000 00 00' + * ``` + */ +export function formatObosMembershipNumber( + input: string, + options: Options, +): string { + // this is the same for no/se. But we want the APIs to be consistent... + return _formatObosMembershipNumber(input); +} diff --git a/packages/format/src/no.ts b/packages/format/src/no.ts index c4ba271..4d22ad6 100644 --- a/packages/format/src/no.ts +++ b/packages/format/src/no.ts @@ -61,10 +61,9 @@ const POSTAL_CODE_FORMAT = /^(\d{4})$/; /** * Format a postal code - * * @example * ``` - * format('0000') // => '0000' + * formatPostalCode('0000') // => '0000' * ``` */ export function formatPostalCode(input: string): string { diff --git a/packages/format/src/se.ts b/packages/format/src/se.ts index d10713e..c233b9d 100644 --- a/packages/format/src/se.ts +++ b/packages/format/src/se.ts @@ -74,7 +74,7 @@ const POSTAL_CODE_FORMAT = /^(\d{3})(\d{2})$/; * Format a postal code * @example * ``` - * format('00000') // => '000 00' + * formatPostalCode('00000') // => '000 00' * ``` */ export function formatPostalCode(input: string): string {