mirror of
https://github.com/danog/CodiceFiscaleJS.git
synced 2024-11-30 04:29:11 +01:00
Merge pull request #1 from turbometalskater/feature/nodejs-support
Add nodejs support
This commit is contained in:
commit
e588cc07c7
15
Gulpfile.js
15
Gulpfile.js
@ -1,15 +0,0 @@
|
||||
|
||||
var gulp = require('gulp');
|
||||
var uglify = require('gulp-uglify');
|
||||
var rename = require("gulp-rename");
|
||||
|
||||
gulp.task('compress', function() {
|
||||
return gulp.src('codice.fiscale.js')
|
||||
.pipe(uglify())
|
||||
.pipe(rename('codice.fiscale.min.js'))
|
||||
.pipe(gulp.dest('.'));
|
||||
});
|
||||
|
||||
gulp.task('default', ['compress'], function() {
|
||||
// place code for your default task here
|
||||
});
|
@ -1,9 +1,10 @@
|
||||
{
|
||||
"name": "codice-fiscale-js",
|
||||
"description": "Calcolo del codice fiscale",
|
||||
"main": "codice.fiscale.js",
|
||||
"main": "dist/codice.fiscale.js",
|
||||
"authors": [
|
||||
"Luca Vandro"
|
||||
"Luca Vandro",
|
||||
"Walter Barbagallo"
|
||||
],
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
@ -18,6 +19,7 @@
|
||||
"moduleType": [],
|
||||
"ignore": [
|
||||
"**/.*",
|
||||
"lib",
|
||||
"node_modules",
|
||||
"bower_components",
|
||||
"test",
|
||||
|
32734
codice.fiscale.js
32734
codice.fiscale.js
File diff suppressed because it is too large
Load Diff
1
dist/codice.fiscale.js
vendored
Normal file
1
dist/codice.fiscale.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -15,7 +15,7 @@ module.exports = function(config) {
|
||||
|
||||
// list of files / patterns to load in the browser
|
||||
files: [
|
||||
'codice.fiscale.js',
|
||||
'dist/codice.fiscale.js',
|
||||
'tests/specs.js',
|
||||
],
|
||||
|
||||
|
32612
lib/catastal-codes.json
Normal file
32612
lib/catastal-codes.json
Normal file
File diff suppressed because it is too large
Load Diff
127
lib/codice.fiscale.js
Normal file
127
lib/codice.fiscale.js
Normal file
@ -0,0 +1,127 @@
|
||||
const catastalCodes = require('./catastal-codes.json')
|
||||
|
||||
var CodiceFiscale={}
|
||||
|
||||
CodiceFiscale.compute=function(name,surname,gender,day,month,year,birthplace, birthplace_provincia){
|
||||
|
||||
var code=
|
||||
this.surnameCode(surname)+
|
||||
this.nameCode(name)+
|
||||
this.dateCode(day,month,year,gender)+
|
||||
this.findComuneCode(birthplace, birthplace_provincia);
|
||||
|
||||
code+=this.getCheckCode(code);
|
||||
|
||||
return code;
|
||||
|
||||
}
|
||||
CodiceFiscale.check = function(codiceFiscale){
|
||||
if(codiceFiscale.length !== 16) return false;
|
||||
var expectedCheckCode = codiceFiscale.charAt(15);
|
||||
var cf = codiceFiscale.slice(0,15);
|
||||
|
||||
return CodiceFiscale.getCheckCode(cf) == expectedCheckCode;
|
||||
|
||||
}
|
||||
CodiceFiscale.getCheckCode=function(codiceFiscale){
|
||||
var val=0;
|
||||
for(var i=0; i<15 ;i++){
|
||||
var c=codiceFiscale[i];
|
||||
val+= i%2 ? this.CHECK_CODE_EVEN[c] : this.CHECK_CODE_ODD[c];
|
||||
}
|
||||
val=val%26;
|
||||
return this.CHECK_CODE_CHARS.charAt(val);
|
||||
}
|
||||
|
||||
CodiceFiscale.estraiConsonanti=function(str){
|
||||
return str.replace(/[^BCDFGHJKLMNPQRSTVWXYZ]/gi,'');
|
||||
}
|
||||
|
||||
CodiceFiscale.estraiVocali=function(str){
|
||||
return str.replace(/[^AEIOU]/gi,'');
|
||||
}
|
||||
|
||||
CodiceFiscale.surnameCode=function(surname){
|
||||
var code_surname = this.estraiConsonanti(surname) + this.estraiVocali(surname) + 'XXX';
|
||||
return code_surname.substr(0,3).toUpperCase();
|
||||
}
|
||||
|
||||
CodiceFiscale.nameCode=function(name){
|
||||
var codNome = this.estraiConsonanti(name);
|
||||
if(codNome.length>=4){
|
||||
codNome= codNome.charAt(0) + codNome.charAt(2) + codNome.charAt(3);
|
||||
}else{
|
||||
codNome+= this.estraiVocali(name) + 'XXX';
|
||||
codNome=codNome.substr(0,3);
|
||||
}
|
||||
return codNome.toUpperCase();
|
||||
}
|
||||
|
||||
CodiceFiscale.dateCode=function(gg,mm,aa,gender){
|
||||
var date=new Date();
|
||||
date.setYear(aa);
|
||||
date.setMonth(mm-1);
|
||||
date.setDate(gg);
|
||||
// Padding year
|
||||
var year="0"+date.getFullYear();
|
||||
year=year.substr(year.length-2,2);
|
||||
|
||||
var month=this.MONTH_CODES[date.getMonth()];
|
||||
var day=date.getDate();
|
||||
if(gender.toUpperCase()=='F') day+=40;
|
||||
|
||||
// Padding day
|
||||
day="0"+day;
|
||||
day=day.substr(day.length-2,2);
|
||||
return String(year+month+day);
|
||||
}
|
||||
|
||||
|
||||
CodiceFiscale.findComuneCode=function(birthplace, birthplace_provincia){
|
||||
for (var i = this.CODICI_CATASTALI[birthplace_provincia].length - 1; i >= 0; i--) {
|
||||
var comune = this.CODICI_CATASTALI[birthplace_provincia][i];
|
||||
if(comune[0] == birthplace.trim().toUpperCase()) return comune[1];
|
||||
}
|
||||
throw Error("Comune not found");
|
||||
}
|
||||
|
||||
CodiceFiscale.getOmocodie = function(code){
|
||||
var results = [];
|
||||
var lastOmocode = code = code.slice(0,15);
|
||||
for (var i = code.length - 1; i >= 0; i--) {
|
||||
var char = code[i];
|
||||
if(char.match(/\d/)){
|
||||
lastOmocode = lastOmocode.substr(0,i) + this.OMOCODIA_TABLE[char] + lastOmocode.substr(i+1);
|
||||
results.push(lastOmocode + this.getCheckCode(lastOmocode));
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
|
||||
CodiceFiscale.MONTH_CODES = ['A','B','C','D','E','H','L','M','P','R','S','T'];
|
||||
|
||||
CodiceFiscale.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
|
||||
};
|
||||
|
||||
CodiceFiscale.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
|
||||
};
|
||||
|
||||
CodiceFiscale.OMOCODIA_TABLE = {
|
||||
"0":"L", "1":"M", "2":"N", "3":"P", "4":"Q",
|
||||
"5":"R", "6":"S", "7":"T", "8":"U", "9":"V"}
|
||||
|
||||
CodiceFiscale.CHECK_CODE_CHARS="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
CodiceFiscale.CODICI_CATASTALI = catastalCodes
|
||||
|
||||
module.exports = CodiceFiscale;
|
15
package.json
15
package.json
@ -2,12 +2,11 @@
|
||||
"name": "codice-fiscale-js",
|
||||
"version": "1.0.0",
|
||||
"description": "Calcolo del codice fiscale",
|
||||
"main": "codice.fiscale.js",
|
||||
"main": "lib/codice.fiscale.js",
|
||||
"directories": {
|
||||
"test": "tests"
|
||||
},
|
||||
"dependencies": {
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
"jasmine-core": "^2.4.1",
|
||||
"karma": "^0.13.22",
|
||||
@ -15,12 +14,18 @@
|
||||
"karma-jasmine": "^0.3.7",
|
||||
"karma-phantomjs-launcher": "^1.0.0",
|
||||
"phantomjs-prebuilt": "^2.1.5",
|
||||
"gulp-rename": "^1.2.2",
|
||||
"gulp-uglify": "^1.5.3"
|
||||
"uglify-js": "^2.8.11",
|
||||
"uglifyjs-webpack-plugin": "^0.3.0",
|
||||
"webpack": "^2.2.1"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "webpack",
|
||||
"pretest": "npm run build",
|
||||
"test": "karma start"
|
||||
},
|
||||
"author": "Luca Vandro",
|
||||
"contributors": [
|
||||
"Walter Barbagallo <brb.walter@gmail.com>"
|
||||
],
|
||||
"license": "MIT"
|
||||
}
|
||||
|
@ -43,3 +43,11 @@ Get all the omocodie for a given Codice Fiscale. It returns an array of strings
|
||||
```js
|
||||
var omocodie = CodiceFiscale.getOmocodie("VNDLDL10A01G410Z");
|
||||
```
|
||||
|
||||
|
||||
-------
|
||||
|
||||
## Available npm scripts:
|
||||
|
||||
- `npm run build`: build the bundle into `dist` directory.
|
||||
- `npm run test`: launch the karma tests.
|
||||
|
19
webpack.config.js
Normal file
19
webpack.config.js
Normal file
@ -0,0 +1,19 @@
|
||||
const path = require('path'),
|
||||
UglifyJSPlugin = require('uglifyjs-webpack-plugin');
|
||||
|
||||
|
||||
module.exports = {
|
||||
entry: {
|
||||
'codice.fiscale': './lib/codice.fiscale.js'
|
||||
},
|
||||
output: {
|
||||
// this will publish the module on the window object in order to support the karma tests
|
||||
library: 'CodiceFiscale',
|
||||
libraryTarget: 'window',
|
||||
filename: '[name].js',
|
||||
path: path.resolve(__dirname, 'dist')
|
||||
},
|
||||
plugins: [
|
||||
new UglifyJSPlugin()
|
||||
]
|
||||
};
|
Loading…
Reference in New Issue
Block a user