Skip to content

Commit 6d782ab

Browse files
authored
Merge branch 'main' into renovate/pnpm-10.x
2 parents e2ec742 + a4a7471 commit 6d782ab

File tree

18 files changed

+825
-90
lines changed

18 files changed

+825
-90
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This a monorepo of OBOS' open source frontend modules.
66
## Packages
77

88
* [format](./packages/format)
9+
* [validation](./packages/validation)
910

1011
## Contributing
1112

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@arethetypeswrong/cli": "0.17.3",
1717
"@biomejs/biome": "1.9.4",
1818
"@changesets/cli": "2.27.12",
19-
"bunchee": "6.3.2",
19+
"bunchee": "6.4.0",
2020
"typescript": "5.7.3",
2121
"vitest": "3.0.5"
2222
},

packages/format/CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# @obosbbl/format
22

3+
## 0.3.1
4+
5+
### Patch Changes
6+
7+
- 3a52b13: allow country code for phone number input in `formatPhoneNumber()`.
8+
9+
Previously it was unable to format phone numbers with country codes (+47, +46), now it can.
10+
Note that the country code will _not_ be part of the formatted output.
11+
12+
```js
13+
// 🇳🇴 example
14+
formatPhoneNumber("+4700000000"); // => '00 00 00 00'
15+
16+
// 🇸🇪 example
17+
formatPhoneNumber("+46303123456"); // => '0303-12 34 56'
18+
```
19+
320
## 0.3.0
421

522
### Minor Changes

packages/format/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@obosbbl/format",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "A collection of formatting methods for OBOS",
55
"repository": {
66
"url": "https://github.com/code-obos/public-frontend-modules"

packages/format/src/format.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ describe('no', () => {
1616
test.each([
1717
['22865500', '22 86 55 00'],
1818
['80000000', '800 00 000'],
19+
// with country code
20+
['+4722865500', '22 86 55 00'],
1921
])('formatPhoneNumber(%s) -> %s', (input, expected) => {
2022
expect(formatPhoneNumberNo(input)).toBe(expected);
2123
});
@@ -57,6 +59,8 @@ describe('se', () => {
5759
['0303123456', '0303-12 34 56'],
5860
['03031234567', '0303-123 45 67'],
5961
['030312345678', '0303-123 456 78'],
62+
// with country code
63+
['+46303123456', '0303-12 34 56'],
6064
// invalid, too long a number
6165
['0303123456789', '0303123456789'],
6266
])('formatPhoneNumber(%s) -> %s', (input, expected) => {

packages/format/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ type Options = {
1717
};
1818

1919
/**
20-
* Format a phone number
20+
* Format a phone number.
21+
*
22+
* Country code can be present in the input, but it will be removed in the formatted output.
23+
*
2124
* @example
2225
* ```
2326
* formatPhoneNumber('00000000', { locale: 'no' }) // => '00 00 00 00'
2427
* formatPhoneNumber('07012345678', { locale: 'se' }) // => '070-123 45 678'
28+
* formatPhoneNumber('+4700000000', { locale: 'no' }) // => '00 00 00 00'
2529
* ```
2630
*/
2731
export function formatPhoneNumber(input: string, options: Options): string {

packages/format/src/no.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ const REGULAR_PHONE_NUMBER_FORMAT = /^(\d{2})(\d{2})(\d{2})(\d{2})$/;
77
const EIGHT_HUNDRED_SERIES_PHONE_NUMBER_FORMAT = /^(\d{3})(\d{2})(\d{3})$/;
88

99
/**
10-
* Format a phone number
10+
* Format a phone number.
11+
*
12+
* Country code can be present in the input, but it will be removed in the formatted output.
13+
*
1114
* @example
1215
* ```
1316
* formatPhoneNumber('00000000') // => '00 00 00 00'
1417
* formatPhoneNumber('80000000') // => '800 00 000'
18+
* formatPhoneNumber('+4700000000') // => '00 00 00 00'
1519
* ```
1620
*/
1721
export function formatPhoneNumber(input: string): string {
22+
// remove country code
23+
// biome-ignore lint/style/noParameterAssign:
24+
input = input.replace(/^\+47/, '');
25+
1826
const number = replaceIfMatch(
1927
input,
2028
REGULAR_PHONE_NUMBER_FORMAT,

packages/format/src/se.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ const THREE_DIGIT_AREA_CODE =
1414
/^0(11|13|16|18|19|21|23|26|31|33|35|36|40|42|44|46|54|60|63|90)/;
1515

1616
/**
17-
* Format a phone number
17+
* Format a phone number.
18+
*
19+
* Country code can be present in the input, but it will be removed in the formatted output.
20+
*
1821
* @example
1922
* ```
2023
* formatPhoneNumber('07012345678') // => '070-123 45 678'
2124
* formatPhoneNumber('0812345') // => '08-123 45'
2225
* formatPhoneNumber('0311234567') // => '031-123 45 67'
2326
* formatPhoneNumber('0303123456') // => '0303-12 34 56'
27+
* formatPhoneNumber('+46303123456') // => '0303-12 34 56'
2428
* ```
2529
*/
2630
export function formatPhoneNumber(input: string): string {
31+
// biome-ignore lint/style/noParameterAssign:
32+
input = input.replace(/^\+46/, '0');
33+
2734
const normalizedInput = cleanInput(input);
2835

2936
if (MOBILE_PHONE_NUMBER_FORMAT.test(normalizedInput)) {

packages/validation/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# @obosbbl/validation
2+
3+
## 0.2.0
4+
5+
### Minor Changes
6+
7+
- b444dd8: feat: add method `validateObosMembershipNumber()` to validate if the input value is a valid OBOS membership number.
8+
9+
## 0.1.0
10+
11+
### Minor Changes
12+
13+
- 66676fe: initial release

packages/validation/README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# @obosbbl/validation
2+
3+
[![NPM Version](https://img.shields.io/npm/v/%40obosbbl%2Fvalidation)](https://www.npmjs.com/package/@obosbbl/validation)
4+
5+
6+
A collection of validation methods for both 🇳🇴 and 🇸🇪 with zero dependencies.
7+
8+
## Install
9+
10+
```sh
11+
# npm
12+
npm install @obosbbl/validation
13+
14+
# pnpm
15+
pnpm add @obosbbl/validation
16+
```
17+
18+
## Usage
19+
20+
The package has two entrypoints, one for `no` and one for `se`. That allows you to import for only the locale you need.
21+
22+
23+
```js
24+
// 🇳🇴 example
25+
import { validateOrganizationNumber } from '@obosbbl/validation/no';
26+
validateOrganizationNumber('937052766') // => true
27+
28+
validateOrganizationNumber('000') // => false
29+
30+
// 🇸🇪 example
31+
import { validateOrganizationNumber } from '@obosbbl/validation/se';
32+
validateOrganizationNumber('5592221054') // => true
33+
34+
validateOrganizationNumber('000') // => false
35+
```
36+
37+
## Strictness and formatting characters
38+
39+
The methods are "strict" by default, meaning no formatting characters in the input is allowed.
40+
This is preferrable, for instance when doing server-side validation, where the input is often expected to be a "clean" value.
41+
42+
If you want to allow formatting characters in the input, you can pass `allowFormatting: true` in the options object to the method.
43+
Note that this currently allows any formatting characters, not just the just the "expected" ones for the input type.
44+
45+
46+
```js
47+
import { validateOrganizationNumber } from '@obosbbl/validation/no';
48+
49+
validateOrganizationNumber('937052766') // true
50+
51+
// formatting characters disallowed by default
52+
validateOrganizationNumber('937 052 766') // false;
53+
54+
// allow formatting characters
55+
validateOrganizationNumber('937 052 766', { allowFormatting: true }) // true;
56+
```
57+
58+
## Methods
59+
60+
* validatePostalCode
61+
* validatePhoneNumber
62+
* supports mobileOnly option
63+
* validateOrganizationNumber
64+
* Check digit verification is currently only implemented for Norwegian organization numbers. For Swedish organiation numbers, we only check the length of the input. PRs are welcome to fix this.
65+
* validateObosMembershipNumber
66+
67+
68+
## Example usage with Zod
69+
70+
```js
71+
import { z } from 'zod';
72+
import { validatePhoneNumber } from '@obosbbl/validation/no';
73+
74+
const mobileOnlySchema = z.object({
75+
name: z.string(),
76+
phoneNumber: z
77+
.string()
78+
.refine(
79+
(val) => validatePhoneNumber(val, { mobileOnly: true }),
80+
'Telefonnummeret er ikke et gyldig mobilnummer',
81+
),
82+
});
83+
84+
const validData = {
85+
name: 'Kari Nordmann',
86+
phoneNumber: '92345678',
87+
};
88+
89+
mobileOnlySchema.parse(validData); // => { name: 'Kari Nordmann', phoneNumber: '92345678' }
90+
91+
const invalidData = {
92+
name: 'Ola Nordmann',
93+
phoneNumber: '22865500',
94+
}
95+
96+
mobileOnlySchema.parse(invalidData); // => throws ZodError
97+
```

0 commit comments

Comments
 (0)