mirror of
https://github.com/danog/CodiceFiscaleJS.git
synced 2024-11-26 20:14:55 +01:00
compute inverse even if there is an omocodia
This commit is contained in:
parent
6614d3a9ba
commit
1584277349
3
.gitignore
vendored
3
.gitignore
vendored
@ -29,6 +29,9 @@ node_modules
|
|||||||
# Optional npm cache directory
|
# Optional npm cache directory
|
||||||
.npm
|
.npm
|
||||||
|
|
||||||
|
# intellij files
|
||||||
|
.idea
|
||||||
|
|
||||||
# Optional REPL history
|
# Optional REPL history
|
||||||
.node_repl_history
|
.node_repl_history
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
43
package-lock.json
generated
43
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "codice-fiscale-js",
|
"name": "codice-fiscale-js",
|
||||||
"version": "2.2.3",
|
"version": "2.2.4",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -3718,7 +3718,8 @@
|
|||||||
"ansi-regex": {
|
"ansi-regex": {
|
||||||
"version": "2.1.1",
|
"version": "2.1.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"aproba": {
|
"aproba": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
@ -3739,12 +3740,14 @@
|
|||||||
"balanced-match": {
|
"balanced-match": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"brace-expansion": {
|
"brace-expansion": {
|
||||||
"version": "1.1.11",
|
"version": "1.1.11",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"balanced-match": "^1.0.0",
|
"balanced-match": "^1.0.0",
|
||||||
"concat-map": "0.0.1"
|
"concat-map": "0.0.1"
|
||||||
@ -3759,17 +3762,20 @@
|
|||||||
"code-point-at": {
|
"code-point-at": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"concat-map": {
|
"concat-map": {
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"console-control-strings": {
|
"console-control-strings": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"core-util-is": {
|
"core-util-is": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
@ -3886,7 +3892,8 @@
|
|||||||
"inherits": {
|
"inherits": {
|
||||||
"version": "2.0.3",
|
"version": "2.0.3",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"ini": {
|
"ini": {
|
||||||
"version": "1.3.5",
|
"version": "1.3.5",
|
||||||
@ -3898,6 +3905,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"number-is-nan": "^1.0.0"
|
"number-is-nan": "^1.0.0"
|
||||||
}
|
}
|
||||||
@ -3912,6 +3920,7 @@
|
|||||||
"version": "3.0.4",
|
"version": "3.0.4",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"brace-expansion": "^1.1.7"
|
"brace-expansion": "^1.1.7"
|
||||||
}
|
}
|
||||||
@ -3919,12 +3928,14 @@
|
|||||||
"minimist": {
|
"minimist": {
|
||||||
"version": "0.0.8",
|
"version": "0.0.8",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"minipass": {
|
"minipass": {
|
||||||
"version": "2.3.5",
|
"version": "2.3.5",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"safe-buffer": "^5.1.2",
|
"safe-buffer": "^5.1.2",
|
||||||
"yallist": "^3.0.0"
|
"yallist": "^3.0.0"
|
||||||
@ -3943,6 +3954,7 @@
|
|||||||
"version": "0.5.1",
|
"version": "0.5.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"minimist": "0.0.8"
|
"minimist": "0.0.8"
|
||||||
}
|
}
|
||||||
@ -4023,7 +4035,8 @@
|
|||||||
"number-is-nan": {
|
"number-is-nan": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"object-assign": {
|
"object-assign": {
|
||||||
"version": "4.1.1",
|
"version": "4.1.1",
|
||||||
@ -4035,6 +4048,7 @@
|
|||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"wrappy": "1"
|
"wrappy": "1"
|
||||||
}
|
}
|
||||||
@ -4120,7 +4134,8 @@
|
|||||||
"safe-buffer": {
|
"safe-buffer": {
|
||||||
"version": "5.1.2",
|
"version": "5.1.2",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"safer-buffer": {
|
"safer-buffer": {
|
||||||
"version": "2.1.2",
|
"version": "2.1.2",
|
||||||
@ -4156,6 +4171,7 @@
|
|||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"code-point-at": "^1.0.0",
|
"code-point-at": "^1.0.0",
|
||||||
"is-fullwidth-code-point": "^1.0.0",
|
"is-fullwidth-code-point": "^1.0.0",
|
||||||
@ -4175,6 +4191,7 @@
|
|||||||
"version": "3.0.1",
|
"version": "3.0.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"ansi-regex": "^2.0.0"
|
"ansi-regex": "^2.0.0"
|
||||||
}
|
}
|
||||||
@ -4218,12 +4235,14 @@
|
|||||||
"wrappy": {
|
"wrappy": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"yallist": {
|
"yallist": {
|
||||||
"version": "3.0.3",
|
"version": "3.0.3",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Comune } from './comune'
|
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'
|
import { extractConsonants, extractVowels, getValidDate, birthplaceFields } from './utils'
|
||||||
|
|
||||||
class CodiceFiscale {
|
class CodiceFiscale {
|
||||||
@ -112,6 +112,13 @@ class CodiceFiscale {
|
|||||||
day = day.substr(day.length - 2, 2)
|
day = day.substr(day.length - 2, 2)
|
||||||
return `${year}${month}${day}`
|
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 () {
|
toString () {
|
||||||
return this.code
|
return this.code
|
||||||
}
|
}
|
||||||
@ -166,7 +173,8 @@ class CodiceFiscale {
|
|||||||
this.name = this.code.substr(3, 3)
|
this.name = this.code.substr(3, 3)
|
||||||
this.surname = this.code.substr(0, 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 year19XX = parseInt(`19${yearCode}`, 10)
|
||||||
const year20XX = parseInt(`20${yearCode}`, 10)
|
const year20XX = parseInt(`20${yearCode}`, 10)
|
||||||
const currentYear20XX = new Date().getFullYear()
|
const currentYear20XX = new Date().getFullYear()
|
||||||
@ -174,13 +182,17 @@ class CodiceFiscale {
|
|||||||
const monthChar = this.code.substr(8, 1)
|
const monthChar = this.code.substr(8, 1)
|
||||||
const month = MONTH_CODES.indexOf(monthChar)
|
const month = MONTH_CODES.indexOf(monthChar)
|
||||||
this.gender = 'M'
|
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) {
|
if (day > 31) {
|
||||||
this.gender = 'F'
|
this.gender = 'F'
|
||||||
day = day - 40
|
day = day - 40
|
||||||
}
|
}
|
||||||
this.birthday = new Date(Date.UTC(year, month, day, 0, 0, 0, 0))
|
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)
|
this.birthplace = Comune.GetByCC(cc)
|
||||||
return this.toJSON()
|
return this.toJSON()
|
||||||
}
|
}
|
||||||
|
@ -104,4 +104,17 @@ export const OMOCODIA_TABLE = {
|
|||||||
9: 'V'
|
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'
|
export const CHECK_CODE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||||
|
@ -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')
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user