Skip to content

Commit a92336d

Browse files
authored
format: add default entrypoint (#8)
1 parent e829c50 commit a92336d

File tree

6 files changed

+116
-6
lines changed

6 files changed

+116
-6
lines changed

.changeset/witty-items-drive.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
"@obosbbl/format": minor
3+
---
4+
5+
add default entrypoint for formatting functions.
6+
7+
This makes it easier to support both Norwegian and Swedish formatting in the same project,
8+
but it requires you to specify the wanted locale as an options argument to the method.
9+
10+
Example:
11+
```js
12+
// Combined 🇳🇴🇸🇪 example
13+
import { formatOrganizationNumber } from '@obosbbl/format';
14+
formatOrganizationNumber('000000000', { locale: 'no' }) // => '000 000 000'
15+
formatOrganizationNumber('0000000000', { locale: 'se' }) // => '000000-0000'
16+
17+
// 🇳🇴 only example
18+
import { formatOrganizationNumber } from '@obosbbl/format/no';
19+
formatOrganizationNumber('000000000') // => '000 000 000'
20+
21+
// 🇸🇪 only example
22+
import { formatOrganizationNumber } from '@obosbbl/format/se';
23+
formatOrganizationNumber('0000000000') // => '000000-0000'
24+
```

packages/format/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@ pnpm add @obosbbl/format
1717

1818
## Usage
1919

20-
The package has two entrypoints, one for `no` and one for `se`. That allows you to import for only the locale you need.
20+
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.
2121

2222
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.
2323
That way any existing format of the input won't affect the formatted output
2424

2525
If unable to format the input, the method will return the (cleaned) input as is.
2626

2727
```js
28-
// 🇳🇴 example
28+
// Combined 🇳🇴🇸🇪 example
29+
import { formatOrganizationNumber } from '@obosbbl/format';
30+
formatOrganizationNumber('000000000', { locale: 'no' }) // => '000 000 000'
31+
formatOrganizationNumber('0000000000', { locale: 'se' }) // => '000000-0000'
32+
33+
// 🇳🇴 only example
2934
import { formatOrganizationNumber } from '@obosbbl/format/no';
3035
formatOrganizationNumber('000000000') // => '000 000 000'
3136

32-
// 🇸🇪 example
37+
// 🇸🇪 only example
3338
import { formatOrganizationNumber } from '@obosbbl/format/se';
3439
formatOrganizationNumber('0000000000') // => '000000-0000'
3540
```

packages/format/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
"sideEffects": false,
1515
"type": "module",
1616
"exports": {
17+
".": {
18+
"types": "./dist/index.d.mts",
19+
"default": "./dist/index.mjs"
20+
},
1721
"./no": {
1822
"types": "./dist/no.d.mts",
1923
"default": "./dist/no.mjs"

packages/format/src/index.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
formatObosMembershipNumber as _formatObosMembershipNumber,
3+
formatOrganizationNumber as formatOrganizationNumberNo,
4+
formatPhoneNumber as formatPhoneNumberNo,
5+
formatPostalCode as formatPostalCodeNo,
6+
} from './no';
7+
import {
8+
formatOrganizationNumber as formatOrganizationNumberSe,
9+
formatPhoneNumber as formatPhoneNumberSe,
10+
formatPostalCode as formatPostalCodeSe,
11+
} from './se';
12+
13+
export type Locale = 'no' | 'se';
14+
15+
type Options = {
16+
locale: Locale;
17+
};
18+
19+
/**
20+
* Format a phone number
21+
* @example
22+
* ```
23+
* formatPhoneNumber('00000000', { locale: 'no' }) // => '00 00 00 00'
24+
* formatPhoneNumber('07012345678', { locale: 'se' }) // => '070-123 45 678'
25+
* ```
26+
*/
27+
export function formatPhoneNumber(input: string, options: Options): string {
28+
return options.locale === 'no'
29+
? formatPhoneNumberNo(input)
30+
: formatPhoneNumberSe(input);
31+
}
32+
33+
/**
34+
* Format an organization number
35+
* @example
36+
* ```
37+
* formatOrganizationNumber('000000000', { locale: 'no' }) // => '000 000 000'
38+
* formatOrganizationNumber('0000000000', { locale: 'se' }) // => '000000-0000'
39+
* ```
40+
*/
41+
export function formatOrganizationNumber(
42+
input: string,
43+
options: Options,
44+
): string {
45+
return options.locale === 'no'
46+
? formatOrganizationNumberNo(input)
47+
: formatOrganizationNumberSe(input);
48+
}
49+
50+
/**
51+
* Format a postal code
52+
* @example
53+
* ```
54+
* formatPostalCode('0000', { locale: 'no' }) // => '0000'
55+
* formatPostalCode('00000', { locale: 'se' }) // => '000 00'
56+
* ```
57+
*/
58+
export function formatPostalCode(input: string, options: Options): string {
59+
return options.locale === 'no'
60+
? formatPostalCodeNo(input)
61+
: formatPostalCodeSe(input);
62+
}
63+
64+
/**
65+
* Format an OBOS membership number
66+
* @example
67+
* ```
68+
* formatObosMembershipNumber('0000000', { locale: 'no' }) // => '000 00 00'
69+
* formatObosMembershipNumber('0000000', { locale: 'se' }) // => '000 00 00'
70+
* ```
71+
*/
72+
export function formatObosMembershipNumber(
73+
input: string,
74+
options: Options,
75+
): string {
76+
// this is the same for no/se. But we want the APIs to be consistent...
77+
return _formatObosMembershipNumber(input);
78+
}

packages/format/src/no.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ const POSTAL_CODE_FORMAT = /^(\d{4})$/;
6161

6262
/**
6363
* Format a postal code
64-
*
6564
* @example
6665
* ```
67-
* format('0000') // => '0000'
66+
* formatPostalCode('0000') // => '0000'
6867
* ```
6968
*/
7069
export function formatPostalCode(input: string): string {

packages/format/src/se.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const POSTAL_CODE_FORMAT = /^(\d{3})(\d{2})$/;
7474
* Format a postal code
7575
* @example
7676
* ```
77-
* format('00000') // => '000 00'
77+
* formatPostalCode('00000') // => '000 00'
7878
* ```
7979
*/
8080
export function formatPostalCode(input: string): string {

0 commit comments

Comments
 (0)