Skip to content

Commit

Permalink
Merge pull request #6 from niels-bosman/feature/fix-forbidden-words-bug
Browse files Browse the repository at this point in the history
Feature / Improve forbidden words functionality
  • Loading branch information
niels-bosman authored Sep 9, 2021
2 parents 0a772eb + 3c7cdde commit ea189b1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
3 changes: 0 additions & 3 deletions src/forbidden-words.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ const forbiddenWords: string[] = [
'TBS',
'SS',
'SD',
'PVV',
'SGP',
'VVD',
];

export default forbiddenWords;
34 changes: 23 additions & 11 deletions src/license-plate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ export default class LicensePlate {
private readonly licensePlate: string;

/**
* Constructs the licence plate with the input
* value in uppercase with dashed removed.
* Constructs the licence plate with the input value as uppercase.
*/
constructor(licensePlate: string) {
this.licensePlate = licensePlate.replace(/-/g, '').toUpperCase();
this.licensePlate = licensePlate.toUpperCase();
}

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

return sidecodes.findIndex((sidecode: RegExp) => licensePlate.match(sidecode)) + 1;
}

/**
* Strips the dashes from the license plate.
*
* @private
* @return {string}
*/
private strippedLicensePlate(): string {
return this.licensePlate.replace(/-/g, '');
}

/**
Expand All @@ -46,29 +57,30 @@ export default class LicensePlate {
* @returns {string} The formatted license plate.
*/
public pretty(): string {
const sidecode = this.sidecode();
const sidecode = this.sidecode();
const licensePlate = this.strippedLicensePlate();

if (sidecode <= 6 && sidecode > 0) {
return `${this.licensePlate.substr(0, 2)}-${this.licensePlate.substr(2, 2)}-${this.licensePlate.substr(4, 2)}`;
return `${licensePlate.substr(0, 2)}-${licensePlate.substr(2, 2)}-${licensePlate.substr(4, 2)}`;
}

if ([7, 9].includes(sidecode)) {
return `${this.licensePlate.substr(0, 2)}-${this.licensePlate.substr(2, 3)}-${this.licensePlate.substr(5, 1)}`;
return `${licensePlate.substr(0, 2)}-${licensePlate.substr(2, 3)}-${licensePlate.substr(5, 1)}`;
}

if ([8, 10].includes(sidecode)) {
return `${this.licensePlate.substr(0, 1)}-${this.licensePlate.substr(1, 3)}-${this.licensePlate.substr(4, 2)}`;
return `${licensePlate.substr(0, 1)}-${licensePlate.substr(1, 3)}-${licensePlate.substr(4, 2)}`;
}

if ([11, 14].includes(sidecode)) {
return `${this.licensePlate.substr(0, 3)}-${this.licensePlate.substr(3, 2)}-${this.licensePlate.substr(5, 1)}`;
return `${licensePlate.substr(0, 3)}-${licensePlate.substr(3, 2)}-${licensePlate.substr(5, 1)}`;
}

if ([12, 13].includes(sidecode)) {
return `${this.licensePlate.substr(0, 1)}-${this.licensePlate.substr(1, 2)}-${this.licensePlate.substr(3, 3)}`;
return `${licensePlate.substr(0, 1)}-${licensePlate.substr(1, 2)}-${licensePlate.substr(3, 3)}`;
}

return this.licensePlate;
return licensePlate;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions test/license-plate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,5 @@ describe('License Plate', () => {
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 ea189b1

Please sign in to comment.