Added github pages

This commit is contained in:
Valentina Vandro 2018-10-08 11:47:18 +02:00
parent 50524230e9
commit c6a4dfc64c
30 changed files with 26 additions and 22201 deletions

21
LICENSE
View File

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2016 Luca Adalberto Vandro
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,28 +0,0 @@
{
"name": "codice-fiscale-js",
"description": "Calcolo del codice fiscale",
"main": "dist/codice.fiscale.js",
"authors": [
"Luca Vandro",
"Walter Barbagallo"
],
"license": "MIT",
"keywords": [
"codice",
"fiscale",
"italian",
"social",
"security",
"number"
],
"homepage": "https://github.com/lucavandro/CodiceFiscaleJS",
"moduleType": [],
"ignore": [
"**/.*",
"lib",
"node_modules",
"bower_components",
"test",
"tests"
]
}

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,88 +0,0 @@
# Javascript
Create and compute
-------
Compute a codice fiscale given:
- Name (String)
- Surname (String)
- Gender (String) ["M","F"]
- Birthday day (Number)
- Birthday month (Number)
- Birthday year (Number)
- Place of birth (String)
- Province of birth (String)
```js
var cf = new CodiceFiscale({
name: "Enzo",
surname: "Righi",
gender: "M",
day: 24,
month: 7,
year: 1957,
birthplace: "Napoli",
birthplaceProvincia: "NA" // Optional
});
console.log(cf);
```
Support foreign countries
===
Use the italian name of the foreign country (e.g. Francia, for France) as birthplace
and "EE" as birthplace_provincia
```js
var cf = new CodiceFiscale({
name: "Enzo",
surname: "Righi",
gender: "M",
day: 24,
month: 7,
year: 1957,
birthplace: "Francia",
birthplaceProvincia: "EE"
});
console.log(cf);
```
----------
Inverse Computation
-------
Get a person data for a given Codice Fiscale. Throws an exeption if the code provided as argument is invalid.
```js
var cf = new CodiceFiscale("RGHNZE10L24F839E");
console.log(cf.toJSON());
/**
OUTPUT
{
name: "NZE",
surname: "RGH",
gender: "M",
day: 24,
month: 7,
year: 2010,
birthplace: "NAPOLI",
birthplaceProvincia: "NA"
}
*/
```
Or you can access reversed attribute via instance properties:
```js
var cf = new CodiceFiscale("RGHNZE10L24F839E");
console.log(cf.toJSON());
cf.name === "NZE"; // true
cf.surname === "RGH"; // true
cf.gender === "M"; // true
cf.day === 24; // true
cf.month === 7; // true
cf.year === 2010; // true
cf.birthday === new Date(2010, 6, 24,0,0,0,0); // true
cf.birthplace === "NAPOLI"; // true
cf.birthplaceProvincia === "NA"; // true
```
Omocodie
-------
Get all the omocodie for a given Codice Fiscale. It returns an array of strings
```js
var cf = new CodiceFiscale("VNDLDL10A01G410Z");
var omocodie = cf.omocodie();
```

View File

@ -1,89 +0,0 @@
# Javascript v1.2.0 Deprecated
Compute
-------
Compute a codice fiscale given:
- Name (String)
- Surname (String)
- Gender (String) ["M","F"]
- Birthday day (Number)
- Birthday month (Number)
- Birthday year (Number)
- Place of birth (String)
- Province of birth (String)
```js
var cf = CodiceFiscale.compute({
name: "Enzo",
surname: "Righi",
gender: "M",
day: 24,
month: 7,
year: 1957,
birthplace: "Napoli",
birthplaceProvincia: "NA"});
```
Support foreign countries
===
Use the italian name of the foreign country (e.g. Francia, for France) as birthplace
and "EE" as birthplace_provincia
```js
var cf = CodiceFiscale.compute({
name: "Enzo",
surname: "Righi",
gender: "M",
day: 24,
month: 7,
year: 1957,
birthplace: "Francia",
birthplaceProvincia: "EE"});
```
----------
Inverse Computation
-------
Get a person data for a given Codice Fiscale. It returns an **object**:
- Name (String)
- Surname (String)
- Gender (String)
- Birthday day (Number)
- Birthday month (Number)
- Birthday year [Number]
- Place of birth (String)
- Province of birth (String)
```js
var personData = CodiceFiscale.computeInverse("RGHNZE10L24F839E");
// personData
{
name: "NZE",
surname: "RGH",
gender: "M",
day: 24,
month: 7
year: [1910, 2010],
birthplace: "NAPOLI",
birthplaceProvincia: "NA"
}
```
----------
Check
-------
Check if a codice fiscale is valid. It returns a **boolean** value.
```js
var isValid = CodiceFiscale.check("VNDLDL10A01G410Z");
```
----------
Omocodie
-------
Get all the omocodie for a given Codice Fiscale. It returns an array of strings
```js
var omocodie = CodiceFiscale.getOmocodie("VNDLDL10A01G410Z");
```

