Skip to content

Commit

Permalink
Improved omocodie and reverse handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lucavandro committed Apr 5, 2024
1 parent 94970f7 commit c74ce4b
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/codice.fiscale.amd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/codice.fiscale.commonjs2.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/codice.fiscale.umd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/codice.fiscale.var.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codice-fiscale-js",
"version": "2.3.20",
"version": "2.3.21",
"description": "The Italian Tax Code Library for Javascript and Typescript",
"main": "dist/codice.fiscale.commonjs2.js",
"types": "types/codice-fiscale.d.ts",
Expand Down
35 changes: 21 additions & 14 deletions src/codice-fiscale.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Comune } from './comune'
import { CHECK_CODE_CHARS, CHECK_CODE_EVEN, CHECK_CODE_ODD, MONTH_CODES, OMOCODIA_TABLE, OMOCODIA_TABLE_INVERSE } from './constants'
import { CHECK_CODE_CHARS, CHECK_CODE_EVEN, CHECK_CODE_ODD, MONTH_CODES, NUMERIC_POS, OMOCODIA_TABLE, OMOCODIA_TABLE_INVERSE } from './constants'
import { extractConsonants, extractVowels, getValidDate, birthplaceFields, getAllSubsets } from './utils'

class CodiceFiscale {
Expand Down Expand Up @@ -92,7 +92,7 @@ class CodiceFiscale {
return CodiceFiscale.getCheckCode(cf).toUpperCase() === expectedCheckCode.toUpperCase();
}
static isOmocodia(cf){
for(const pos of [6,7,9,10,12,13,14]){
for(const pos of NUMERIC_POS){
if(!/^[0-9]$/.test(cf[pos])) return true;
}
return false;
Expand Down Expand Up @@ -137,6 +137,17 @@ class CodiceFiscale {
}
return input
}

static fromOmocodiaToOriginal(code){
code = code.substr(0,15)
for(let pos of NUMERIC_POS){
let char = code[pos]
if (char.match(/[A-Z]/i))
code = `${code.substr(0, pos)}${OMOCODIA_TABLE_INVERSE[char]}${code.substr(pos + 1)}`
}
code = code + CodiceFiscale.getCheckCode(code)
return code
}
toString () {
return this.code
}
Expand Down Expand Up @@ -170,8 +181,7 @@ class CodiceFiscale {
omocodie () {
const results = []
let code= (this.code.slice(0, 15))
const numericCharPos = [14, 13, 12, 10, 9, 7, 6]
const allSubsets = getAllSubsets(numericCharPos)
const allSubsets = getAllSubsets(NUMERIC_POS)
for(let subset of allSubsets){
let omocode = code
for(let position of subset){
Expand All @@ -193,29 +203,26 @@ class CodiceFiscale {
this.code = code
}
reverse () {
this.name = this.code.substr(3, 3)
this.surname = this.code.substr(0, 3)
const code = CodiceFiscale.isOmocodia(this.code) ? CodiceFiscale.fromOmocodiaToOriginal(this.code) : this.code
this.name = code.substr(3, 3)
this.surname = code.substr(0, 3)

let yearCode = this.code.substr(6, 2)
yearCode = CodiceFiscale.toNumberIfOmocodia(yearCode);
let yearCode = code.substr(6, 2)
const year19XX = parseInt(`19${yearCode}`, 10)
const year20XX = parseInt(`20${yearCode}`, 10)
const currentYear20XX = new Date().getFullYear()
const year = year20XX > currentYear20XX ? year19XX : year20XX
const monthChar = this.code.substr(8, 1)
const monthChar = code.substr(8, 1)
const month = MONTH_CODES.indexOf(monthChar)
this.gender = 'M'
let dayString = this.code.substr(9, 2);
dayString = CodiceFiscale.toNumberIfOmocodia(dayString);
let dayString = code.substr(9, 2);
let day = parseInt(dayString, 10)
if (day > 31) {
this.gender = 'F'
day = day - 40
}
this.birthday = new Date(Date.UTC(year, month, day, 0, 0, 0, 0))
let cc = this.code.substr(11, 4)
const ccNumbers = CodiceFiscale.toNumberIfOmocodia(cc.substr(1, 3));
cc = cc.charAt(0) + ccNumbers;
let cc = code.substr(11, 4)
this.birthplace = Comune.GetByCC(cc)
return this.toJSON()
}
Expand Down
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,5 @@ export const OMOCODIA_TABLE_INVERSE = {
}

export const CHECK_CODE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

export const NUMERIC_POS = [6,7,9,10,12,13,14]
32 changes: 31 additions & 1 deletion tests/omocodie.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ describe('CodiceFiscale.getOmocodie', () => {
expect(CodiceFiscale.check(omocodia)).toEqual(true)
})

test('tutte le omocodie generate dal metodo vengono riconosciute come omocodie', () => {
const omocodie = CodiceFiscale.getOmocodie('BNZVCN32S10E573Z');
for(let omocodia of omocodie)
expect(CodiceFiscale.isOmocodia(omocodia)).toEqual(true)
})


})

Expand All @@ -41,4 +47,28 @@ describe('CodiceFiscale.isOmocodia', () => {
expect(CodiceFiscale.isOmocodia('BNZVCNPNSMLERTPX')).toEqual(true);
expect(CodiceFiscale.isOmocodia('CCHGNN67R05H1S3I')).toEqual(true);
})
})

})



describe('CodiceFiscale.fromOmocodiaToOriginal', () => {
test('è definito', () => {
expect(CodiceFiscale.fromOmocodiaToOriginal).toBeDefined()
})

test('calcola la versione non omocodica di un omocodia', () => {
expect(CodiceFiscale.fromOmocodiaToOriginal('BNZVCN32S10E57PV'))
.toEqual('BNZVCN32S10E573Z')
})


test('tutte le omocodie vengono riconosciute come codici fiscali validi', () => {
const omocodie = CodiceFiscale.getOmocodie('BNZVCN32S10E573Z');
for(let omocodia of omocodie)
expect(CodiceFiscale.fromOmocodiaToOriginal(omocodia)).toEqual('BNZVCN32S10E573Z')
})



})

0 comments on commit c74ce4b

Please sign in to comment.