-
-
Notifications
You must be signed in to change notification settings - Fork 906
/
index.ts
276 lines (261 loc) · 8.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* IATA stands for [International Air Transport Association](https://iata.org).
* It's the trade association for the world's airlines and it is
* responsible for setting standards relating to many aspects of airline
* operations.
*/
import { ModuleBase } from '../../internal/module-base';
export enum Aircraft {
Narrowbody = 'narrowbody',
Regional = 'regional',
Widebody = 'widebody',
}
export type AircraftType = `${Aircraft}`;
export interface Airline {
/**
* The name of the airline (e.g. `'American Airlines'`).
*/
readonly name: string;
/**
* The 2 character IATA code of the airline (e.g. `'AA'`).
*/
readonly iataCode: string;
}
export interface Airplane {
/**
* The name of the airplane (e.g. `'Airbus A321'`).
*/
readonly name: string;
/**
* The IATA code of the airplane (e.g. `'321'`).
*/
readonly iataTypeCode: string;
}
export interface Airport {
/**
* The name of the airport (e.g. `'Dallas Fort Worth International Airport'`).
*/
readonly name: string;
/**
* The IATA code of the airport (e.g. `'DFW'`).
*/
readonly iataCode: string;
}
const numerics = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const visuallySimilarCharacters = ['0', 'O', '1', 'I', 'L'];
const aircraftTypeMaxRows: Record<AircraftType, number> = {
regional: 20,
narrowbody: 35,
widebody: 60,
};
const aircraftTypeSeats: Record<AircraftType, string[]> = {
regional: ['A', 'B', 'C', 'D'],
narrowbody: ['A', 'B', 'C', 'D', 'E', 'F'],
widebody: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K'],
};
/**
* Module to generate airline and airport related data.
*
* ### Overview
*
* Several methods in this module return objects rather than strings. For example, you can use `faker.airline.airport().iataCode` to pick out the specific property you need.
*
* For a random airport, use [`airport()`](https://fakerjs.dev/api/airline.html#airport).
*
* For a random airline, use [`airline()`](https://fakerjs.dev/api/airline.html#airline).
*
* For a dummy booking, a passenger will generally book a flight on a specific [`flightNumber()`](https://fakerjs.dev/api/airline.html#flightnumber), [`airplane()`](https://fakerjs.dev/api/airline.html#airplane), be allocated a [`seat()`](https://fakerjs.dev/api/airline.html#seat), and [`recordLocator()`](https://fakerjs.dev/api/airline.html#recordlocator).
*
* ### Related Modules
*
* - To generate sample passenger data, you can use the methods of the [`faker.person`](https://fakerjs.dev/api/person.html) module.
*/
export class AirlineModule extends ModuleBase {
/**
* Generates a random airport.
*
* @example
* faker.airline.airport() // { name: 'Dallas Fort Worth International Airport', iataCode: 'DFW' }
*
* @since 8.0.0
*/
airport(): Airport {
return this.faker.helpers.arrayElement(
this.faker.definitions.airline.airport
);
}
/**
* Generates a random airline.
*
* @example
* faker.airline.airline() // { name: 'American Airlines', iataCode: 'AA' }
*
* @since 8.0.0
*/
airline(): Airline {
return this.faker.helpers.arrayElement(
this.faker.definitions.airline.airline
);
}
/**
* Generates a random airplane.
*
* @example
* faker.airline.airplane() // { name: 'Airbus A321neo', iataTypeCode: '32Q' }
*
* @since 8.0.0
*/
airplane(): Airplane {
return this.faker.helpers.arrayElement(
this.faker.definitions.airline.airplane
);
}
/**
* Generates a random [record locator](https://en.wikipedia.org/wiki/Record_locator). Record locators
* are used by airlines to identify reservations. They're also known as booking reference numbers,
* locator codes, confirmation codes, or reservation codes.
*
* @param options The options to use.
* @param options.allowNumerics Whether to allow numeric characters. Defaults to `false`.
* @param options.allowVisuallySimilarCharacters Whether to allow visually similar characters such as '1' and 'I'. Defaults to `false`.
*
* @example
* faker.airline.recordLocator() // 'KIFRWE'
* faker.airline.recordLocator({ allowNumerics: true }) // 'E5TYEM'
* faker.airline.recordLocator({ allowVisuallySimilarCharacters: true }) // 'ANZNEI'
* faker.airline.recordLocator({ allowNumerics: true, allowVisuallySimilarCharacters: true }) // '1Z2Z3E'
*
* @since 8.0.0
*/
recordLocator(
options: {
/**
* Whether to allow numeric characters.
*
* @default false
*/
allowNumerics?: boolean;
/**
* Whether to allow visually similar characters such as '1' and 'I'.
*
* @default false
*/
allowVisuallySimilarCharacters?: boolean;
} = {}
): string {
const { allowNumerics = false, allowVisuallySimilarCharacters = false } =
options;
const excludedChars: string[] = [];
if (!allowNumerics) {
excludedChars.push(...numerics);
}
if (!allowVisuallySimilarCharacters) {
excludedChars.push(...visuallySimilarCharacters);
}
return this.faker.string.alphanumeric({
length: 6,
casing: 'upper',
exclude: excludedChars,
});
}
/**
* Generates a random seat.
*
* @param options The options to use.
* @param options.aircraftType The aircraft type. Can be one of `narrowbody`, `regional`, `widebody`. Defaults to `narrowbody`.
*
* @example
* faker.airline.seat() // '22C'
* faker.airline.seat({ aircraftType: 'regional' }) // '7A'
* faker.airline.seat({ aircraftType: 'widebody' }) // '42K'
*
* @since 8.0.0
*/
seat(
options: {
/**
* The aircraft type. Can be one of `narrowbody`, `regional`, `widebody`.
*
* @default 'narrowbody'
*/
aircraftType?: AircraftType;
} = {}
): string {
const { aircraftType = Aircraft.Narrowbody } = options;
const maxRow = aircraftTypeMaxRows[aircraftType];
const allowedSeats = aircraftTypeSeats[aircraftType];
const row = this.faker.number.int({ min: 1, max: maxRow });
const seat = this.faker.helpers.arrayElement(allowedSeats);
return `${row}${seat}`;
}
/**
* Returns a random aircraft type.
*
* @example
* faker.airline.aircraftType() // 'narrowbody'
*
* @since 8.0.0
*/
aircraftType(): AircraftType {
return this.faker.helpers.enumValue(Aircraft);
}
/**
* Returns a random flight number. Flight numbers are always 1 to 4 digits long. Sometimes they are
* used without leading zeros (e.g.: `American Airlines flight 425`) and sometimes with leading
* zeros, often with the airline code prepended (e.g.: `AA0425`).
*
* To generate a flight number prepended with an airline code, combine this function with the
* `airline()` function and use template literals:
* ```
* `${faker.airline.airline().iataCode}${faker.airline.flightNumber({ addLeadingZeros: true })}` // 'AA0798'
* ```
*
* @param options The options to use.
* @param options.length The number or range of digits to generate. Defaults to `{ min: 1, max: 4 }`.
* @param options.addLeadingZeros Whether to pad the flight number up to 4 digits with leading zeros. Defaults to `false`.
*
* @example
* faker.airline.flightNumber() // '2405'
* faker.airline.flightNumber({ addLeadingZeros: true }) // '0249'
* faker.airline.flightNumber({ addLeadingZeros: true, length: 2 }) // '0042'
* faker.airline.flightNumber({ addLeadingZeros: true, length: { min: 2, max: 3 } }) // '0624'
* faker.airline.flightNumber({ length: 3 }) // '425'
* faker.airline.flightNumber({ length: { min: 2, max: 3 } }) // '84'
*
* @since 8.0.0
*/
flightNumber(
options: {
/**
* The number or range of digits to generate.
*
* @default { min: 1, max: 4 }
*/
length?:
| number
| {
/**
* The minimum number of digits to generate.
*/
min: number;
/**
* The maximum number of digits to generate.
*/
max: number;
};
/**
* Whether to pad the flight number up to 4 digits with leading zeros.
*
* @default false
*/
addLeadingZeros?: boolean;
} = {}
): string {
const { length = { min: 1, max: 4 }, addLeadingZeros = false } = options;
const flightNumber = this.faker.string.numeric({
length,
allowLeadingZeros: false,
});
return addLeadingZeros ? flightNumber.padStart(4, '0') : flightNumber;
}
}