diff --git a/src/index.ts b/src/index.ts index c7298cde4b1..d864155cf83 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,7 @@ import { Random } from './random'; import { System } from './system'; import { Time } from './time'; import { Unique } from './unique'; +import { Vehicle } from './vehicle'; import { Word } from './word'; export interface FakerOptions { @@ -201,7 +202,7 @@ export class Faker { readonly phone: Phone = new Phone(this); readonly system: System = new System(this); readonly time: Time = new Time(); - readonly vehicle = new (require('./vehicle'))(this); + readonly vehicle: Vehicle = new Vehicle(this); readonly word: Word = new Word(this); constructor(opts: FakerOptions = {}) { diff --git a/src/vehicle.ts b/src/vehicle.ts new file mode 100644 index 00000000000..6badab91c80 --- /dev/null +++ b/src/vehicle.ts @@ -0,0 +1,170 @@ +import type { Faker } from '.'; +import type { Fake } from './fake'; + +let fake: Fake['fake']; + +export class Vehicle { + constructor(private readonly faker: Faker) { + fake = faker.fake; + + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Vehicle.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + + // TODO @Shinigami92 2022-01-13: Find better strategy + // @ts-expect-error + this.vehicle.schema = { + description: 'Generates a random vehicle.', + sampleResults: ['BMW Explorer', 'Ford Camry', 'Lamborghini Ranchero'], + }; + // @ts-expect-error + this.manufacturer.schema = { + description: 'Generates a manufacturer name.', + sampleResults: ['Ford', 'Jeep', 'Tesla'], + }; + // @ts-expect-error + this.model.schema = { + description: 'Generates a vehicle model.', + sampleResults: ['Explorer', 'Camry', 'Ranchero'], + }; + // @ts-expect-error + this.type.schema = { + description: 'Generates a vehicle type.', + sampleResults: ['Coupe', 'Convertable', 'Sedan', 'SUV'], + }; + // @ts-expect-error + this.fuel.schema = { + description: 'Generates a fuel type.', + sampleResults: ['Electric', 'Gasoline', 'Diesel'], + }; + // @ts-expect-error + this.vin.schema = { + description: 'Generates a valid VIN number.', + sampleResults: ['YV1MH682762184654', '3C7WRMBJ2EG208836'], + }; + // @ts-expect-error + this.color.schema = { + description: 'Generates a color', + sampleResults: ['red', 'white', 'black'], + }; + // @ts-expect-error + this.vrm.schema = { + description: 'Generates a vehicle vrm', + sampleResults: ['MF56UPA', 'GL19AAQ', 'SF20TTA'], + }; + // @ts-expect-error + this.bicycle.schema = { + description: 'Generates a type of bicycle', + sampleResults: [ + 'Adventure Road Bicycle', + 'City Bicycle', + 'Recumbent Bicycle', + ], + }; + } + + /** + * vehicle + * + * @method faker.vehicle.vehicle + */ + vehicle(): string { + return fake('{{vehicle.manufacturer}} {{vehicle.model}}'); + } + + /** + * manufacturer + * + * @method faker.vehicle.manufacturer + */ + manufacturer(): string { + return this.faker.random.arrayElement( + this.faker.definitions.vehicle.manufacturer + ); + } + + /** + * model + * + * @method faker.vehicle.model + */ + model(): string { + return this.faker.random.arrayElement(this.faker.definitions.vehicle.model); + } + + /** + * type + * + * @method faker.vehicle.type + */ + type(): string { + return this.faker.random.arrayElement(this.faker.definitions.vehicle.type); + } + + /** + * fuel + * + * @method faker.vehicle.fuel + */ + fuel(): string { + return this.faker.random.arrayElement(this.faker.definitions.vehicle.fuel); + } + + /** + * vin + * + * @method faker.vehicle.vin + */ + vin(): string { + const bannedChars = ['o', 'i', 'q']; + return ( + this.faker.random.alphaNumeric(10, { bannedChars: bannedChars }) + + this.faker.random.alpha({ + count: 1, + upcase: true, + bannedChars: bannedChars, + }) + + this.faker.random.alphaNumeric(1, { bannedChars: bannedChars }) + + this.faker.datatype.number({ min: 10000, max: 100000 }) + ) // return five digit # + .toUpperCase(); + } + + /** + * color + * + * @method faker.vehicle.color + */ + color(): string { + return fake('{{commerce.color}}'); + } + + /** + * vrm + * + * @method faker.vehicle.vrm + */ + vrm(): string { + return ( + this.faker.random.alpha({ count: 2, upcase: true }) + + this.faker.datatype.number({ min: 0, max: 9 }) + + this.faker.datatype.number({ min: 0, max: 9 }) + + this.faker.random.alpha({ count: 3, upcase: true }) + ).toUpperCase(); + } + + /** + * bicycle + * + * @method faker.vehicle.bicycle + */ + bicycle(): string { + return this.faker.random.arrayElement( + this.faker.definitions.vehicle.bicycle_type + ); + } +}