Skip to content

Commit

Permalink
Fixed: Forbidden words outside of dashes to be invalid.
Browse files Browse the repository at this point in the history
  • Loading branch information
niels-bosman committed Sep 9, 2021
1 parent 0a772eb commit 4e25748
Showing 1 changed file with 23 additions and 11 deletions.
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

0 comments on commit 4e25748

Please sign in to comment.