added constants to CodiceFiscale as static consts

This commit is contained in:
Marco Pesani 2017-12-19 13:26:02 +01:00
parent 2b561b2624
commit 278fff8d95
7 changed files with 31 additions and 22 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

14
package-lock.json generated
View File

@ -5570,7 +5570,8 @@
"lodash": {
"version": "4.17.4",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz",
"integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4="
"integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=",
"dev": true
},
"lodash.assign": {
"version": "4.2.0",
@ -5584,6 +5585,11 @@
"integrity": "sha1-9HGh2khr5g9quVXRcRVSPdHSVdU=",
"dev": true
},
"lodash.deburr": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/lodash.deburr/-/lodash.deburr-4.1.0.tgz",
"integrity": "sha1-3bG7s+8HRYwBd7oH3hRCLLAz/5s="
},
"lodash.endswith": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/lodash.endswith/-/lodash.endswith-4.2.1.tgz",
@ -6860,12 +6866,6 @@
"inherits": "2.0.3"
}
},
"rollup": {
"version": "0.52.3",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-0.52.3.tgz",
"integrity": "sha512-cw+vb9NqaTXlwJyb8G+Ve+uhhlVTcl1NKBkfANdeQqVcpZFilQgeNnAnNiu7MwfeXrqiKEGz+3R03a3zeFkmEQ==",
"dev": true
},
"run-async": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/run-async/-/run-async-0.1.0.tgz",

View File

@ -11,7 +11,7 @@
"test": "tests"
},
"dependencies": {
"lodash": "^4.17.4"
"lodash.deburr": "^4.1.0"
},
"devDependencies": {
"babel-core": "^6.26.0",

View File

@ -7,9 +7,9 @@ import {
CODICI_CATASTALI
} from './constants'
import { deburr } from 'lodash'
import deburr from 'lodash.deburr'
module.exports = class CodiceFiscale {
class CodiceFiscale {
static compute (codiceFiscaleObject) {
let code = this.surnameCode(codiceFiscaleObject.surname)
code += this.nameCode(codiceFiscaleObject.name)
@ -38,10 +38,10 @@ module.exports = class CodiceFiscale {
let val = 0
for (let i = 0; i < 15; i++) {
const c = codiceFiscale[i]
val += i % 2 ? CHECK_CODE_EVEN[c] : CHECK_CODE_ODD[c]
val += i % 2 ? this.CHECK_CODE_EVEN[c] : this.CHECK_CODE_ODD[c]
}
val = val % 26
return CHECK_CODE_CHARS.charAt(val)
return this.CHECK_CODE_CHARS.charAt(val)
}
static estraiVocali (str) {
@ -73,7 +73,7 @@ module.exports = class CodiceFiscale {
let year = '0' + date.getFullYear()
year = year.substr(year.length - 2, 2)
let month = MONTH_CODES[date.getMonth()]
let month = this.MONTH_CODES[date.getMonth()]
let day = date.getDate()
if (gender.toUpperCase() === 'F') day += 40
@ -84,10 +84,10 @@ module.exports = class CodiceFiscale {
}
static findComuneCode (birthplace, birthplaceProvincia) {
if (!CODICI_CATASTALI[birthplaceProvincia]) {
if (!this.CODICI_CATASTALI[birthplaceProvincia]) {
throw new Error('Provincia not found')
}
const comune = CODICI_CATASTALI[birthplaceProvincia]
const comune = this.CODICI_CATASTALI[birthplaceProvincia]
.find(comune => {
return this.normalizeString(comune[0]) === this.normalizeString(birthplace)
})
@ -112,7 +112,7 @@ module.exports = class CodiceFiscale {
if (char.match(/\d/)) {
lastOmocode =
lastOmocode.substr(0, i) +
OMOCODIA_TABLE[char] +
this.OMOCODIA_TABLE[char] +
lastOmocode.substr(i + 1)
results.push(lastOmocode + this.getCheckCode(lastOmocode))
}
@ -156,8 +156,8 @@ module.exports = class CodiceFiscale {
var birthplace = ''
var birthplaceProvincia = ''
for (var province in CODICI_CATASTALI) {
birthplace = CODICI_CATASTALI[province].find(function (code) {
for (var province in this.CODICI_CATASTALI) {
birthplace = this.CODICI_CATASTALI[province].find(function (code) {
return code[1] === codiceFiscale.substr(11, 4)
})
if (birthplace) {
@ -179,3 +179,12 @@ module.exports = class CodiceFiscale {
}
}
}
CodiceFiscale.MONTH_CODES = MONTH_CODES
CodiceFiscale.CHECK_CODE_ODD = CHECK_CODE_ODD
CodiceFiscale.CHECK_CODE_EVEN = CHECK_CODE_EVEN
CodiceFiscale.OMOCODIA_TABLE = OMOCODIA_TABLE
CodiceFiscale.CHECK_CODE_CHARS = CHECK_CODE_CHARS
CodiceFiscale.CODICI_CATASTALI = CODICI_CATASTALI
module.exports = CodiceFiscale