Skip to content

Commit

Permalink
Converted to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
storbukas committed Jan 26, 2022
1 parent 6601fff commit 1f1acd8
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 52 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ typings/
.next

# Build
/lib/
/dist/

# NPM
package-lock.json
package-lock.json

#IDE
.idea
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "norsk-validator",
"version": "1.1.1",
"version": "1.2.0",
"description": "Validering av kontonummer, KID, fødselsnummer og organisasjonsnummer",
"author": "Lars Erik Storbukås",
"homepage": "https://github.com/storbukas/norsk-validator#readme",
"license": "MIT",
"main": "lib/index.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/storbukas/norsk-validator.git"
Expand All @@ -14,7 +15,7 @@
"test": "tap --reporter=spec test/*.test.js",
"lint": "eslint src",
"lint:fix": "eslint --fix src",
"build": "babel src --out-dir lib --extensions \".js,.jsx\" --source-maps inline",
"build": "tsc",
"coverage": "tap test/*.js --coverage",
"refresh": "rm -rf node_modules && rm package-lock.json && npm install",
"size-build": "npm run build",
Expand Down Expand Up @@ -46,7 +47,8 @@
"eslint-config-airbnb-base": "^13.2.0",
"eslint-plugin-import": "^2.18.2",
"size-limit": "^4.4.5",
"tap": "^14.10.7"
"tap": "^14.10.7",
"typescript": "^2.7.2"
},
"size-limit": [
{
Expand Down
6 changes: 3 additions & 3 deletions src/fodselsnummer.js → src/fodselsnummer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Kode for validering av fødselsnummer
*
* updated 08 Feb 2021
* updated 26 Jan 2022
* by Lars Erik Storbukås <https://github.com/storbukas>
*
* Source: https://github.com/storbukas/norsk-validator
Expand All @@ -18,7 +18,7 @@ const vekttallKontrollsiffer1 = [3, 7, 6, 1, 8, 9, 4, 5, 2];
const vekttallKontrollsiffer2 = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
const PERIOD_COMMA_SPACE_REGEX = /[,.\s]/g;

const vekttallSum = (fodselsnummer, factors) => {
const vekttallSum = (fodselsnummer: any, factors: any) => {
let sum = 0;
const l = factors.length;

Expand All @@ -29,7 +29,7 @@ const vekttallSum = (fodselsnummer, factors) => {
return sum;
};

const fodselsnummer = (fodselsnummer) => {
const fodselsnummer = (fodselsnummer: any) => {
const fodselsnummerString = fodselsnummer.toString()
.replace(PERIOD_COMMA_SPACE_REGEX, '');

Expand Down
26 changes: 0 additions & 26 deletions src/index.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Validering av fødselsnummer, organisasjonsnummer, kontonummer og KID
*
* updated 26 Jan 2022
* by Lars Erik Storbukås <https://github.com/storbukas>
*
* Source: https://github.com/storbukas/norsk-validator
*
* Licensed under the MIT License
* https://opensource.org/licenses/MIT
*
*/

'use strict';

import fodselsnummer from "./fodselsnummer";
import kidnummer from "./kidnummer";
import kontonummer from "./kontonummer";
import organisasjonsnummer from "./organisasjonsnummer";

export default {
kontonummer,
kidnummer,
fodselsnummer,
organisasjonsnummer,
};
10 changes: 5 additions & 5 deletions src/kidnummer.js → src/kidnummer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Kode for validering av KID-nummer
*
* updated 08 Feb 2021
* updated 26 Jan 2022
* by Lars Erik Storbukås <https://github.com/storbukas>
*
* Source: https://github.com/storbukas/norsk-validator
Expand All @@ -13,11 +13,11 @@

'use strict';

const kontrollsiffer = require('./tools/kontrollsiffer');
import { kontrollsiffferMod10, kontrollsiffferMod11 } from "./tools/kontrollsiffer";

const SPACE_REGEX = /[\s]/g;

const kidnummer = (kidnummer, mod11 = true, mod10 = true) => {
const kidnummer = (kidnummer: any, mod11 = true, mod10 = true) => {
if (!kidnummer || kidnummer.length === 0) {
return false;
}
Expand All @@ -26,9 +26,9 @@ const kidnummer = (kidnummer, mod11 = true, mod10 = true) => {

const controlDigit = kidnummerString.charAt(kidnummer.length - 1);
const erMod11 = parseInt(controlDigit, 10)
=== kontrollsiffer.kontrollsiffferMod11(kidnummerString);
=== kontrollsiffferMod11(kidnummerString);
const erMod10 = parseInt(controlDigit, 10)
=== kontrollsiffer.kontrollsiffferMod10(kidnummerString);
=== kontrollsiffferMod10(kidnummerString);

return (mod11 && erMod11) || (mod10 && erMod10);
};
Expand Down
8 changes: 4 additions & 4 deletions src/kontonummer.js → src/kontonummer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Kode for validering av kontonummer
*
* updated 08 Feb 2021
* updated 26 Jan 2022
* by Lars Erik Storbukås <https://github.com/storbukas>
*
* Source: https://github.com/storbukas/norsk-validator
Expand All @@ -13,11 +13,11 @@

'use strict';

const kontrollsiffer = require('./tools/kontrollsiffer');
import { kontrollsiffferMod11 } from "./tools/kontrollsiffer";

const PERIOD_COMMA_SPACE_REGEX = /[,.\s]/g;

const kontonummer = (kontonummer) => {
const kontonummer = (kontonummer: any) => {
if (!kontonummer) {
return false;
}
Expand All @@ -30,7 +30,7 @@ const kontonummer = (kontonummer) => {

return (
parseInt(kontonummerString.charAt(kontonummerString.length - 1), 10)
=== kontrollsiffer.kontrollsiffferMod11(kontonummerString)
=== kontrollsiffferMod11(kontonummerString)
);
};

Expand Down
8 changes: 4 additions & 4 deletions src/organisasjonsnummer.js → src/organisasjonsnummer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Kode for validering av organisasjonsnummer
*
* updated 08 Feb 2021
* updated 26 Jan 2022
* by Lars Erik Storbukås <https://github.com/storbukas>
*
* Source: https://github.com/storbukas/norsk-validator
Expand All @@ -13,17 +13,17 @@

'use strict';

const kontrollsiffer = require('./tools/kontrollsiffer');
import { kontrollsiffferMod11 } from "./tools/kontrollsiffer";

const organisasjonsnummer = (organisasjonsnummer) => {
const organisasjonsnummer = (organisasjonsnummer: any) => {
const organisasjonsnummerString = organisasjonsnummer.toString();

if (!organisasjonsnummerString || organisasjonsnummerString.length !== 9) {
return false;
}

return parseInt(organisasjonsnummerString.charAt(organisasjonsnummerString.length - 1), 10)
=== kontrollsiffer.kontrollsiffferMod11(organisasjonsnummerString);
=== kontrollsiffferMod11(organisasjonsnummerString);
};

export default organisasjonsnummer;
8 changes: 4 additions & 4 deletions src/tools/kontrollsiffer.js → src/tools/kontrollsiffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Test av fødselsnummer
*
* updated 08 Feb 2021
* updated 26 Jan 2022
* by Lars Erik Storbukås <https://github.com/storbukas>
*
* Source: https://github.com/storbukas/norsk-validator
Expand All @@ -16,7 +16,7 @@
const MOD11 = 11;
const MOD10 = 10;

export const kontrollsiffferMod11 = (tall) => {
export const kontrollsiffferMod11 = (tall: any) => {
let kontrollsiffer = 2;
let sum = 0;

Expand All @@ -35,13 +35,13 @@ export const kontrollsiffferMod11 = (tall) => {
return result === MOD11 ? 0 : result;
};

export const kontrollsiffferMod10 = (tall) => {
export const kontrollsiffferMod10 = (tall: any) => {
let sum = 0;
let dbl;

for (let i = tall.length - 2; i >= 0; i -= 2) {
dbl = (parseInt(tall.charAt(i), 10) * 2).toString();
sum += parseInt(dbl.charAt(0), 10) + parseInt(dbl.charAt(1) || 0, 10);
sum += parseInt(dbl.charAt(0), 10) + parseInt(dbl.charAt(1) || '0', 10);
}

for (let i = tall.length - 3; i >= 0; i -= 2) {
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": ["es2017", "es7", "es6", "dom"],
"declaration": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true
},
"exclude": [
"node_modules",
"dist"
]
}

0 comments on commit 1f1acd8

Please sign in to comment.