Skip to content

Commit c856db7

Browse files
authored
format: add formatPostalCode method (#7)
1 parent 21a8fb3 commit c856db7

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

.changeset/wicked-tools-provide.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@obosbbl/format": minor
3+
---
4+
5+
add `formatPostalCode` method.
6+
7+
```js
8+
import { formatPostalCode} from '@obosbbl/format/no';
9+
10+
formatPostalCode('0000') // => '0000'
11+
12+
import { formatPostalCode } from '@obosbbl/format/se';
13+
14+
formatPhoneNumber('00000') // => '000 00'
15+
```

packages/format/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ formatOrganizationNumber('0000000000') // => '000000-0000'
3939
* formatPhoneNumber
4040
* formatOrganizationNumber
4141
* formatObosMembershipNumber
42+
* formatPostalCode

packages/format/src/format.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import {
33
formatObosMembershipNumber as formatObosMembershipNumberNo,
44
formatOrganizationNumber as formatOrganizationNumberNo,
55
formatPhoneNumber as formatPhoneNumberNo,
6+
formatPostalCode as formatPostalCodeNo,
67
} from './no';
78
import {
89
formatObosMembershipNumber as formatObosMembershipNumberSe,
910
formatOrganizationNumber as formatOrganizationNumberSe,
1011
formatPhoneNumber as formatPhoneNumberSe,
12+
formatPostalCode as formatPostalCodeSe,
1113
} from './se';
1214

1315
describe('no', () => {
@@ -26,6 +28,13 @@ describe('no', () => {
2628
])('formatOrganizationNumber(%s) -> %s', (input, expected) => {
2729
expect(formatOrganizationNumberNo(input)).toBe(expected);
2830
});
31+
32+
test.each([['0000', '0000']])(
33+
'formatPostalCode(%s) -> %s',
34+
(input, expected) => {
35+
expect(formatPostalCodeNo(input)).toBe(expected);
36+
},
37+
);
2938
});
3039

3140
describe('se', () => {
@@ -64,6 +73,13 @@ describe('se', () => {
6473
])('formatOrganizationNumber(%s) -> %s', (input, expected) => {
6574
expect(formatOrganizationNumberSe(input)).toBe(expected);
6675
});
76+
77+
test.each([
78+
['00000', '000 00'],
79+
['000 00', '000 00'],
80+
])('formatPostalCode(%s) -> %s', (input, expected) => {
81+
expect(formatPostalCodeSe(input)).toBe(expected);
82+
});
6783
});
6884

6985
test.each([

packages/format/src/no.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ const OBOS_MEMBERSHIP_NUMBER_FORMAT = /^(\d{3})(\d{2})(\d{2})$/;
5656
export function formatObosMembershipNumber(input: string): string {
5757
return replaceIfMatch(input, OBOS_MEMBERSHIP_NUMBER_FORMAT, '$1 $2 $3');
5858
}
59+
60+
const POSTAL_CODE_FORMAT = /^(\d{4})$/;
61+
62+
/**
63+
* Format a postal code
64+
*
65+
* @example
66+
* ```
67+
* format('0000') // => '0000'
68+
* ```
69+
*/
70+
export function formatPostalCode(input: string): string {
71+
return replaceIfMatch(input, POSTAL_CODE_FORMAT, '$1');
72+
}

packages/format/src/se.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,18 @@ export function formatOrganizationNumber(input: string): string {
6868
return replaceIfMatch(input, ORG_NUMBER_FORMAT, '$1-$2');
6969
}
7070

71+
const POSTAL_CODE_FORMAT = /^(\d{3})(\d{2})$/;
72+
73+
/**
74+
* Format a postal code
75+
* @example
76+
* ```
77+
* format('00000') // => '000 00'
78+
* ```
79+
*/
80+
export function formatPostalCode(input: string): string {
81+
return replaceIfMatch(input, POSTAL_CODE_FORMAT, '$1 $2');
82+
}
83+
7184
// just reexport the no method for API feature parity
7285
export { formatObosMembershipNumber } from './no';

0 commit comments

Comments
 (0)