Last active
August 23, 2017 11:09
-
-
Save JoostKiens/2ccfbe151d8379de4909 to your computer and use it in GitHub Desktop.
Dutch License Plate Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function () { | |
'use strict'; | |
function getSidecodeByLicensePlate (licensePlate) { | |
var sidecodes = []; | |
var i = 0; | |
licensePlate = licensePlate.replace(/-/g, '').toUpperCase(); | |
// See http://nl.wikipedia.org/wiki/Nederlands_kenteken#Alle_sidecodes | |
sidecodes[0] = /^[a-zA-Z]{2}[\d]{2}[\d]{2}$/; // 1 XX-99-99 | |
sidecodes[1] = /^[\d]{2}[\d]{2}[a-zA-Z]{2}$/; // 2 99-99-XX | |
sidecodes[2] = /^[\d]{2}[a-zA-Z]{2}[\d]{2}$/; // 3 99-XX-99 | |
sidecodes[3] = /^[a-zA-Z]{2}[\d]{2}[a-zA-Z]{2}$/; // 4 XX-99-XX | |
sidecodes[4] = /^[a-zA-Z]{2}[a-zA-Z]{2}[\d]{2}$/; // 5 XX-XX-99 | |
sidecodes[5] = /^[\d]{2}[a-zA-Z]{2}[a-zA-Z]{2}$/; // 6 99-XX-XX | |
sidecodes[6] = /^[\d]{2}[a-zA-Z]{3}[\d]{1}$/; // 7 99-XXX-9 | |
sidecodes[7] = /^[\d]{1}[a-zA-Z]{3}[\d]{2}$/; // 8 9-XXX-99 | |
sidecodes[8] = /^[a-zA-Z]{2}[\d]{3}[a-zA-Z]{1}$/; // 9 XX-999-X | |
sidecodes[9] = /^[a-zA-Z]{1}[\d]{3}[a-zA-Z]{2}$/; // 10 X-999-XX | |
sidecodes[10] = /^[a-zA-Z]{3}[\d]{2}[a-zA-Z]{1}$/; // 11 XXX-99-X | |
sidecodes[11] = /^[a-zA-Z]{1}[\d]{2}[a-zA-Z]{3}$/; // 12 X-99-XXX | |
sidecodes[12] = /^[\d]{1}[a-zA-Z]{2}[\d]{3}$/; // 13 9-XX-999 | |
sidecodes[13] = /^[\d]{3}[a-zA-Z]{2}[\d]{1}$/; // 14 999-XX-9 | |
for (i = 0; i < sidecodes.length; i++) { | |
if (licensePlate.match(sidecodes[i])) { | |
return i + 1; | |
} | |
} | |
// Licenseplates for diplomats | |
if (licensePlate.match(/^CD[ABFJNST][0-9]{1,3}$/)) { //for example: CDB1 of CDJ45 | |
return 'CD'; | |
} | |
return false; | |
} | |
function formatLicensePlate(licensePlate, sidecode) { | |
/* jshint maxcomplexity:15 */ | |
licensePlate = licensePlate.replace(/-/g, '').toUpperCase(); | |
// See http://nl.wikipedia.org/wiki/Nederlands_kenteken#Alle_sidecodes | |
var positions = {}; | |
positions['1'] = positions['2'] = positions['3'] = positions['4'] = positions['5'] = positions['6'] = [2, 4]; | |
positions['7'] = positions['9'] = [2, 5]; | |
positions['8'] = positions['10'] = [1, 4]; | |
positions['11'] = positions['14'] = [3, 5]; | |
positions['12'] = positions['13'] = [1, 3]; | |
var dashPositions = positions[sidecode]; | |
addDashes(licensePlate, dashPositions[0], dashPositions[1]); | |
} | |
function addDashes(licensePlate, position1, position2) { | |
return [ | |
licensePlate.substring(0, position1), | |
licensePlate.substring(position1, position2), | |
licensePlate.substr(position2) | |
].join('-'); | |
} | |
function validateLicensePlate(licensePlate) { | |
return !!getSidecodeByLicensePlate(licensePlate); | |
} | |
return { | |
validate: validateLicensePlate, | |
format: formatLicensePlate, | |
getSideCode: getSidecodeByLicensePlate | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment