Skip to content

Commit

Permalink
feat: change IdNumberType to be enum (from 'type')
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaello committed Jun 12, 2020
1 parent 79f658c commit d783dd0
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 37 deletions.
36 changes: 19 additions & 17 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
### Enumerations

* [Gender](enums/gender.md)

### Type aliases

* [IDNumberType](README.md#idnumbertype)
* [IDNumberType](enums/idnumbertype.md)

### Functions

Expand All @@ -19,7 +16,7 @@
* [getBirthDate](README.md#private-getbirthdate)
* [getGender](README.md#getgender)
* [idNumberContainsBirthDate](README.md#idnumbercontainsbirthdate)
* [idNumberType](README.md#private-idnumbertype)
* [idNumberType](README.md#idnumbertype)
* [isValidCheckDigit](README.md#private-isvalidcheckdigit)
* [isValidCheckDigits](README.md#private-isvalidcheckdigits)
* [isValidDate](README.md#isvaliddate)
Expand All @@ -31,12 +28,6 @@
* [possibleBirthDateOfIdNumber](README.md#possiblebirthdateofidnumber)
* [validateNorwegianIdNumber](README.md#validatenorwegianidnumber)

## Type aliases

### IDNumberType

Ƭ **IDNumberType**: *"birthNumber" | "DNumber" | "HNumber" | "FHNumber"*

## Functions

### `Const` NorwegianId
Expand Down Expand Up @@ -143,17 +134,28 @@ Name | Type | Description |

___

### `Private` idNumberType
### idNumberType

**idNumberType**(`elevenDigits`: string): *[IDNumberType](enums/idnumbertype.md)*

**idNumberType**(`elevenDigits`: string): *[IDNumberType](README.md#idnumbertype)*
Get the ID number kind/type. This function does not validate, so
it should be combined with [`validateNorwegianIdNumber`](README.md#validatenorwegianidnumber).

**`example`**
```javascript
import { idNumberType, validateNorwegianIdNumber } from 'norwegian-national-id-validator';
if (validateNorwegianIdNumber(0000000000)) {
const type = idNumberType(00000000000);
}
```

**Parameters:**

Name | Type |
------ | ------ |
`elevenDigits` | string |
Name | Type | Description |
------ | ------ | ------ |
`elevenDigits` | string | IdNumber |

**Returns:** *[IDNumberType](README.md#idnumbertype)*
**Returns:** *[IDNumberType](enums/idnumbertype.md)*

___

Expand Down
6 changes: 6 additions & 0 deletions docs/enums/gender.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Enumeration: Gender

Birth numbers, D-number and H-number contains information about gender

## Index

### Enumeration members
Expand All @@ -15,8 +17,12 @@

**Female**:

If the third last digit in the ID number is even, it is a female

___

### Male

**Male**:

If the third last digit in the ID number is odd, it is a male
54 changes: 54 additions & 0 deletions docs/enums/idnumbertype.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[norwegian-national-id-validator](../README.md)[IDNumberType](idnumbertype.md)

# Enumeration: IDNumberType

In Norway there are several different ID numbers

## Index

### Enumeration members

* [BirthNumber](idnumbertype.md#birthnumber)
* [DNumber](idnumbertype.md#dnumber)
* [FHNumber](idnumbertype.md#fhnumber)
* [HNumber](idnumbertype.md#hnumber)

## Enumeration members

### BirthNumber

**BirthNumber**:

A national identity number (birth number) is an ID number for you who
have a residence permit and are going to live in Norway for more than
six months.

___

### DNumber

**DNumber**:

A D number is a temporary identification number that you get if you have
applied for protection (asylum), or if you have a residence permit and
are going to stay in Norway for less than six months.

___

### FHNumber

**FHNumber**:

A FH number is used in health care to uniquely identify patients that
does not have a known national ID or D number. A FH number does not have
any information about age or gender.

___

### HNumber

**HNumber**:

A H number is a number used for assistance, a unique identification of a
person that does not have a national ID or a D number or in cases where
this is not known. A H number contains information about age and gender.
82 changes: 62 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @example
* ```javascript
* import { NorwegianId } from 'norwegian-national-id-validator';
*
*
* const valid = NorwegianId('0000000000');
* ```
* @param idNumber norwegian social security number
Expand All @@ -21,27 +21,29 @@ export const NorwegianId = (idNumber: string) => {
* have a residence permit and are going to live in Norway for more than
* six months.
*/
isBirthNumber: () => valid && idNumberType(idNumber) == 'birthNumber',
isBirthNumber: () =>
valid && idNumberType(idNumber) == IDNumberType.BirthNumber,

/**
* A D number is a temporary identification number that you get if you have
* applied for protection (asylum), or if you have a residence permit and
* are going to stay in Norway for less than six months. */
isDNumber: () => valid && idNumberType(idNumber) == 'DNumber',
* are going to stay in Norway for less than six months.
*/
isDNumber: () => valid && idNumberType(idNumber) == IDNumberType.DNumber,

/**
* A H number is a number used for assistance, a unique identification of a
* person that does not have a national ID or a D number or in cases where
* this is not known. A H number contains information about age and gender.
*/
isHNumber: () => valid && idNumberType(idNumber) == 'HNumber',
isHNumber: () => valid && idNumberType(idNumber) == IDNumberType.HNumber,

/**
* A FH number is used in health care to uniquely identify patients that
* does not have a known national ID or D number. A FH number does not have
* any information about age or gender.
*/
isFhNumber: () => valid && idNumberType(idNumber) == 'FHNumber',
isFhNumber: () => valid && idNumberType(idNumber) == IDNumberType.FHNumber,
isMale: () => valid && getGender(idNumber) == Gender.Male,
isFemale: () => valid && getGender(idNumber) == Gender.Female,
age: () => possibleAgeOfPersonWithIdNumber(idNumber),
Expand Down Expand Up @@ -75,8 +77,8 @@ export function diffYears(startDate: Date, endDate: Date) {
* Checks if a date is valid against another
* @param date Date instance
* @param expectedYear
* @param expectedMonth
* @param expectedDay
* @param expectedMonth
* @param expectedDay
*/
export function isValidDate(
date: Date,
Expand All @@ -91,10 +93,41 @@ export function isValidDate(
);
}

type IDNumberType = 'birthNumber' | 'DNumber' | 'HNumber' | 'FHNumber';
/** In Norway there are several different ID numbers */
export enum IDNumberType {
/**
* A national identity number (birth number) is an ID number for you who
* have a residence permit and are going to live in Norway for more than
* six months.
*/
BirthNumber,
/**
* A D number is a temporary identification number that you get if you have
* applied for protection (asylum), or if you have a residence permit and
* are going to stay in Norway for less than six months.
*/
DNumber,
/**
* A H number is a number used for assistance, a unique identification of a
* person that does not have a national ID or a D number or in cases where
* this is not known. A H number contains information about age and gender.
*/
HNumber,
/**
* A FH number is used in health care to uniquely identify patients that
* does not have a known national ID or D number. A FH number does not have
* any information about age or gender.
*/
FHNumber,
}

/**
* Birth numbers, D-number and H-number contains information about gender
*/
export enum Gender {
/** If the third last digit in the ID number is odd, it is a male */
Male,
/** If the third last digit in the ID number is even, it is a female */
Female,
}

Expand All @@ -114,7 +147,7 @@ export function validateNorwegianIdNumber(idNumber: string): boolean {
if (trimmed.length !== 11) return false;
if (!isValidCheckDigits(trimmed)) return false;
const type = idNumberType(trimmed);
if (type === 'FHNumber') return true;
if (type === IDNumberType.FHNumber) return true;
else return possibleAgesOfPersonWithIdNumber(trimmed).length > 0;
}

Expand Down Expand Up @@ -150,7 +183,7 @@ export function possibleAgeOfPersonWithIdNumber(
* @param elevenDigits idNumber
*/
export function idNumberContainsBirthDate(elevenDigits: string): boolean {
return idNumberType(elevenDigits) !== 'FHNumber';
return idNumberType(elevenDigits) !== IDNumberType.FHNumber;
}

/**
Expand All @@ -161,26 +194,35 @@ function possibleBirthDateOfIdNumber(elevenDigits: string): Date | undefined {
if (elevenDigits.length !== 11) return undefined;
const type = idNumberType(elevenDigits);
switch (type) {
case 'birthNumber':
case IDNumberType.BirthNumber:
return possibleBirthDateOfBirthNumber(elevenDigits);
case 'DNumber':
case IDNumberType.DNumber:
return possibleBirthDateOfDNumber(elevenDigits);
case 'HNumber':
case IDNumberType.HNumber:
return possibleBirthDateOfHNumber(elevenDigits);
}
return undefined;
}

/**
* @private
* Get the ID number kind/type. This function does not validate, so
* it should be combined with {@linkcode validateNorwegianIdNumber}.
* @example
* ```javascript
* import { idNumberType, validateNorwegianIdNumber } from 'norwegian-national-id-validator';
* if (validateNorwegianIdNumber(0000000000)) {
* const type = idNumberType(00000000000);
* }
* ```
* @param elevenDigits IdNumber
*/
function idNumberType(elevenDigits: string): IDNumberType {
const firstDigit = parseInt(elevenDigits[0]);
if (firstDigit === 8 || firstDigit === 9) return 'FHNumber';
if (firstDigit >= 4 && firstDigit <= 7) return 'DNumber';
if (firstDigit === 8 || firstDigit === 9) return IDNumberType.FHNumber;
if (firstDigit >= 4 && firstDigit <= 7) return IDNumberType.DNumber;
const thirdDigit = parseInt(elevenDigits[2]);
if (thirdDigit === 4 || thirdDigit === 5) return 'HNumber';
else return 'birthNumber';
if (thirdDigit === 4 || thirdDigit === 5) return IDNumberType.HNumber;
else return IDNumberType.BirthNumber;
}

/**
Expand Down Expand Up @@ -280,7 +322,7 @@ function isValidCheckDigit(
export function getGender(elevenDigits: string): Gender | undefined {
if (elevenDigits.length != 11) {
return undefined;
} else if (idNumberType(elevenDigits) == 'FHNumber') {
} else if (idNumberType(elevenDigits) == IDNumberType.FHNumber) {
return undefined;
}

Expand Down

0 comments on commit d783dd0

Please sign in to comment.