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
24 changes: 24 additions & 0 deletions .changeset/witty-items-drive.md
Original file line number Diff line number Diff line change
@@ -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'
```
11 changes: 8 additions & 3 deletions packages/format/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,24 @@ 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

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'
```
Expand Down
4 changes: 4 additions & 0 deletions packages/format/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
78 changes: 78 additions & 0 deletions packages/format/src/index.ts
Original file line number Diff line number Diff line change
@@ -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);
}
3 changes: 1 addition & 2 deletions packages/format/src/no.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/format/src/se.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading