Skip to content

Commit 6643d93

Browse files
committed
remove h-number
1 parent d907fb1 commit 6643d93

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

packages/validation/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ validateOrganizationNumber('937 052 766', { allowFormatting: true }) // true;
5757

5858
## Methods
5959

60+
* validateNationalIdentityNumber
6061
* validatePostalCode
6162
* validatePhoneNumber
6263
* supports mobileOnly option

packages/validation/src/no.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,16 @@ export function validateObosMembershipNumber(
107107
type PersonalIdentityNumberOptions = ValidatorOptions;
108108

109109
/**
110-
* Validates that the input value is a Norwegian personal identity number.
110+
* Validates that the input value is a Norwegian national identity number.
111111
*
112-
* Supports both Fødselsnummer, D-numbers and H-numbers.
112+
* Supports both fødselsnummer and d-number.
113113
* @example
114114
* ```
115-
* validatePersonalIdentityNumber('0000000') // => true
115+
* validatePersonalIdentityNumber('21075417753') // => true
116+
* validatePersonalIdentityNumber('61075440676') // => true
116117
* ```
117118
*/
118-
export function validatePersonalIdentityNumber(
119+
export function validateNationalIdentityNumber(
119120
value: string,
120121
options: PersonalIdentityNumberOptions = {},
121122
): boolean {
@@ -124,6 +125,7 @@ export function validatePersonalIdentityNumber(
124125
value = stripFormatting(value);
125126
}
126127

128+
// Fødselsn
127129
// Fødselsnummer and d numbers have two control digits.
128130
// where the first one is calculated using the first 10 digits.
129131
const valueForControlDigit1 = value.slice(0, -1);
@@ -133,23 +135,18 @@ export function validatePersonalIdentityNumber(
133135
);
134136
const controlDigit2 = mod11(value, [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]);
135137

136-
if (!controlDigit1 && controlDigit2) {
138+
if (!controlDigit1 && !controlDigit2) {
137139
return false;
138140
}
139141

140142
let day = Number(value.substring(0, 2));
141-
let month = Number(value.substring(2, 4));
143+
const month = Number(value.substring(2, 4));
142144
const year = Number(value.substring(4, 6));
143145

144-
switch (true) {
145-
// d number
146-
case value[0] >= 4:
147-
day = day - 40;
148-
break;
149-
// h number
150-
case value[2] >= 4:
151-
month = month - 40;
152-
break;
146+
// for a d-number the first digit is increased by 4. Eg the 31st of a month would be 71, or the 3rd would be 43.
147+
// thus we need to subtract 40 to get the correct day of the month
148+
if (day >= 40) {
149+
day = day - 40;
153150
}
154151

155152
const date = new Date(Date.UTC(year, month - 1, day));

packages/validation/src/validation.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ describe('no', () => {
6060
expect(no.validateObosMembershipNumber(input, options)).toBe(expected);
6161
});
6262

63-
test('validatePersonalIdentityNumber()', () => {
63+
test('validateNationalIdentityNumber()', () => {
6464
for (let i = 0; i < 1000; ++i) {
6565
expect(
66-
no.validatePersonalIdentityNumber(
66+
no.validateNationalIdentityNumber(
6767
navfaker.personIdentifikator.fødselsnummer(),
6868
),
6969
).toBe(true);
7070
expect(
71-
no.validatePersonalIdentityNumber(
71+
no.validateNationalIdentityNumber(
7272
navfaker.personIdentifikator.dnummer(),
7373
),
7474
).toBe(true);

0 commit comments

Comments
 (0)