Skip to content

Commit b4d2228

Browse files
committed
format: add formatPostalCode method
1 parent 884790a commit b4d2228

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,16 @@ export function formatObosMembershipNumber(input: string): string {
5757
return replaceIfMatch(input, OBOS_MEMBERSHIP_NUMBER_FORMAT, '$1 $2 $3');
5858
}
5959

60+
const POSTAL_CODE_FORMAT = /^(\d{4})$/;
61+
6062
/**
6163
* Format a postal code
64+
*
6265
* @example
6366
* ```
6467
* format('0000') // => '0000'
6568
* ```
6669
*/
6770
export function formatPostalCode(input: string): string {
68-
return input;
71+
return replaceIfMatch(input, POSTAL_CODE_FORMAT, '$1');
6972
}

packages/format/src/se.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ 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+
7173
/**
7274
* Format a postal code
7375
* @example
@@ -76,7 +78,7 @@ export function formatOrganizationNumber(input: string): string {
7678
* ```
7779
*/
7880
export function formatPostalCode(input: string): string {
79-
return input;
81+
return replaceIfMatch(input, POSTAL_CODE_FORMAT, '$1 $2');
8082
}
8183

8284
// just reexport the no method for API feature parity

0 commit comments

Comments
 (0)