From fbe9c024b681ce328e8ad2bafa265cb0d1ce45f2 Mon Sep 17 00:00:00 2001 From: Niels Bosman Date: Wed, 1 Sep 2021 11:06:50 +0200 Subject: [PATCH 1/3] Added banned words functionality and added tests. --- src/forbidden-words.ts | 22 ++++++++++++ src/index.ts | 2 +- src/{LicensePlate.ts => license-plate.ts} | 42 +++++++++-------------- src/sidecodes.ts | 23 +++++++++++++ test/license-plate.test.ts | 17 ++++++++- 5 files changed, 78 insertions(+), 28 deletions(-) create mode 100644 src/forbidden-words.ts rename src/{LicensePlate.ts => license-plate.ts} (63%) create mode 100644 src/sidecodes.ts diff --git a/src/forbidden-words.ts b/src/forbidden-words.ts new file mode 100644 index 0000000..6b30709 --- /dev/null +++ b/src/forbidden-words.ts @@ -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; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 79d1255..38839e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ -import LicensePlate from './LicensePlate'; +import LicensePlate from './license-plate'; export default LicensePlate; diff --git a/src/LicensePlate.ts b/src/license-plate.ts similarity index 63% rename from src/LicensePlate.ts rename to src/license-plate.ts index e13fa06..b5f2af5 100644 --- a/src/LicensePlate.ts +++ b/src/license-plate.ts @@ -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 availableSidecodes from './sidecodes'; +import forbiddenWords from './forbidden-words'; +export default class LicensePlate { /** * License plate. * @@ -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(); } /** @@ -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 availableSidecodes.findIndex((sidecode: RegExp) => this.licensePlate.match(sidecode)) + 1; } /** @@ -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)); + } } diff --git a/src/sidecodes.ts b/src/sidecodes.ts new file mode 100644 index 0000000..90caee4 --- /dev/null +++ b/src/sidecodes.ts @@ -0,0 +1,23 @@ +/** + * All dutch available sidecodes. + * + * @type {RegExp[]} + */ +const availableSidecodes: 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 availableSidecodes; \ No newline at end of file diff --git a/test/license-plate.test.ts b/test/license-plate.test.ts index dccc3aa..5ce74a2 100644 --- a/test/license-plate.test.ts +++ b/test/license-plate.test.ts @@ -1,4 +1,4 @@ -import LicensePlate from '../src/LicensePlate'; +import LicensePlate from '../src/license-plate'; describe('License Plate', () => { it('Should format sidecode 1 correctly', () => { @@ -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(); + }); }); From 410dc72880e0332ba151894b7eeaf4c777b841bd Mon Sep 17 00:00:00 2001 From: Niels Bosman Date: Wed, 1 Sep 2021 11:07:08 +0200 Subject: [PATCH 2/3] Bumped version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2b63a48..a6bccae 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "version": "1.3.1", + "version": "1.4.0", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts", From 34eef5ec20e929f604d51106fbcdabc3fe7117e0 Mon Sep 17 00:00:00 2001 From: Niels Bosman Date: Wed, 1 Sep 2021 11:25:28 +0200 Subject: [PATCH 3/3] Changed sidecode import name --- src/license-plate.ts | 4 ++-- src/sidecodes.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/license-plate.ts b/src/license-plate.ts index b5f2af5..872dc45 100644 --- a/src/license-plate.ts +++ b/src/license-plate.ts @@ -1,4 +1,4 @@ -import availableSidecodes from './sidecodes'; +import sidecodes from './sidecodes'; import forbiddenWords from './forbidden-words'; export default class LicensePlate { @@ -36,7 +36,7 @@ export default class LicensePlate { * @return {number} The index of the sidecode, 0 if not found. */ public sidecode(): number { - return availableSidecodes.findIndex((sidecode: RegExp) => this.licensePlate.match(sidecode)) + 1; + return sidecodes.findIndex((sidecode: RegExp) => this.licensePlate.match(sidecode)) + 1; } /** diff --git a/src/sidecodes.ts b/src/sidecodes.ts index 90caee4..1b16d6e 100644 --- a/src/sidecodes.ts +++ b/src/sidecodes.ts @@ -3,7 +3,7 @@ * * @type {RegExp[]} */ -const availableSidecodes: 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 @@ -20,4 +20,4 @@ const availableSidecodes: RegExp[] = [ /^[0-9]{3}[a-zA-Z]{2}[0-9]$/, // [14] => 999-XX-9 ]; -export default availableSidecodes; \ No newline at end of file +export default sidecodes; \ No newline at end of file