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": { "lodash": {
"version": "4.17.4", "version": "4.17.4",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz",
"integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4="
"dev": true
}, },
"lodash.cond": { "lodash.cond": {
"version": "4.5.2", "version": "4.5.2",

View File

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

View File

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

View File

@ -114,6 +114,14 @@ describe('CodiceFiscale.findComuneCode', () => {
expect(CodiceFiscale.findComuneCode('Roma', 'RM')).toBe('H501') 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', () => { test('se il comune non esiste lancia un eccezione', () => {
var comuneInventato = function () { var comuneInventato = function () {
CodiceFiscale.findComuneCode('Pufflandia', 'RM') CodiceFiscale.findComuneCode('Pufflandia', 'RM')