Skip to content

Commit

Permalink
code fiscal
Browse files Browse the repository at this point in the history
  • Loading branch information
raupach-e2n committed Nov 17, 2023
1 parent af580d9 commit 9759426
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 81 deletions.
97 changes: 23 additions & 74 deletions validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,20 @@
* @module validators
*/

function string_type(input) {
return typeof input == "string";
function luhn(input) {
let sum = 0;
let odd = false;
/* start at the rightmost digit */
for (let i = input.length - 1; i >= 0; i--) {
let n = input.charCodeAt(i) - 48;
/* double every second digit */
n = odd ? n << 1 : n;
odd = !odd;
/* add the digit sum */
sum += n / 10 | 0;
sum += n % 10;
}
return sum % 10 === 0;
}

/* IBAN REGISTRY Release 95 – Jul 2023 */
Expand Down Expand Up @@ -69,7 +81,7 @@ function digit_sum(value) {
let digit_sum = 0;
while (value) {
digit_sum += value % 10;
value = Math.floor(value / 10);
value = value / 10 | 0;
}
return digit_sum;
}
Expand All @@ -84,7 +96,7 @@ function digit_sum(value) {
*/
function iban(input) {
/* Type check */
if (!string_type(input)) {
if (typeof input !== "string") {
return false;
}

Expand Down Expand Up @@ -347,54 +359,14 @@ function nir(input) {
* Payment card number
*/
function pan(input) {
/* Type check */
if (!input && typeof input !== "string") {
return false;
}

let sum = 0;
let even = false;

for (let i = input.length - 1; i >= 0; i--) {
let n = input[i].charCodeAt(0) - 48;

n = (even) ? (n * 2) : n;

sum += Math.floor(n / 10);
sum += n % 10;

even = !even;
}

return sum % 10 === 0;
return typeof input === "string" && luhn(input);
}

/**
* International Mobile Equipment Identity (IMEI)
*/
function imei(input) {
/* Type check */
if (!input && typeof input !== "string") {
return false;
}

/* Length check */
if (input.length !== 15) {
return false;
}

let sum = 0;
let even = false;

for (let i = 0; i < input.length - 1; i++) {
let n = input[i].charCodeAt(0) - 48;
n = (even) ? (n * 2) : n;
sum += Math.floor(n / 10);
sum += n % 10;
even = !even;
}

return sum % 10 === 0;
return typeof input === "string" && /\d{15}/.test(input) && luhn(input);
}

/**
Expand All @@ -416,10 +388,10 @@ function cf(input) {

for (let i = 0; i < input.length - 1; i++) {
let char = input.charAt(i);
sum += even ? cf_even.get(char) : sum + cf_odd.get(char);
sum += even ? cf_even.get(char) : cf_odd.get(char);
even = !even;
}

let reminder = sum % 26;

return input.charAt(15) === cf_reminder[reminder];
Expand Down Expand Up @@ -469,7 +441,7 @@ const dni_reminder = "TRWAGMYFPDXBNJZSQVHLCKE".split('');
*/
function nhs(input) {
/* Type check */
if (!input && typeof input !== "string") {
if (typeof input !== "string") {
return false;
}

Expand All @@ -491,6 +463,7 @@ function nhs(input) {
if (reminder === 10) {
return false;
}

if (reminder === 11) {
reminder = 0;
}
Expand All @@ -502,31 +475,7 @@ function nhs(input) {
* Canadian Social Insurance Number (SIN)
*/
function sin(input) {
/* Type check */
if (!input && typeof input !== "string") {
return false;
}

/* Length check */
if (input.length !== 9) {
return false;
}

let sum = 0;
let even = false;

for (let i = 0; i < input.length; i++) {
let n = input[i].charCodeAt(0) - 48;

n = (even) ? (n * 2) : n;

sum += Math.floor(n / 10);
sum += n % 10;

even = !even;
}

return sum % 10 === 0;
return typeof input === "string" && /\d{9}/.test(input) && luhn(input);
}

function pid(input) {
Expand Down
11 changes: 4 additions & 7 deletions validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const validators = require("./validators");

describe("International Bank Account Number (IBAN)", function() {
test.each([
[undefined, false],
["AD1200012030200359100100", true],
["AD12 0001 2030 2003 5910 0100", true],
["AE070331234567890123456", true],
Expand Down Expand Up @@ -264,6 +265,9 @@ describe("Betriebsnummer (BBNR)", function() {

describe("International Mobile Equipment Identity (IMEI)", function() {
test.each([
[undefined, false],
[{}, false],
[0, false],
["490154203237510", false],
["490154203237511", false],
["490154203237512", false],
Expand Down Expand Up @@ -302,11 +306,4 @@ describe("Canadian Social Insurance Number (SIN)", function() {
test.each([
["046454286", true]
])("%s", (input, expected) => expect(validators.sin(input)).toBe(expected));
});

describe("Personalausweis-ID oder Pass-ID (PID)", function() {
test.each([
["T220001293", true],
["1220001297", true]
])("%s", (input, expected) => expect(validators.pid(input)).toBe(expected));
});

0 comments on commit 9759426

Please # to comment.