From 1584277349d8d02af773ec274f0bc7616284e9a1 Mon Sep 17 00:00:00 2001 From: Giacomo Fava Date: Tue, 14 Apr 2020 19:04:28 +0200 Subject: [PATCH] compute inverse even if there is an omocodia --- .gitignore | 3 +++ package-lock.json | 43 +++++++++++++++++++++++++++++++------------ src/codice-fiscale.js | 20 ++++++++++++++++---- src/constants.js | 13 +++++++++++++ tests/index.spec.js | 15 +++++++++++++++ 5 files changed, 78 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 257b2aa..b8d3445 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,9 @@ node_modules # Optional npm cache directory .npm +# intellij files +.idea + # Optional REPL history .node_repl_history .DS_Store diff --git a/package-lock.json b/package-lock.json index d2c5162..9baba59 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "codice-fiscale-js", - "version": "2.2.3", + "version": "2.2.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3718,7 +3718,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -3739,12 +3740,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3759,17 +3762,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -3886,7 +3892,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -3898,6 +3905,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3912,6 +3920,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3919,12 +3928,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -3943,6 +3954,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -4023,7 +4035,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -4035,6 +4048,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -4120,7 +4134,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -4156,6 +4171,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -4175,6 +4191,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4218,12 +4235,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/src/codice-fiscale.js b/src/codice-fiscale.js index 88cebb6..76ba3fa 100755 --- a/src/codice-fiscale.js +++ b/src/codice-fiscale.js @@ -1,5 +1,5 @@ import { Comune } from './comune' -import { CHECK_CODE_CHARS, CHECK_CODE_EVEN, CHECK_CODE_ODD, MONTH_CODES, OMOCODIA_TABLE } from './constants' +import { CHECK_CODE_CHARS, CHECK_CODE_EVEN, CHECK_CODE_ODD, MONTH_CODES, OMOCODIA_TABLE, OMOCODIA_TABLE_INVERSE } from './constants' import { extractConsonants, extractVowels, getValidDate, birthplaceFields } from './utils' class CodiceFiscale { @@ -112,6 +112,13 @@ class CodiceFiscale { day = day.substr(day.length - 2, 2) return `${year}${month}${day}` } + static toNumberIfOmocodia(input){ + if (isNaN(input)) { + input = [...input].map((c) => isNaN(c) ? OMOCODIA_TABLE_INVERSE[c] : c); + input = input.join(''); + } + return input + } toString () { return this.code } @@ -166,7 +173,8 @@ class CodiceFiscale { this.name = this.code.substr(3, 3) this.surname = this.code.substr(0, 3) - const yearCode = this.code.substr(6, 2) + let yearCode = this.code.substr(6, 2) + yearCode = CodiceFiscale.toNumberIfOmocodia(yearCode); const year19XX = parseInt(`19${yearCode}`, 10) const year20XX = parseInt(`20${yearCode}`, 10) const currentYear20XX = new Date().getFullYear() @@ -174,13 +182,17 @@ class CodiceFiscale { const monthChar = this.code.substr(8, 1) const month = MONTH_CODES.indexOf(monthChar) this.gender = 'M' - let day = parseInt(this.code.substr(9, 2), 10) + let dayString = this.code.substr(9, 2); + dayString = CodiceFiscale.toNumberIfOmocodia(dayString); + let day = parseInt(dayString, 10) if (day > 31) { this.gender = 'F' day = day - 40 } this.birthday = new Date(Date.UTC(year, month, day, 0, 0, 0, 0)) - const cc = this.code.substr(11, 4) + let cc = this.code.substr(11, 4) + const ccNumbers = CodiceFiscale.toNumberIfOmocodia(cc.substr(1, 3)); + cc = cc.charAt(0) + ccNumbers; this.birthplace = Comune.GetByCC(cc) return this.toJSON() } diff --git a/src/constants.js b/src/constants.js index 9a74ac7..0abefcf 100755 --- a/src/constants.js +++ b/src/constants.js @@ -104,4 +104,17 @@ export const OMOCODIA_TABLE = { 9: 'V' } +export const OMOCODIA_TABLE_INVERSE = { + 'L': '0', + 'M': '1', + 'N': '2', + 'P': '3', + 'Q': '4', + 'R': '5', + 'S': '6', + 'T': '7', + 'U': '8', + 'V': '9' +} + export const CHECK_CODE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' diff --git a/tests/index.spec.js b/tests/index.spec.js index 33556b1..58bd473 100755 --- a/tests/index.spec.js +++ b/tests/index.spec.js @@ -241,6 +241,21 @@ describe('Calcolo codice fiscale inverso -> metodo .computeInverse', () => { }) + /* Nome: GIUSEPPE + * Cognome: ESPOSITO + * Nato a : NAPOLI (NP) + * Giorno : 18 + * Mese : Febbraio (2) + * Anno : 1975 + * Sesso : M + * OMOCODIA: true + */ + let giuseppe_esposito_cf = "SPSGPP75B18F83VM"; + + test('restituisce la città natale corretta nonostante omocodia', () => { + expect(CodiceFiscale.computeInverse(giuseppe_esposito_cf).birthplace).toEqual('NAPOLI') + }) + })