improved check on town name

This commit is contained in:
Marco Pesani 2017-12-19 11:56:42 +01:00
parent b907924cca
commit 6cb414910e
5 changed files with 29 additions and 8 deletions

File diff suppressed because one or more lines are too long

3
package-lock.json generated
View File

@ -5537,8 +5537,7 @@
"lodash": {
"version": "4.17.4",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz",
"integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=",
"dev": true
"integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4="
},
"lodash.cond": {
"version": "4.5.2",

View File

@ -10,7 +10,9 @@
"directories": {
"test": "tests"
},
"dependencies": {},
"dependencies": {
"lodash": "^4.17.4"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-jest": "^22.0.1",

View File

@ -7,6 +7,8 @@ import {
CODICI_CATASTALI
} from './constants'
import { deburr, replace } from 'lodash'
export default class CodiceFiscale {
static compute (codiceFiscaleObject) {
let code = this.surnameCode(codiceFiscaleObject.surname)
@ -82,12 +84,22 @@ export default class CodiceFiscale {
}
static findComuneCode (birthplace, birthplaceProvincia) {
for (var i = CODICI_CATASTALI[birthplaceProvincia].length - 1; i >= 0; i--) {
var comune = CODICI_CATASTALI[birthplaceProvincia][i]
if (comune[0] === birthplace.trim().toUpperCase()) return comune[1]
const code = CODICI_CATASTALI[birthplaceProvincia].reduce((accumulator, comune) => {
if (this.normalizeString(comune[0]) === this.normalizeString(birthplace)) {
accumulator = comune[1]
}
return accumulator
}, undefined)
if (code === undefined) {
throw new Error('Comune not found')
}
return code
}
static normalizeString (str) {
return replace(deburr(str).trim().toUpperCase(), /('|\s)/ig, '')
}
static getOmocodie (code) {
var results = []

View File

@ -114,6 +114,14 @@ describe('CodiceFiscale.findComuneCode', () => {
expect(CodiceFiscale.findComuneCode('Roma', 'RM')).toBe('H501')
})
test('trova il codice di un comune che contiene apostrofi', () => {
expect(CodiceFiscale.findComuneCode("Sant'Angelo Romano", 'RM')).toBe('I284')
})
test('trova il codice di un comune che contiene lettere accentate', () => {
expect(CodiceFiscale.findComuneCode('Riccò del Golfo di Spezia', 'SP')).toBe('H275')
})
test('se il comune non esiste lancia un eccezione', () => {
var comuneInventato = function () {
CodiceFiscale.findComuneCode('Pufflandia', 'RM')