Skip to content

Commit

Permalink
Merge pull request #4 from niels-bosman/feature/banned-words
Browse files Browse the repository at this point in the history
Feature / Banned words
  • Loading branch information
niels-bosman authored Sep 1, 2021
2 parents be2accf + 34eef5e commit cdd66aa
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.3.1",
"version": "1.4.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
22 changes: 22 additions & 0 deletions src/forbidden-words.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* All forbidden words in license plates.
*
* @type {RegExp[]}
*/
const forbiddenWords: string[] = [
'GVD',
'KKK',
'KVT',
'LPF',
'NSB',
'PKK',
'PSV',
'TBS',
'SS',
'SD',
'PVV',
'SGP',
'VVD',
];

export default forbiddenWords;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import LicensePlate from './LicensePlate';
import LicensePlate from './license-plate';

export default LicensePlate;
42 changes: 16 additions & 26 deletions src/LicensePlate.ts → src/license-plate.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
export default class LicensePlate {
/**
* All Dutch sidecodes.
*
* @private
* @readonly
* @type {RegExp[]}
*/
private readonly sidecodes: RegExp[] = [
/^[a-zA-Z]{2}[0-9]{2}[0-9]{2}$/, // [1] => XX-99-99
/^[0-9]{2}[0-9]{2}[a-zA-Z]{2}$/, // [2] => 99-99-XX
/^[0-9]{2}[a-zA-Z]{2}[0-9]{2}$/, // [3] => 99-XX-99
/^[a-zA-Z]{2}[0-9]{2}[a-zA-Z]{2}$/, // [4] => XX-99-XX
/^[a-zA-Z]{2}[a-zA-Z]{2}[0-9]{2}$/, // [5] => XX-XX-99
/^[0-9]{2}[a-zA-Z]{2}[a-zA-Z]{2}$/, // [6] => 99-XX-XX
/^[0-9]{2}[a-zA-Z]{3}[0-9]$/, // [7] => 99-XXX-9
/^[0-9][a-zA-Z]{3}[0-9]{2}$/, // [8] => 9-XXX-99
/^[a-zA-Z]{2}[0-9]{3}[a-zA-Z]$/, // [9] => XX-999-X
/^[a-zA-Z][0-9]{3}[a-zA-Z]{2}$/, // [10] => X-999-XX
/^[a-zA-Z]{3}[0-9]{2}[a-zA-Z]$/, // [11] => XXX-99-X
/^[a-zA-Z][0-9]{2}[a-zA-Z]{3}$/, // [12] => X-99-XXX
/^[0-9][a-zA-Z]{2}[0-9]{3}$/, // [13] => 9-XX-999
/^[0-9]{3}[a-zA-Z]{2}[0-9]$/, // [14] => 999-XX-9
];
import sidecodes from './sidecodes';
import forbiddenWords from './forbidden-words';

export default class LicensePlate {
/**
* License plate.
*
Expand All @@ -47,7 +26,7 @@ export default class LicensePlate {
* @return {boolean} True if sidecode matches, false if none matches.
*/
public valid(): boolean {
return this.sidecode() !== 0;
return this.sidecode() !== 0 && !this.forbidden();
}

/**
Expand All @@ -57,7 +36,7 @@ export default class LicensePlate {
* @return {number} The index of the sidecode, 0 if not found.
*/
public sidecode(): number {
return this.sidecodes.findIndex((sidecode: RegExp) => this.licensePlate.match(sidecode)) + 1;
return sidecodes.findIndex((sidecode: RegExp) => this.licensePlate.match(sidecode)) + 1;
}

/**
Expand Down Expand Up @@ -91,4 +70,15 @@ export default class LicensePlate {

return this.licensePlate;
}

/**
* Checks whether the given license plate includes
* some of the forbidden letter combinations
*
* @return {boolean}
* @private
*/
private forbidden(): boolean {
return forbiddenWords.some((letterCombination: string) => this.licensePlate.includes(letterCombination));
}
}
23 changes: 23 additions & 0 deletions src/sidecodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* All dutch available sidecodes.
*
* @type {RegExp[]}
*/
const sidecodes: RegExp[] = [
/^[a-zA-Z]{2}[0-9]{2}[0-9]{2}$/, // [1] => XX-99-99
/^[0-9]{2}[0-9]{2}[a-zA-Z]{2}$/, // [2] => 99-99-XX
/^[0-9]{2}[a-zA-Z]{2}[0-9]{2}$/, // [3] => 99-XX-99
/^[a-zA-Z]{2}[0-9]{2}[a-zA-Z]{2}$/, // [4] => XX-99-XX
/^[a-zA-Z]{2}[a-zA-Z]{2}[0-9]{2}$/, // [5] => XX-XX-99
/^[0-9]{2}[a-zA-Z]{2}[a-zA-Z]{2}$/, // [6] => 99-XX-XX
/^[0-9]{2}[a-zA-Z]{3}[0-9]$/, // [7] => 99-XXX-9
/^[0-9][a-zA-Z]{3}[0-9]{2}$/, // [8] => 9-XXX-99
/^[a-zA-Z]{2}[0-9]{3}[a-zA-Z]$/, // [9] => XX-999-X
/^[a-zA-Z][0-9]{3}[a-zA-Z]{2}$/, // [10] => X-999-XX
/^[a-zA-Z]{3}[0-9]{2}[a-zA-Z]$/, // [11] => XXX-99-X
/^[a-zA-Z][0-9]{2}[a-zA-Z]{3}$/, // [12] => X-99-XXX
/^[0-9][a-zA-Z]{2}[0-9]{3}$/, // [13] => 9-XX-999
/^[0-9]{3}[a-zA-Z]{2}[0-9]$/, // [14] => 999-XX-9
];

export default sidecodes;
17 changes: 16 additions & 1 deletion test/license-plate.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import LicensePlate from '../src/LicensePlate';
import LicensePlate from '../src/license-plate';

describe('License Plate', () => {
it('Should format sidecode 1 correctly', () => {
Expand Down Expand Up @@ -140,4 +140,19 @@ describe('License Plate', () => {
expect(new LicensePlate('999xx9').sidecode()).toEqual(14);
expect(new LicensePlate('999-xx-9').sidecode()).toEqual(14);
});

it('Should detect all forbidden words', () => {
expect(new LicensePlate('x-99-gvd').valid()).toBeFalsy();
expect(new LicensePlate('x-99-kkk').valid()).toBeFalsy();
expect(new LicensePlate('x-99-kvt').valid()).toBeFalsy();
expect(new LicensePlate('x-99-lpf').valid()).toBeFalsy();
expect(new LicensePlate('x-99-nsb').valid()).toBeFalsy();
expect(new LicensePlate('x-99-pkk').valid()).toBeFalsy();
expect(new LicensePlate('x-99-psv').valid()).toBeFalsy();
expect(new LicensePlate('x-99-sss').valid()).toBeFalsy();
expect(new LicensePlate('x-99-sds').valid()).toBeFalsy();
expect(new LicensePlate('x-99-pvv').valid()).toBeFalsy();
expect(new LicensePlate('x-99-sgp').valid()).toBeFalsy();
expect(new LicensePlate('x-99-vvd').valid()).toBeFalsy();
});
});

0 comments on commit cdd66aa

Please sign in to comment.