View File

@ -1,89 +0,0 @@
# TypeScript
Create and compute
-------
Compute a codice fiscale given:
- Name (String)
- Surname (String)
- Gender (String) ["M","F"]
- Birthday day (Number)
- Birthday month (Number)
- Birthday year (Number)
- Place of birth (String)
- Province of birth (String)
```js
import { CodiceFiscale } from 'codice-fiscale-js';
const cf = new CodiceFiscale({
name: "Enzo",
surname: "Righi",
gender: "M",
day: 24,
month: 7,
year: 1957,
birthplace: "Napoli",
birthplaceProvincia: "NA" // Optional
});
console.log(cf);
```
Support foreign countries
===
Use the italian name of the foreign country (e.g. Francia, for France) as birthplace
and "EE" as birthplace_provincia
```js
const cf = new CodiceFiscale({
name: "Enzo",
surname: "Righi",
gender: "M",
day: 24,
month: 7,
year: 1957,
birthplace: "Francia",
birthplaceProvincia: "EE"
});
console.log(cf);
```
----------
Inverse Computation
-------
Get a person data for a given Codice Fiscale. Throws an exeption if the code provided as argument is invalid.
```js
const cf = new CodiceFiscale("RGHNZE10L24F839E");
console.log(cf.toJSON());
/**
OUTPUT
{
name: "NZE",
surname: "RGH",
gender: "M",
day: 24,
month: 7,
year: 2010,
birthplace: "NAPOLI",
birthplaceProvincia: "NA"
}
*/
```
Or you can access reversed attribute via instance properties:
```js
const cf = new CodiceFiscale("RGHNZE10L24F839E");
console.log(cf.toJSON());
cf.name === "NZE"; // true
cf.surname === "RGH"; // true
cf.gender === "M"; // true
cf.day === 24; // true
cf.month === 7; // true
cf.year === 2010; // true
cf.birthday === new Date(2010, 6, 24,0,0,0,0); // true
cf.birthplace === "NAPOLI"; // true
cf.birthplaceProvincia === "NA"; // true
```
Omocodie
-------
Get all the omocodie for a given Codice Fiscale. It returns an array of strings
```js
const cf = new CodiceFiscale("VNDLDL10A01G410Z");
const omocodie = cf.omocodie();
```

File diff suppressed because one or more lines are too long

View File

@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="bootstrap.min.css">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<title>CodiceFiscaleJS</title>
<script src="jquery-3.3.1.min.js"></script>
<script src="bootstrap.bundle.min.js"></script>
@ -15,11 +16,29 @@
body{
background-color: #f7f7f9;
}
footer{
padding: 3em 0;
}
</style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Codice Fiscale JS</a>
<div class="container-fluid">
<a class="navbar-brand" href="#">Codice Fiscale JS</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="https://github.com/lucavandro/CodiceFiscaleJS"> <i class="fa fa-github"></i>GitHub</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="https://github.com/lucavandro/CodiceFiscaleJS/tree/master/docs"><i class="fa fa-book"></i> Docs </a>
</li>
</div>
</div>
</nav>
<div class="jumbotron jumbotron-fluid text-center">
<div class="container">
@ -179,6 +198,12 @@
</div>
</div>
<footer>
<div class="container text-center">
Made with <i class="fa fa-heart text-danger"></i> by <a href="https://lucavandro.it">Luca Vandro</a>
</div>
</footer>
<script>
$(function(){
/*

9683
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,44 +0,0 @@
{
"name": "codice-fiscale-js",
"version": "2.1.0",
"description": "The Italian Tax Code Library for Javascript and Typescript",
"main": "dist/codice.fiscale.umd.js",
"repository": {
"type": "git",
"url": "git://github.com/lucavandro/CodiceFiscaleJS.git"
},
"directories": {
"test": "tests"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-jest": "^22.0.1",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.7.0",
"eslint": "^3.0.0",
"jest": "^22.0.1",
"parallel-webpack": "^2.2.0",
"regenerator-runtime": "^0.11.1",
"request": "^2.88.0",
"standard": "^10.0.3",
"webpack": "^4.20.2"
},
"scripts": {
"build": "webpack && cp ./dist/codice.fiscale.var.js ./example/",
"clean": "rm ./example/codice.fiscale.var.js",
"pretest": "npm run build",
"test": "standard && jest",
"fix": "standard --fix"
},
"author": "Luca Vandro <lucavandro@gmail.com>",
"contributors": [
"Walter Barbagallo <brb.walter@gmail.com>",
"Marco Pesani <marco.pesani@gmail.com>"
],
"license": "MIT",
"standard": {
"ignore": [
"dist/"
]
}
}

View File

@ -1,31 +0,0 @@
# CodiceFiscale.js
CodiceFiscale.js is a javascript and typescript utility library to compute and validate Italian Italian Tax code (codice fiscale).
## Change log
**Version 2.0.0**
- Added TypeScript support
- Added Object-oriented API
- Improved performance
- Improved error detection
- Backcompatible
## Installation
**Node**
```sh
npm install codice-fiscale-js --save
```
**Web**
```html
<script src="/dist/codice.fiscale.var.js"></script>
```
AMD, UMD e CommonJS version are available. Check `dist` folder
## Docs
- [Javascript](https://github.com/lucavandro/CodiceFiscaleJS/tree/master/docs/js-oop.md)
- [TypeScript](https://github.com/lucavandro/CodiceFiscaleJS/tree/master/docs/typescript.md)
- [Javascript v1.2.0 deprecated](https://github.com/lucavandro/CodiceFiscaleJS/tree/master/docs/js-static.md)
## Available npm scripts:
- `npm run build`: build the bundle into `dist` directory.
- `npm run test`: launch the tests.

View File

@ -1,204 +0,0 @@
import { Comune } from './comune';
import { CHECK_CODE_CHARS, CHECK_CODE_EVEN, CHECK_CODE_ODD, MONTH_CODES, OMOCODIA_TABLE } from './constants';
import { extractConsonants, extractVowels, getValidDate, birthplaceFields } from './utils';
class CodiceFiscale {
get day() {
return this.birthday.getDate();
}
set day(d) {
this.birthday.setDate(d);
}
get month() {
return this.birthday.getMonth() + 1;
}
set month(m) {
this.birthday.setMonth(m - 1);
}
get year() {
return this.birthday.getFullYear();
}
set year(y) {
this.birthday.setFullYear(y);
}
get cf() {
return this.code;
}
get nameCode() {
return this.code.substr(3, 3);
}
get surnameCode() {
return this.code.substr(0, 3);
}
get checkCode() {
return this.code.substr(15, 1);
}
constructor(data) {
if (typeof data === 'string') {
if (CodiceFiscale.check(data)) {
this.code = data;
this.reverse();
}
else {
throw new Error('Provided input is not a valid Codice Fiscale');
}
}
else if (typeof data === 'object') {
const cfData = data;
this.name = cfData.name;
this.surname = cfData.surname;
this.gender = this.checkGender(cfData.gender);
this.birthday = cfData.birthday ? getValidDate(cfData.birthday) : getValidDate(cfData.day, cfData.month, cfData.year);
this.birthplace = new Comune(cfData.birthplace, cfData.birthplaceProvincia);
this.compute();
}
else {
throw new Error('Comune constructor accept either a string or a plain object. Check the documentation');
}
}
static getCheckCode(codiceFiscale) {
let val = 0;
for (let i = 0; i < 15; i = i + 1) {
const c = codiceFiscale[i];
val += i % 2 !== 0 ? CHECK_CODE_EVEN[c] : CHECK_CODE_ODD[c];
}
val = val % 26;
return CHECK_CODE_CHARS.charAt(val);
}
static findLocationCode(name, prov) {
return new Comune(name, prov).cc;
}
static computeInverse(codiceFiscale) {
return new CodiceFiscale(codiceFiscale).toJSON();
}
static compute(obj) {
return new CodiceFiscale(obj).toString();
}
static check(codiceFiscale) {
if (typeof codiceFiscale !== 'string') {
return false;
}
let cf = codiceFiscale.toUpperCase();
if (cf.length !== 16) {
return false;
}
const expectedCheckCode = codiceFiscale.charAt(15);
cf = codiceFiscale.slice(0, 15);
return CodiceFiscale.getCheckCode(cf) === expectedCheckCode;
}
static getOmocodie(cf) {
return new CodiceFiscale(cf).omocodie();
}
toString() {
return this.code;
}
toJSON() {
return {
name: this.name,
surname: this.surname,
gender: this.gender,
birthday: this.birthday.toISOString().slice(0,10),
birthplace: this.birthplace.nome,
birthplaceProvincia: this.birthplace.prov,
cf: this.code,
};
}
isValid() {
if (typeof this.code !== 'string') {
return false;
}
this.code = this.code.toUpperCase();
if (this.code.length !== 16) {
return false;
}
const expectedCheckCode = this.code.charAt(15);
const cf = this.code.slice(0, 15);
return CodiceFiscale.getCheckCode(cf) === expectedCheckCode;
}
omocodie() {
const results = [];
let lastOmocode = (this.code = this.code.slice(0, 15));
for (let i = this.code.length - 1; i >= 0; i = i - 1) {
const char = this.code[i];
if (char.match(/\d/) !== null) {
lastOmocode = `${lastOmocode.substr(0, i)}${OMOCODIA_TABLE[char]}${lastOmocode.substr(i + 1)}`;
results.push(lastOmocode + CodiceFiscale.getCheckCode(lastOmocode));
}
}
return results;
}
compute() {
let code = this.getSurnameCode();
code += this.getNameCode();
code += this.dateCode();
code += this.birthplace.cc;
code += CodiceFiscale.getCheckCode(code);
this.code = code;
}
reverse() {
this.name = this.code.substr(3, 3);
this.surname = this.code.substr(0, 3);
const yearCode = this.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 month = MONTH_CODES.indexOf(monthChar);
this.gender = 'M';
let day = parseInt(this.code.substr(9, 2), 10);
if (day > 31) {
this.gender = 'F';
day = day - 40;
}
this.birthday = new Date(year, month, day, 0, 0, 0, 0);
const cc = this.code.substr(11, 4);
this.birthplace = Comune.GetByCC(cc);
return this.toJSON();
}
checkGender(gender) {
this.gender = gender !== undefined ? gender.toUpperCase() : this.gender.toUpperCase();
if (typeof this.gender !== 'string') {
throw new Error('Gender must be a string');
}
if (this.gender !== 'M' && this.gender !== 'F') {
throw new Error('Gender must be either \'M\' or \'F\'');
}
return gender;
}
getSurnameCode() {
const codeSurname = `${extractConsonants(this.surname)}${extractVowels(this.surname)}XXX`;
return codeSurname.substr(0, 3).toUpperCase();
}
getNameCode() {
let codNome = extractConsonants(this.name);
if (codNome.length >= 4) {
codNome = codNome.charAt(0) + codNome.charAt(2) + codNome.charAt(3);
}
else {
codNome += `${extractVowels(this.name)}XXX`;
codNome = codNome.substr(0, 3);
}
return codNome.toUpperCase();
}
dateCode() {
let year = `0${this.birthday.getFullYear()}`;
year = year.substr(year.length - 2, 2);
const month = MONTH_CODES[this.birthday.getMonth()];
let day = this.birthday.getDate();
if (this.gender.toUpperCase() === 'F') {
day += 40;
}
let dayStr = `0${day}`;
dayStr = dayStr.substr(dayStr.length - 2, 2);
return String(year + month + dayStr);
}
}
CodiceFiscale.utils = {
birthplaceFields : birthplaceFields
}
module.exports = CodiceFiscale

View File

@ -1,134 +0,0 @@
import { COMUNI } from './geo-data';
import { normalizeString } from './utils';
export class Comune {
get nomeNorm() {
return normalizeString(this.nome);
}
constructor(nome, prov, cc, check = true) {
if (check || cc === undefined || prov === undefined) {
let comune;
comune = prov !== undefined ? this.searchByNameAndProvince(nome, prov) : this.searchByName(nome);
if (comune === undefined && nome.length === 4) {
comune = this.searchByCC(nome);
}
if (comune === undefined) {
throw new Error(`Comune with name ${nome} doesn't exist`);
}
else if (cc !== undefined && comune.cc !== cc) {
throw new Error(`Comune with cc ${cc} doesn't exist`);
}
else {
this.nome = comune.nome;
this.prov = comune.prov;
this.cc = comune.cc;
}
} else {
this.nome = nome;
this.prov = prov;
this.cc = cc;
}
}
static GetByName(name, prov) {
return new Comune(name, prov);
}
static GetByCC(cc) {
let result;
for (const item of COMUNI) {
if (item[0] === cc) {
result = item;
break;
}
}
if (result !== undefined) {
return new Comune(result[2], result[1], result[0], false);
}
throw new Error(`Comune with cc ${cc} doesn't exist`);
}
searchByCC(cc) {
let result;
try{
result = Comune.GetByCC(cc);
}catch(e){}
if (result !== undefined) {
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) {
throw new Error(`Comune with name of ${nome} is found in more than one province. Please specify the province code`);
}
}
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`);
}
}
toJSON(){
return {
cc : this.cc,
nome : this.nome,
prov : this.prov
}
}
}

View File

@ -1,107 +0,0 @@
export const MONTH_CODES = [
'A',
'B',
'C',
'D',
'E',
'H',
'L',
'M',
'P',
'R',
'S',
'T'
]
export const CHECK_CODE_ODD = {
0: 1,
1: 0,
2: 5,
3: 7,
4: 9,
5: 13,
6: 15,
7: 17,
8: 19,
9: 21,
A: 1,
B: 0,
C: 5,
D: 7,
E: 9,
F: 13,
G: 15,
H: 17,
I: 19,
J: 21,
K: 2,
L: 4,
M: 18,
N: 20,
O: 11,
P: 3,
Q: 6,
R: 8,
S: 12,
T: 14,
U: 16,
V: 10,
W: 22,
X: 25,
Y: 24,
Z: 23
}
export const CHECK_CODE_EVEN = {
0: 0,
1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
A: 0,
B: 1,
C: 2,
D: 3,
E: 4,
F: 5,
G: 6,
H: 7,
I: 8,
J: 9,
K: 10,
L: 11,
M: 12,
N: 13,
O: 14,
P: 15,
Q: 16,
R: 17,
S: 18,
T: 19,
U: 20,
V: 21,
W: 22,
X: 23,
Y: 24,
Z: 25
}
export const OMOCODIA_TABLE = {
0: 'L',
1: 'M',
2: 'N',
3: 'P',
4: 'Q',
5: 'R',
6: 'S',
7: 'T',
8: 'U',
9: 'V'
}
export const CHECK_CODE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

File diff suppressed because it is too large Load Diff

View File

@ -1,91 +0,0 @@
import { PROVINCE, COMUNI } from './geo-data';
export function normalizeString(str) {
return str.trim()
.replace(new RegExp(/[àá]/g), 'a\'')
.replace(new RegExp(/[èé]/g), 'e\'')
.replace(new RegExp(/[ìí]/g), 'i\'')
.replace(new RegExp(/[òó]/g), 'o\'')
.replace(new RegExp(/[ùú]/g), 'u\'')
.toUpperCase();
}
export function daysInMonth(m, y) {
switch (m) {
case 1:
return (y % 4 === 0 && y % 100 !== 0) || (y % 400 === 0) ? 29 : 28;
case 8:
case 3:
case 5:
case 10:
return 30;
default:
return 31;
}
}
export function isValidDate(d, m, y) {
const month = m - 1;
return month >= 0 && month < 12 && d > 0 && d <= daysInMonth(month, y);
}
export function getValidDate(d, m, y) {
if(typeof d === 'string' && m === undefined && y === undefined){
return new Date(d);
}else if (isValidDate(d, m, y)) {
return new Date(y, m - 1, d, 0, 0, 0, 0);
}
else {
throw new Error(`The date ${y}/${m}/${d} is not a valid date`);
}
}
export function extractVowels(str) {
return str.replace(/[^AEIOU]/gi, '');
}
export function extractConsonants(str) {
return str.replace(/[^BCDFGHJKLMNPQRSTVWXYZ]/gi, '');
}
export function pad(n, size = 2) {
let s = String(n);
while (s.length < size) {
s = `0${s}`;
}
return s;
}
export function birthplaceFields(provinceSelector, birthplaceSelector) {
const provinceSelect = document.querySelector(provinceSelector);
const birthplaceSelect = document.querySelector(birthplaceSelector);
const optGroupProv = document.createElement('optgroup');
const optGroupEE = document.createElement('optgroup');
optGroupEE.label = "-----------";
provinceSelect.appendChild(optGroupProv);
provinceSelect.appendChild(optGroupEE);
Object.keys(PROVINCE).forEach((code, i) => {
const name = PROVINCE[code];
const option = document.createElement('option');
option.value = code;
option.textContent = name;
if(code === 'EE'){
optGroupEE.appendChild(option);
} else {
optGroupProv.appendChild(option);
}
});
provinceSelect.onchange = (e)=>{
let province = provinceSelect.value;
while (birthplaceSelect.firstChild) {
birthplaceSelect.removeChild(birthplaceSelect.firstChild);
}
COMUNI.forEach((comune)=>{
let cc = comune[0], nome = comune[2], prov = comune[1];
if(prov === province){
let option = document.createElement('option');
option.value = cc;
option.textContent = nome.toLowerCase().replace(/\b\w/g, l => l.toUpperCase());
birthplaceSelect.appendChild(option);
}
});
}
provinceSelect.selectedIndex = "0";
provinceSelect.onchange();
}

View File

@ -1,212 +0,0 @@
import CodiceFiscale from '../src/index'
let { describe, test, expect } = global
describe('Codice Fiscale', () => {
test('esiste un oggetto chiamato CodiceFiscale', () => {
expect(CodiceFiscale).not.toBe(undefined)
})
})
describe('CodiceFiscale.surnameCode', () => {
test('è definito', () => {
expect(CodiceFiscale.surnameCode).not.toBe(undefined)
})
test('restituisce il corretto risultato in caso di sufficienti consonanti', () => {
expect(CodiceFiscale.surnameCode('Moreno')).toBe('MRN')
})
test('restituisce il corretto risultato in caso di insufficienti consonanti', () => {
expect(CodiceFiscale.surnameCode('Julea')).toBe('JLU')
})
})
describe('CodiceFiscale.nameCode', () => {
test('è definito', () => {
expect(CodiceFiscale.nameCode).not.toBe(undefined)
})
test('restituisce il corretto risultato in caso di sufficienti consonanti', () => {
expect(CodiceFiscale.nameCode('Marco')).toBe('MRC')
})
test('restituisce il corretto risultato in caso di insufficienti consonanti', () => {
expect(CodiceFiscale.nameCode('Luca')).toBe('LCU')
})
})
describe('CodiceFiscale.dateCode', () => {
test('è definito', () => {
expect(CodiceFiscale.dateCode).not.toBe(undefined)
})
test('restituisce il corretto risultato', () => {
expect(CodiceFiscale.dateCode(1, 1, 2000, 'M')).toBe('00A01')
})
})
describe('CodiceFiscale.getCheckCode', () => {
test('è definito', () => {
expect(CodiceFiscale.getCheckCode).not.toBe(undefined)
})
test('restituisce il corretto risultato', () => {
expect(CodiceFiscale.getCheckCode('MRNLCU00A01H501')).toBe('J')
})
})
describe('CodiceFiscale.compute', () => {
test('è definito', () => {
expect(CodiceFiscale.compute).not.toBe(undefined)
})
test('calcola il codice fiscale', () => {
expect(CodiceFiscale.compute({
name: 'Luca',
surname: 'Moreno',
gender: 'M',
day: 1,
month: 1,
year: 2000,
birthplace: 'Roma',
birthplaceProvincia: 'RM'
}))
.toBe('MRNLCU00A01H501J')
})
test("calcola il codice fiscale di persone nate all'estero", () => {
expect(CodiceFiscale.compute({
name: 'Luca',
surname: 'Moreno',
gender: 'M',
day: 1,
month: 1,
year: 2000,
birthplace: 'Albania',
birthplaceProvincia: 'EE'
}))
.toBe('MRNLCU00A01Z100P')
})
test('se il comune non esiste lancia un eccezione', () => {
var comuneInventato = function () {
CodiceFiscale.compute({
name: 'Luca',
surname: 'Moreno',
gender: 'M',
day: 1,
month: 1,
year: 2000,
birthplace: 'Foo',
birthplaceProvincia: 'EE'
})
}
expect(comuneInventato).toThrowError('Location not found')
})
})
describe('CodiceFiscale.findLocationCode', () => {
test('è definito', () => {
expect(CodiceFiscale.findLocationCode).not.toBe(undefined)
})
test('trova il codice del comune', () => {
expect(CodiceFiscale.findLocationCode('Roma', 'RM')).toBe('H501')
})
test('trova il codice di un comune che contiene apostrofi', () => {
expect(CodiceFiscale.findLocationCode("Sant'Angelo Romano", 'RM')).toBe('I284')
})
test('trova il codice di un comune che contiene lettere accentate', () => {
expect(CodiceFiscale.findLocationCode('Riccò del Golfo di Spezia', 'SP')).toBe('H275')
})
test('se la provincia non esiste lancia un eccezione', () => {
var comuneInventato = function () {
CodiceFiscale.findLocationCode('Foo', 'Bar')
}
expect(comuneInventato).toThrowError('Area code not found')
})
test('se il comune non esiste lancia un eccezione', () => {
var comuneInventato = function () {
CodiceFiscale.findLocationCode('Foo', 'RM')
}
expect(comuneInventato).toThrowError('Location not found')
})
})
describe('CodiceFiscale.check', () => {
test('è definito', () => {
expect(CodiceFiscale.check).not.toBe(undefined)
})
test('controlla se il codice fiscale è valido', () => {
expect(CodiceFiscale.check('MRNLCU00A01H501J')).toBe(true)
})
test('controlla che sia composto dal non più 16 valori alfanumerici', () => {
expect(CodiceFiscale.check('MRNLCU00A01H501JK')).toBe(false)
})
test('controlla che sia composto dal almeno 16 valori alfanumerici', () => {
expect(CodiceFiscale.check('MRNLCU00A01H501J3')).toBe(false)
})
test('controlla che il carattere di controllo sia esatto', () => {
expect(CodiceFiscale.check('VNDLDL87D07B963G')).toBe(false)
})
test('funziona anche in caso di omocodie', () => {
expect(CodiceFiscale.check('BNZVCN32S10E57PV')).toBe(true)
expect(CodiceFiscale.check('BNZVCNPNSMLERTPX')).toBe(true)
})
})
describe('CodiceFiscale.getOmocodie', () => {
test('è definito', () => {
expect(CodiceFiscale.getOmocodie).not.toBe(undefined)
})
test('calcola le omocodie dato un codice fiscale', () => {
expect(CodiceFiscale.getOmocodie('BNZVCN32S10E573Z'))
.toEqual(expect.arrayContaining(['BNZVCN32S10E57PV', 'BNZVCNPNSMLERTPX']))
})
})
describe('Calcolo codice fiscale inverso -> metodo .computeInverse', () => {
test('è definito', () => {
expect(CodiceFiscale.computeInverse).not.toBe(undefined)
})
test("se l'input non è una stringa lancia un eccezione", () => {
var notAString = function () {
CodiceFiscale.computeInverse(0)
}
expect(notAString).toThrowError('Provided input is not a valid Codice Fiscale')
})
test("restituisce falso se l'input è stringa formattata male", () => {
var notValid = function () {
CodiceFiscale.computeInverse('BNZVCN32SC0E573Z')
}
expect(notValid).toThrowError('Provided input is not a valid Codice Fiscale')
})
test('restituisce il genere corretto', () => {
expect(CodiceFiscale.computeInverse('MRNLCU00A01H501J').gender).toEqual('M')
})
test('restituisce la città natale corretta', () => {
expect(CodiceFiscale.computeInverse('MRNLCU00A01H501J').birthplace).toEqual('ROMA')
})
test('restituisce la provincia della città natale corretta', () => {
expect(CodiceFiscale.computeInverse('MRNLCU00A01H501J').birthplaceProvincia).toEqual('RM')
})
test('restituisce il giorno di nascita come numero compreso tra 1 e 31', () => {
expect(CodiceFiscale.computeInverse('MRNLCU00A01H501J').day >= 1 && CodiceFiscale.computeInverse('MRNLCU00A01H501J').day <= 31).toBe(true)
})
})

View File

@ -1,49 +0,0 @@
import { Comune } from './comune';
import { birthplaceFields } from './utils';
interface ICodiceFiscaleObject {
name: string;
surname: string;
gender: string;
day: number;
month: number;
year: number;
birthplace: string;
birthplaceProvincia: string;
cf?: string;
}
declare class CodiceFiscale {
day: number;
month: number;
year: number;
readonly cf: string;
readonly nameCode: string;
readonly surnameCode: string;
readonly checkCode: string;
static utils: {
birthplaceFields: typeof birthplaceFields;
};
birthday: Date;
birthplace: Comune;
name: string;
surname: string;
gender: string;
private code;
constructor(data: string | ICodiceFiscaleObject | object);
static getCheckCode(codiceFiscale: string): string;
static findLocationCode(name: string, prov?: string): string;
static computeInverse(codiceFiscale: string): ICodiceFiscaleObject;
static compute(obj: object): string;
static check(codiceFiscale: string): boolean;
static getOmocodie(cf: string): string[];
toString(): string;
toJSON(): ICodiceFiscaleObject;
isValid(): boolean;
omocodie(): string[];
compute(): void;
reverse(): ICodiceFiscaleObject;
private checkGender;
private getSurnameCode;
private getNameCode;
private dateCode;
}
export default CodiceFiscale;

16
types/comune.d.ts vendored
View File

@ -1,16 +0,0 @@
export interface IComuneObject {
nome: string;
prov?: string;
cc?: string;
}
export declare class Comune {
nome: string;
prov: string;
cc: string;
readonly nomeNorm: string;
constructor(nome: string, prov?: string, cc?: string, check?: boolean);
static GetByName(name: string, prov?: string): Comune;
static GetByCC(cc: string): Comune;
private searchByName;
private searchByNameAndProvince;
}

90
types/constants.d.ts vendored
View File

@ -1,90 +0,0 @@
export declare const MONTH_CODES: string[];
export declare const CHECK_CODE_ODD: {
0: number;
1: number;
2: number;
3: number;
4: number;
5: number;
6: number;
7: number;
8: number;
9: number;
A: number;
B: number;
C: number;
D: number;
E: number;
F: number;
G: number;
H: number;
I: number;
J: number;
K: number;
L: number;
M: number;
N: number;
O: number;
P: number;
Q: number;
R: number;
S: number;
T: number;
U: number;
V: number;
W: number;
X: number;
Y: number;
Z: number;
};
export declare const CHECK_CODE_EVEN: {
0: number;
1: number;
2: number;
3: number;
4: number;
5: number;
6: number;
7: number;
8: number;
9: number;
A: number;
B: number;
C: number;
D: number;
E: number;
F: number;
G: number;
H: number;
I: number;
J: number;
K: number;
L: number;
M: number;
N: number;
O: number;
P: number;
Q: number;
R: number;
S: number;
T: number;
U: number;
V: number;
W: number;
X: number;
Y: number;
Z: number;
};
export declare const OMOCODIA_TABLE: {
0: string;
1: string;
2: string;
3: string;
4: string;
5: string;
6: string;
7: string;
8: string;
9: string;
};
export declare const CHECK_CODE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

113
types/geo-data.d.ts vendored
View File

@ -1,113 +0,0 @@
export declare const COMUNI: string[][];
export declare const PROVINCE: {
AG: string;
AL: string;
AN: string;
AO: string;
AP: string;
AQ: string;
AR: string;
AT: string;
AV: string;
BA: string;
BG: string;
BI: string;
BL: string;
BN: string;
BO: string;
BR: string;
BS: string;
BT: string;
BZ: string;
CA: string;
CB: string;
CE: string;
CH: string;
CI: string;
CL: string;
CN: string;
CO: string;
CR: string;
CS: string;
CT: string;
CZ: string;
EN: string;
FC: string;
FE: string;
FG: string;
FI: string;
FM: string;
FR: string;
GE: string;
GO: string;
GR: string;
IM: string;
IS: string;
KR: string;
LC: string;
LE: string;
LI: string;
LO: string;
LT: string;
LU: string;
MB: string;
MC: string;
ME: string;
MI: string;
MN: string;
MO: string;
MS: string;
MT: string;
NA: string;
NO: string;
NU: string;
OG: string;
OR: string;
OT: string;
PA: string;
PC: string;
PD: string;
PE: string;
PG: string;
PI: string;
PN: string;
PO: string;
PR: string;
PT: string;
PU: string;
PV: string;
PZ: string;
RA: string;
RC: string;
RE: string;
RG: string;
RI: string;
RM: string;
RN: string;
RO: string;
SA: string;
SI: string;
SO: string;
SP: string;
SR: string;
SS: string;
SV: string;
TA: string;
TE: string;
TN: string;
TO: string;
TP: string;
TR: string;
TS: string;
TV: string;
UD: string;
VA: string;
VB: string;
VC: string;
VE: string;
VI: string;
VR: string;
VS: string;
VT: string;
VV: string;
};

8
types/utils.d.ts vendored
View File

@ -1,8 +0,0 @@
export declare function normalizeString(str: string): string;
export declare function daysInMonth(m: number, y: number): 29 | 28 | 30 | 31;
export declare function isValidDate(d: number, m: number, y: number): boolean;
export declare function getValidDate(d: number, m: number, y: number): Date;
export declare function extractVowels(str: string): string;
export declare function extractConsonants(str: string): string;
export declare function pad(n: number, size?: number): string;
export declare function birthplaceFields(selector: string): void;

View File

@ -1,48 +0,0 @@
const path = require('path')
const webpack = require('webpack')
const createVariants = require('parallel-webpack').createVariants
function createConfig (options) {
return {
entry: {
'codice.fiscale': './src/codice-fiscale.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'codice.fiscale.' + options.target + '.js',
library: 'CodiceFiscale',
libraryTarget: options.target
},
module: {
rules: [
{
test: /\.json$/,
loader: 'json-loader'
}, {
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['babel-preset-env']
}
}
}
]
},
plugins: [
new webpack
.optimize
.UglifyJsPlugin()
]
}
}
module.exports = createVariants({
target: [
'var',
'commonjs2',
'umd',
'amd'
]
}, createConfig)

2484
yarn.lock

File diff suppressed because it is too large Load Diff