Skip to content

Commit 661f3b4

Browse files
Shinigami92damienwebdev
authored andcommitted
feat: migrate vehicle (#130)
1 parent 0205183 commit 661f3b4

File tree

2 files changed

+172
-1
lines changed

2 files changed

+172
-1
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { Random } from './random';
2121
import { System } from './system';
2222
import { Time } from './time';
2323
import { Unique } from './unique';
24+
import { Vehicle } from './vehicle';
2425
import { Word } from './word';
2526

2627
export interface FakerOptions {
@@ -201,7 +202,7 @@ export class Faker {
201202
readonly phone: Phone = new Phone(this);
202203
readonly system: System = new System(this);
203204
readonly time: Time = new Time();
204-
readonly vehicle = new (require('./vehicle'))(this);
205+
readonly vehicle: Vehicle = new Vehicle(this);
205206
readonly word: Word = new Word(this);
206207

207208
constructor(opts: FakerOptions = {}) {

src/vehicle.ts

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import type { Faker } from '.';
2+
import type { Fake } from './fake';
3+
4+
let fake: Fake['fake'];
5+
6+
export class Vehicle {
7+
constructor(private readonly faker: Faker) {
8+
fake = faker.fake;
9+
10+
// Bind `this` so namespaced is working correctly
11+
for (const name of Object.getOwnPropertyNames(Vehicle.prototype)) {
12+
if (name === 'constructor' || typeof this[name] !== 'function') {
13+
continue;
14+
}
15+
this[name] = this[name].bind(this);
16+
}
17+
18+
// TODO @Shinigami92 2022-01-13: Find better strategy
19+
// @ts-expect-error
20+
this.vehicle.schema = {
21+
description: 'Generates a random vehicle.',
22+
sampleResults: ['BMW Explorer', 'Ford Camry', 'Lamborghini Ranchero'],
23+
};
24+
// @ts-expect-error
25+
this.manufacturer.schema = {
26+
description: 'Generates a manufacturer name.',
27+
sampleResults: ['Ford', 'Jeep', 'Tesla'],
28+
};
29+
// @ts-expect-error
30+
this.model.schema = {
31+
description: 'Generates a vehicle model.',
32+
sampleResults: ['Explorer', 'Camry', 'Ranchero'],
33+
};
34+
// @ts-expect-error
35+
this.type.schema = {
36+
description: 'Generates a vehicle type.',
37+
sampleResults: ['Coupe', 'Convertable', 'Sedan', 'SUV'],
38+
};
39+
// @ts-expect-error
40+
this.fuel.schema = {
41+
description: 'Generates a fuel type.',
42+
sampleResults: ['Electric', 'Gasoline', 'Diesel'],
43+
};
44+
// @ts-expect-error
45+
this.vin.schema = {
46+
description: 'Generates a valid VIN number.',
47+
sampleResults: ['YV1MH682762184654', '3C7WRMBJ2EG208836'],
48+
};
49+
// @ts-expect-error
50+
this.color.schema = {
51+
description: 'Generates a color',
52+
sampleResults: ['red', 'white', 'black'],
53+
};
54+
// @ts-expect-error
55+
this.vrm.schema = {
56+
description: 'Generates a vehicle vrm',
57+
sampleResults: ['MF56UPA', 'GL19AAQ', 'SF20TTA'],
58+
};
59+
// @ts-expect-error
60+
this.bicycle.schema = {
61+
description: 'Generates a type of bicycle',
62+
sampleResults: [
63+
'Adventure Road Bicycle',
64+
'City Bicycle',
65+
'Recumbent Bicycle',
66+
],
67+
};
68+
}
69+
70+
/**
71+
* vehicle
72+
*
73+
* @method faker.vehicle.vehicle
74+
*/
75+
vehicle(): string {
76+
return fake('{{vehicle.manufacturer}} {{vehicle.model}}');
77+
}
78+
79+
/**
80+
* manufacturer
81+
*
82+
* @method faker.vehicle.manufacturer
83+
*/
84+
manufacturer(): string {
85+
return this.faker.random.arrayElement(
86+
this.faker.definitions.vehicle.manufacturer
87+
);
88+
}
89+
90+
/**
91+
* model
92+
*
93+
* @method faker.vehicle.model
94+
*/
95+
model(): string {
96+
return this.faker.random.arrayElement(this.faker.definitions.vehicle.model);
97+
}
98+
99+
/**
100+
* type
101+
*
102+
* @method faker.vehicle.type
103+
*/
104+
type(): string {
105+
return this.faker.random.arrayElement(this.faker.definitions.vehicle.type);
106+
}
107+
108+
/**
109+
* fuel
110+
*
111+
* @method faker.vehicle.fuel
112+
*/
113+
fuel(): string {
114+
return this.faker.random.arrayElement(this.faker.definitions.vehicle.fuel);
115+
}
116+
117+
/**
118+
* vin
119+
*
120+
* @method faker.vehicle.vin
121+
*/
122+
vin(): string {
123+
const bannedChars = ['o', 'i', 'q'];
124+
return (
125+
this.faker.random.alphaNumeric(10, { bannedChars: bannedChars }) +
126+
this.faker.random.alpha({
127+
count: 1,
128+
upcase: true,
129+
bannedChars: bannedChars,
130+
}) +
131+
this.faker.random.alphaNumeric(1, { bannedChars: bannedChars }) +
132+
this.faker.datatype.number({ min: 10000, max: 100000 })
133+
) // return five digit #
134+
.toUpperCase();
135+
}
136+
137+
/**
138+
* color
139+
*
140+
* @method faker.vehicle.color
141+
*/
142+
color(): string {
143+
return fake('{{commerce.color}}');
144+
}
145+
146+
/**
147+
* vrm
148+
*
149+
* @method faker.vehicle.vrm
150+
*/
151+
vrm(): string {
152+
return (
153+
this.faker.random.alpha({ count: 2, upcase: true }) +
154+
this.faker.datatype.number({ min: 0, max: 9 }) +
155+
this.faker.datatype.number({ min: 0, max: 9 }) +
156+
this.faker.random.alpha({ count: 3, upcase: true })
157+
).toUpperCase();
158+
}
159+
160+
/**
161+
* bicycle
162+
*
163+
* @method faker.vehicle.bicycle
164+
*/
165+
bicycle(): string {
166+
return this.faker.random.arrayElement(
167+
this.faker.definitions.vehicle.bicycle_type
168+
);
169+
}
170+
}

0 commit comments

Comments
 (0)