Skip to content

Commit

Permalink
Merge pull request #4 from Zenedith/numberplates-data
Browse files Browse the repository at this point in the history
Added information about plate and real validators
  • Loading branch information
Zenedith committed Feb 18, 2015
2 parents 3b40371 + 3469d82 commit 00eeddf
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[![NPM](https://nodei.co/npm/pl-numberplates.png?downloads=true&stars=true)](https://nodei.co/npm/pl-numberplates/)

Node JS package to validate PL number plates.
Node JS package to validate Poland number plates.

## Installation

Expand Down
52 changes: 52 additions & 0 deletions lib/formats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//http://pl.wikipedia.org/wiki/Polskie_tablice_rejestracyjne
var formats = [];

formats['2CT5D'] = '[A-Z]{2}[0-9]{5}'; // XY 12345
formats['2CT4D1C'] = '[A-Z]{2}[0-9]{4}[A-Z]{1}'; // XY 1234A
formats['2CT3D2C'] = '[A-Z]{2}[0-9]{3}[A-Z]{2}'; // XY 123AC
formats['2CT1D1C3D'] = '[A-Z]{2}[0-9]{1}[A-Z]{1}[0-9]{3}'; // XY 1A234
formats['2CT1D2C2D'] = '[A-Z]{2}[0-9]{1}[A-Z]{2}[0-9]{2}'; // XY 1AC23

formats['3CT1C3D'] = '[A-Z]{4}[0-9]{3}'; // XYZ A123
formats['3CT2D2C'] = '[A-Z]{3}[0-9]{2}[A-Z]{2}'; // XYZ 12AC
formats['3CT1D1C2D'] = '[A-Z]{3}[0-9]{1}[A-Z]{1}[0-9]{2}'; // XYZ 1A23
formats['3CT2D1C1D'] = '[A-Z]{3}[0-9]{2}[A-Z]{1}[0-9]{1}'; // XYZ 12A3
formats['3CT1D2C1D'] = '[A-Z]{3}[0-9]{1}[A-Z]{2}[0-9]{1}'; // XYZ 1AC2
formats['3CT2C2D'] = '[A-Z]{5}[0-9]{2}'; // XYZ AC12
formats['3CT5D'] = '[A-Z]{3}[0-9]{5}'; // XYZ 12345
formats['3CT4D1C'] = '[A-Z]{3}[0-9]{4}[A-Z]{1}'; // XYZ 1234A
formats['3CT3D2C'] = '[A-Z]{3}[0-9]{3}[A-Z]{2}'; // XYZ 123AC
formats['3CT1C2D1C'] = '[A-Z]{4}[0-9]{2}[A-Z]{1}'; // XYZ A12C
formats['3CT1C1D2C'] = '[A-Z]{4}[0-9]{1}[A-Z]{2}'; // XYZ A1CE

formats['2CT2D1C'] = '[A-Z]{2}[0-9]{2}[A-Z]{1}'; // HIST2-1
formats['2CT3D'] = '[A-Z]{2}[0-9]{3}'; // HIST2-2

formats['3CT1D1C'] = '[A-Z]{3}[0-9]{1}[A-Z]{1}'; // HIST3-1
formats['3CT2D'] = '[A-Z]{3}[0-9]{2}'; // HIST3-2
formats['3CT1C1D'] = '[A-Z]{4}[0-9]{1}'; // HIST3-2

formats['1CT1D3C'] = '[A-Z]{1}[0-9]{1}[A-Z]{3}'; // X0 ABC
formats['1CT1D2C1D'] = '[A-Z]{1}[0-9]{1}[A-Z]{2}[0-9]{1}'; // X0 AB1
formats['1CT1D1C1D'] = '[A-Z]{1}[0-9]{1}[A-Z]{1}[0-9]{2}'; // X0 A12

formats['1CT1D4C'] = '[A-Z]{1}[0-9]{1}[A-Z]{4}'; // X0 ABCD
formats['1CT1D3C1D'] = '[A-Z]{1}[0-9]{1}[A-Z]{3}[0-9]{1}'; // X0 ABC1
formats['1CT1D2C2D'] = '[A-Z]{1}[0-9]{1}[A-Z]{2}[0-9]{2}'; // X0 AB12

formats['1CT1D5C'] = '[A-Z]{1}[0-9]{1}[A-Z]{5}'; // X0 ABCDE
formats['1CT1D4C1D'] = '[A-Z]{1}[0-9]{1}[A-Z]{4}[0-9]{1}'; // X0 ABCD1
formats['1CT1D3C2D'] = '[A-Z]{1}[0-9]{1}[A-Z]{3}[0-9]{2}'; // X0 ABC12


module.exports.getFormat = function (input) {
for (var k in formats) {
if (formats.hasOwnProperty(k)) {
var v = '^' + formats[k] + '$';
if (input.match(String(v))) {
return k;
}
}
}
};

31 changes: 31 additions & 0 deletions lib/model/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var Location = function Location(state, country) {
this.state = state;
this.country = country;
};

var LocationBuilder = function LocationBuilder() {
this.state = null;
this.country = null;

var self = this;

this.withState = function (state) {
self.state = state;
return self;
};

this.withCountry = function (country) {
self.country = country;
return self;
};

this.build = function () {
return new Location(self.state, self.country);
};

};

module.exports = exports = {
Location: Location,
LocationBuilder: LocationBuilder
};
45 changes: 45 additions & 0 deletions lib/model/plate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var Plate = function Plate(plate, prefix, number, location) {
this.plate = plate;
this.prefix = prefix;
this.number = number;
this.location = location;
};

var PlateBuilder = function PlateBuilder() {
this.plate = null;
this.prefix = null;
this.number = null;
this.location = null;

var self = this;

this.withPlate = function (plate) {
self.plate = plate;
return self;
};

this.withPrefix = function (prefix) {
self.prefix = prefix;
return self;
};

this.withNumber = function (number) {
self.number = number;
return self;
};

this.withLocation = function (location) {
self.location = location;
return self;
};

this.build = function () {
return new Plate(self.plate, self.prefix, self.number, self.location);
};

};

module.exports = exports = {
Plate: Plate,
PlateBuilder: PlateBuilder
};
37 changes: 37 additions & 0 deletions lib/plateResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var plateModel = require('../lib/model/plate');
var locationModel = require('../lib/model/location');
var states = require('../lib/states');
var formats = require('../lib/formats');

module.exports.getPlate = function (input) {
var format = formats.getFormat(input);

if (!format) {
return null;
}

var separatorIndex = format.charAt(0);

var prefix = input.substr(0, separatorIndex);
var number = input.substr(separatorIndex);
var plate = prefix + ' ' + number;

var state = states.getState(plate);
var location = null;

if (state) {
new locationModel.LocationBuilder()
.withState(state)
.withCountry('pl-PL')
.build();
}

return new plateModel.PlateBuilder()
.withPlate(plate)
.withPrefix(prefix)
.withNumber(number)
.withLocation(location)
.build();

};

32 changes: 9 additions & 23 deletions lib/polandPlateValidator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var plateResolver = require('./plateResolver');

var prepareInput = function (input) {
input = input || '';
input = input.trim().toUpperCase();
Expand All @@ -8,36 +10,20 @@ var prepareInput = function (input) {
return input.replace(/[\W\s]+/g, '');
};

var regex = function (reg) {
return /^[a-zA-Z]{3}[ -]?\d{2}[a-zA-Z]{2}$/.test(reg) ||
/^[a-zA-Z]{3}[ -]?[a-zA-Z0-9]{4,5}$/.test(reg) ||
/^[a-zA-Z]\d[ -]?[a-zA-Z0-9]{4,5}$/.test(reg);
};

module.exports.validate = function (input, callback) {
var reg = prepareInput(input);
var plate = plateResolver.getPlate(reg);
var err = null;

var err = false; // ERROR CODE :
// false = OK
// 1 = UNKNOWN FORMAT
// 2 = INVALID CHARS
// 3 = ASTERISK IN REG

var data = {
plate: reg,
plateFormat: null
};

var valid = regex(reg);

if (!valid) {
err = 1;
if (!plate) {
err = new Error('Invalid plate number');
}

return callback(err, data);
return callback(err, plate);
};

module.exports.isValid = function (input) {
var reg = prepareInput(input);
return regex(reg);
var plate = plateResolver.getPlate(reg);
return plate ? true : false;
};
29 changes: 29 additions & 0 deletions lib/states.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//http://pl.wikipedia.org/wiki/Polskie_tablice_rejestracyjne
var states = {
B: 'podlaskie',
C: 'kujawsko-pomorskie',
D: 'dolnośląskie',
E: 'łódzkie',
F: 'lubuskie',
G: 'pomorskie',
K: 'małopolskie',
L: 'lubelskie',
N: 'warmińsko-mazurskie',
O: 'opolskie',
P: 'wielkopolskie',
R: 'podkarpackie',
S: 'śląskie',
T: 'świętokrzyskie',
W: 'mazowieckie',
Z: 'zachodniopomorskie'
};

module.exports.getState = function getState(input) {
var state = input.charAt(0);

if (states.hasOwnProperty(state)) {
return states[state];
}

return null;
};
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "pl-numberplates",
"version": "0.5.2",
"description": "Node JS package to validate PL number plates.",
"version": "0.5.3",
"description": "Node JS package to validate Poland number plates.",
"main": "./index.js",
"dependencies": {
},
"devDependencies": {
"chai": "^1.10.0",
"chai": "^2.0.0",
"istanbul": "^0.3.5",
"mocha": "^2.1.0"
},
Expand All @@ -32,7 +32,8 @@
"reg",
"validation",
"numberplate",
"car"
"car",
"poland"
],
"author": {
"name": "Zenedith",
Expand All @@ -44,6 +45,6 @@
"node": ">=0.10.0"
},
"readmeFilename": "README.md",
"_id": "pl-numberplates@0.5.2",
"_from": "pl-numberplates@^0.5.2"
"_id": "pl-numberplates@0.5.3",
"_from": "pl-numberplates@^0.5.3"
}
31 changes: 31 additions & 0 deletions test/model/locationTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var chai = require('chai');
var should = chai.should();
var location = require('../../lib/model/location');

describe('location model test', function () {

it('should create model', function (done) {

var locationModel = new location.Location('wielkopolskie', 'pl-PL');
should.exist(locationModel);
locationModel.state.should.be.equal('wielkopolskie');
locationModel.country.should.be.equal('pl-PL');

done();
});

it('should create model by builder', function (done) {

var locationModel = new location.LocationBuilder()
.withState('wielkopolskie')
.withCountry('pl-PL')
.build();

should.exist(locationModel);
locationModel.state.should.be.equal('wielkopolskie');
locationModel.country.should.be.equal('pl-PL');

done();
});

});
41 changes: 41 additions & 0 deletions test/model/plateTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var chai = require('chai');
var should = chai.should();
var plate = require('../../lib/model/plate');

describe('format model test', function () {

it('should create model', function (done) {

var formatModel = new plate.Plate('plate', 'prefix', 'number', {state: 'state', country: 'country'});
should.exist(formatModel);
formatModel.plate.should.be.equal('plate');
formatModel.prefix.should.be.equal('prefix');
formatModel.number.should.be.equal('number');
should.exist(formatModel.location);
formatModel.location.state.should.be.equal('state');
formatModel.location.country.should.be.equal('country');

done();
});

it('should create model by builder', function (done) {

var formatModel = new plate.PlateBuilder()
.withPlate('plate')
.withPrefix('prefix')
.withNumber('number')
.withLocation({state: 'state', country: 'country'})
.build();

should.exist(formatModel);
formatModel.plate.should.be.equal('plate');
formatModel.prefix.should.be.equal('prefix');
formatModel.number.should.be.equal('number');
should.exist(formatModel.location);
formatModel.location.state.should.be.equal('state');
formatModel.location.country.should.be.equal('country');

done();
});

});
Loading

0 comments on commit 00eeddf

Please sign in to comment.