Fixed bug with Bolzano (issue #41)

This commit is contained in:
Luca Vandro 2020-09-17 16:04:50 +02:00
parent 6f29f10ba4
commit feea1bdc28
8 changed files with 60 additions and 73 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

View File

@ -1,6 +1,6 @@
{ {
"name": "codice-fiscale-js", "name": "codice-fiscale-js",
"version": "2.3.3", "version": "2.3.4",
"description": "The Italian Tax Code Library for Javascript and Typescript", "description": "The Italian Tax Code Library for Javascript and Typescript",
"main": "dist/codice.fiscale.commonjs2.js", "main": "dist/codice.fiscale.commonjs2.js",
"types": "types/codice-fiscale.d.ts", "types": "types/codice-fiscale.d.ts",

View File

@ -8,6 +8,10 @@ CodiceFiscale.js is a javascript and typescript utility library to compute and v
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W2M92TNMXR3CC&source=url) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W2M92TNMXR3CC&source=url)
## Change log ## Change log
**Version 2.3.4**
- Improved search
- Bug fixes
**Version 2.3.0** **Version 2.3.0**
- Complete italian comuni list (comuni soppressi, che hanno cambiato provincia) [last update 29/05/2020] - Complete italian comuni list (comuni soppressi, che hanno cambiato provincia) [last update 29/05/2020]
- Python script to update data directly from the ISTAT website - Python script to update data directly from the ISTAT website

View File

@ -6,8 +6,8 @@ export class Comune {
} }
constructor (nome, prov, cc, check = true) { constructor (nome, prov, cc, check = true) {
if (check || cc === undefined || prov === undefined) { if (check || cc === undefined || prov === undefined) {
let comune let comune = this.searchByNameAndProvince(nome, prov)
comune = prov !== undefined ? this.searchByNameAndProvince(nome, prov) : this.searchByName(nome)
if (comune === undefined && nome.length === 4) { if (comune === undefined && nome.length === 4) {
comune = this.searchByCC(nome) comune = this.searchByCC(nome)
} }
@ -55,74 +55,31 @@ export class Comune {
return result.toJSON() return result.toJSON()
} }
} }
searchByName (nome) { searchByName (nome ) {
const query = normalizeString(nome) this.searchByNameAndProvince(nome)
let left = 0
let right = COMUNI.length - 1
const result = []
while (left <= right) {
const middle = Math.floor((left + right) / 2)
const currentItem = COMUNI[middle]
if (query === currentItem[2]) {
result.push(currentItem)
if (middle > 0 && COMUNI[middle - 1][2] === query) {
result.push(COMUNI[middle - 1])
} else if (middle < COMUNI.length - 1 && COMUNI[middle + 1][2] === query) {
result.push(COMUNI[middle + 1])
}
break
} else if (query < currentItem[2]) {
right = middle - 1
} else {
left = middle + 1
}
}
if (result.length === 1) {
return { cc: result[0][0], prov: result[0][1], nome: result[0][2] }
} else if (result.length > 1) {
for(let i=0; i < result.length; i++){
if (result[i][1]!=result[0][1]){
throw new Error(`Comune with name of ${nome} is found in more than one province. Please specify the province code`);
}
}
for(let i=0; i < result.length; i++){
if (result[i][3]==1){
return { cc: result[i][0], prov: result[i][1], nome: result[i][2] }
}
}
}
} }
searchByNameAndProvince (nome, prov) { searchByNameAndProvince (nome, prov) {
const query = normalizeString(nome) const qNome = normalizeString(nome)
let left = 0 const qProv= prov && normalizeString(prov)
let right = COMUNI.length - 1 let results = COMUNI.filter((c)=>qProv? c[1]===qProv && c[2]===qNome: c[2]===qNome).map((c)=>{
let result return { cc: c[0], prov: c[1], nome: c[2], active:c[3]===1 }
while (left <= right) { })
const middle = Math.floor((left + right) / 2)
const currentItem = COMUNI[middle] // One results: no problem!
if (query === currentItem[2]) { if (results.length === 1) {
if (prov === currentItem[1]) { return results[0]
result = currentItem }
} else if (middle > 0 && COMUNI[middle - 1][2] === query && prov === COMUNI[middle - 1][1]) {
result = COMUNI[middle - 1] // if many results look for the active one
} else if (middle < COMUNI.length - 1 && COMUNI[middle + 1][2] === query && prov === COMUNI[middle + 1][1]) { results = results.filter(c=> c.active)
result = COMUNI[middle + 1]
} if(results.length === 1)
break return results[0]
} else if (query < currentItem[2]) { else if(prov)
right = middle - 1 throw new Error(`Comune with name of ${nome} and prov ${prov} doesn't exists`)
} else { else
left = middle + 1 throw new Error(`Comune with name of ${nome} is found in more than one province. Please specify the province code`)
}
}
if (result !== undefined) {
return { cc: result[0], prov: result[1], nome: result[2] }
} else {
throw new Error(`Comune with name of ${nome} and prov ${prov} doesn't exists. Left:${left} Right:${left} `)
}
} }
toJSON () { toJSON () {

View File

@ -1,4 +1,5 @@
import CodiceFiscale from '../src/codice-fiscale.js'; import CodiceFiscale from '../src/codice-fiscale.js';
import { Comune } from '../src/comune.js';
let { describe, test, expect } = global let { describe, test, expect } = global
@ -75,6 +76,21 @@ describe('CodiceFiscale.compute', () => {
.toBe('MRNLCU00A01H501J') .toBe('MRNLCU00A01H501J')
}) })
test('calcola il codice fiscale anche per i nati nel comune di Bolzano 😁', () => {
expect(CodiceFiscale.compute({
name: 'Mario',
surname: 'Rossi',
gender: 'M',
day: 1,
month: 1,
year: 1980,
birthplace: 'Bolzano',
birthplaceProvincia: 'BZ'
}))
.toBe('RSSMRA80A01A952F')
})
test("calcola il codice fiscale di persone nate all'estero", () => { test("calcola il codice fiscale di persone nate all'estero", () => {
expect(CodiceFiscale.compute({ expect(CodiceFiscale.compute({
name: 'Luca', name: 'Luca',
@ -396,3 +412,13 @@ describe('Calcolo del codice fiscale inverso',()=>{
}); });
}) })
describe("La classe Comune", ()=>{
test("trova il comune di Bolzano", ()=>{
let bz = new Comune("Bolzano", "BZ")
expect(bz.nome).toEqual('BOLZANO')
})
})