mirror of
https://github.com/danog/CodiceFiscaleJS.git
synced 2024-11-26 20:14:55 +01:00
Fixed bug with Bolzano (issue #41)
This commit is contained in:
parent
6f29f10ba4
commit
feea1bdc28
2
dist/codice.fiscale.amd.js
vendored
2
dist/codice.fiscale.amd.js
vendored
File diff suppressed because one or more lines are too long
2
dist/codice.fiscale.commonjs2.js
vendored
2
dist/codice.fiscale.commonjs2.js
vendored
File diff suppressed because one or more lines are too long
2
dist/codice.fiscale.umd.js
vendored
2
dist/codice.fiscale.umd.js
vendored
File diff suppressed because one or more lines are too long
2
dist/codice.fiscale.var.js
vendored
2
dist/codice.fiscale.var.js
vendored
File diff suppressed because one or more lines are too long
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "codice-fiscale-js",
|
||||
"version": "2.3.3",
|
||||
"version": "2.3.4",
|
||||
"description": "The Italian Tax Code Library for Javascript and Typescript",
|
||||
"main": "dist/codice.fiscale.commonjs2.js",
|
||||
"types": "types/codice-fiscale.d.ts",
|
||||
|
@ -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)
|
||||
|
||||
## Change log
|
||||
**Version 2.3.4**
|
||||
- Improved search
|
||||
- Bug fixes
|
||||
|
||||
**Version 2.3.0**
|
||||
- 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
|
||||
|
@ -6,8 +6,8 @@ export class Comune {
|
||||
}
|
||||
constructor (nome, prov, cc, check = true) {
|
||||
if (check || cc === undefined || prov === undefined) {
|
||||
let comune
|
||||
comune = prov !== undefined ? this.searchByNameAndProvince(nome, prov) : this.searchByName(nome)
|
||||
let comune = this.searchByNameAndProvince(nome, prov)
|
||||
|
||||
if (comune === undefined && nome.length === 4) {
|
||||
comune = this.searchByCC(nome)
|
||||
}
|
||||
@ -55,74 +55,31 @@ export class Comune {
|
||||
return result.toJSON()
|
||||
}
|
||||
}
|
||||
searchByName (nome) {
|
||||
const query = normalizeString(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] }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
searchByName (nome ) {
|
||||
this.searchByNameAndProvince(nome)
|
||||
}
|
||||
searchByNameAndProvince (nome, prov) {
|
||||
const query = normalizeString(nome)
|
||||
let left = 0
|
||||
let right = COMUNI.length - 1
|
||||
let result
|
||||
while (left <= right) {
|
||||
const middle = Math.floor((left + right) / 2)
|
||||
const currentItem = COMUNI[middle]
|
||||
if (query === currentItem[2]) {
|
||||
if (prov === currentItem[1]) {
|
||||
result = currentItem
|
||||
} else if (middle > 0 && COMUNI[middle - 1][2] === query && prov === COMUNI[middle - 1][1]) {
|
||||
result = COMUNI[middle - 1]
|
||||
} else if (middle < COMUNI.length - 1 && COMUNI[middle + 1][2] === query && prov === COMUNI[middle + 1][1]) {
|
||||
result = COMUNI[middle + 1]
|
||||
}
|
||||
break
|
||||
} else if (query < currentItem[2]) {
|
||||
right = middle - 1
|
||||
} else {
|
||||
left = middle + 1
|
||||
}
|
||||
}
|
||||
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} `)
|
||||
const qNome = normalizeString(nome)
|
||||
const qProv= prov && normalizeString(prov)
|
||||
let results = COMUNI.filter((c)=>qProv? c[1]===qProv && c[2]===qNome: c[2]===qNome).map((c)=>{
|
||||
return { cc: c[0], prov: c[1], nome: c[2], active:c[3]===1 }
|
||||
})
|
||||
|
||||
// One results: no problem!
|
||||
if (results.length === 1) {
|
||||
return results[0]
|
||||
}
|
||||
|
||||
// if many results look for the active one
|
||||
results = results.filter(c=> c.active)
|
||||
|
||||
if(results.length === 1)
|
||||
return results[0]
|
||||
else if(prov)
|
||||
throw new Error(`Comune with name of ${nome} and prov ${prov} doesn't exists`)
|
||||
else
|
||||
throw new Error(`Comune with name of ${nome} is found in more than one province. Please specify the province code`)
|
||||
|
||||
}
|
||||
|
||||
toJSON () {
|
||||
|
@ -1,4 +1,5 @@
|
||||
import CodiceFiscale from '../src/codice-fiscale.js';
|
||||
import { Comune } from '../src/comune.js';
|
||||
|
||||
let { describe, test, expect } = global
|
||||
|
||||
@ -75,6 +76,21 @@ describe('CodiceFiscale.compute', () => {
|
||||
.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", () => {
|
||||
expect(CodiceFiscale.compute({
|
||||
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')
|
||||
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user