-
-
Notifications
You must be signed in to change notification settings - Fork 949
/
Copy pathindex.ts
364 lines (328 loc) · 10.1 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import type { Faker } from '../..';
const commonFileTypes = ['video', 'audio', 'image', 'text', 'application'];
const commonMimeTypes = [
'application/pdf',
'audio/mpeg',
'audio/wav',
'image/png',
'image/jpeg',
'image/gif',
'video/mp4',
'video/mpeg',
'text/html',
];
const commonInterfaceTypes = ['en', 'wl', 'ww'] as const;
const commonInterfaceSchemas = {
index: 'o',
slot: 's',
mac: 'x',
pci: 'p',
} as const;
const CRON_DAY_OF_WEEK = [
'SUN',
'MON',
'TUE',
'WED',
'THU',
'FRI',
'SAT',
] as const;
/**
* Generates fake data for many computer systems properties.
*/
export class SystemModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(SystemModule.prototype)) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}
}
/**
* Returns a random file name with extension.
*
* @param options An options object.
* @param options.extensionCount Define how many extensions the file name should have. A negative number will be treated as `0`. Defaults to `1`.
*
* @example
* faker.system.fileName() // 'self_enabling_accountability_toys.kpt'
* faker.system.fileName({ extensionCount: 2 }) // 'bike_table.res.vcs'
*
* @since 3.1.0
*/
fileName(
options: {
/**
* Define how many extensions the file name should have. A negative number will be treated as `0`. Defaults to `1`.
*/
extensionCount?: number;
} = {}
): string {
const { extensionCount = 1 } = options;
const baseName = this.faker.random
.words()
.toLowerCase()
.replace(/\W/g, '_');
if (extensionCount <= 0) {
return baseName;
}
const extensionsStr = Array.from({ length: extensionCount })
.map(() => this.fileExt())
.join('.');
return `${baseName}.${extensionsStr}`;
}
/**
* Returns a random file name with a given extension or a commonly used extension.
*
* @param ext Extension. Empty string is considered to be not set.
* @example
* faker.system.commonFileName() // 'dollar.jpg'
* faker.system.commonFileName('txt') // 'global_borders_wyoming.txt'
*
* @since 3.1.0
*/
commonFileName(ext?: string): string {
const str = this.fileName({ extensionCount: 0 });
return `${str}.${ext || this.commonFileExt()}`;
}
/**
* Returns a mime-type.
*
* @example
* faker.system.mimeType() // 'video/vnd.vivo'
*
* @since 3.1.0
*/
mimeType(): string {
const mimeTypeKeys = Object.keys(this.faker.definitions.system.mimeTypes);
return this.faker.helpers.arrayElement(mimeTypeKeys);
}
/**
* Returns a commonly used file type.
*
* @example
* faker.system.commonFileType() // 'audio'
*
* @since 3.1.0
*/
commonFileType(): string {
return this.faker.helpers.arrayElement(commonFileTypes);
}
/**
* Returns a commonly used file extension.
*
* @example
* faker.system.commonFileExt() // 'gif'
*
* @since 3.1.0
*/
commonFileExt(): string {
return this.fileExt(this.faker.helpers.arrayElement(commonMimeTypes));
}
/**
* Returns a file type.
*
* @example
* faker.system.fileType() // 'message'
*
* @since 3.1.0
*/
fileType(): string {
const typeSet = new Set<string>();
const mimeTypes = this.faker.definitions.system.mimeTypes;
Object.keys(mimeTypes).forEach((m) => {
const type = m.split('/')[0];
typeSet.add(type);
});
const types = Array.from(typeSet);
return this.faker.helpers.arrayElement(types);
}
/**
* Returns a file extension.
*
* @param mimeType Valid [mime-type](https://github.com/jshttp/mime-db/blob/master/db.json)
*
* @example
* faker.system.fileExt() // 'emf'
* faker.system.fileExt('application/json') // 'json'
*
* @since 3.1.0
*/
fileExt(mimeType?: string): string {
if (typeof mimeType === 'string') {
const mimes = this.faker.definitions.system.mimeTypes;
return this.faker.helpers.arrayElement(mimes[mimeType].extensions);
}
const mimeTypes = this.faker.definitions.system.mimeTypes;
const extensionSet = new Set<string>();
Object.keys(mimeTypes).forEach((m) => {
if (mimeTypes[m].extensions instanceof Array) {
mimeTypes[m].extensions.forEach((ext) => {
extensionSet.add(ext);
});
}
});
const extensions = Array.from(extensionSet);
return this.faker.helpers.arrayElement(extensions);
}
/**
* Returns a directory path.
*
* @example
* faker.system.directoryPath() // '/etc/mail'
*
* @since 3.1.0
*/
directoryPath(): string {
const paths = this.faker.definitions.system.directoryPaths;
return this.faker.helpers.arrayElement(paths);
}
/**
* Returns a file path.
*
* @example
* faker.system.filePath() // '/usr/local/src/money.dotx'
*
* @since 3.1.0
*/
// TODO @prisis 2022-01-25: add a parameter to have the possibility to have one or two ext on file.
filePath(): string {
return `${this.directoryPath()}/${this.fileName()}`;
}
/**
* Returns a [semantic version](https://semver.org).
*
* @example
* faker.system.semver() // '1.1.2'
*
* @since 3.1.0
*/
semver(): string {
return [
this.faker.datatype.number(9),
this.faker.datatype.number(9),
this.faker.datatype.number(9),
].join('.');
}
/**
* Returns a random [network interface](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/sec-understanding_the_predictable_network_interface_device_names).
*
* @param options The options to use. Defaults to `{}`.
* @param options.interfaceType The interface type. Can be one of `en`, `wl`, `ww`.
* @param options.interfaceSchema The interface schema. Can be one of `index`, `slot`, `mac`, `pci`.
*
* @example
* faker.system.networkInterface() // 'enp0s3'
* faker.system.networkInterface({ interfaceType: 'wl' }) // 'wlo1'
* faker.system.networkInterface({ interfaceSchema: 'mac' }) // 'enx000c29c00000'
* faker.system.networkInterface({ interfaceType: 'en', interfaceSchema: 'pci' }) // 'enp5s0f1d0'
*
* @since 7.4.0
*/
networkInterface(
options: {
interfaceType?: typeof commonInterfaceTypes[number];
interfaceSchema?: keyof typeof commonInterfaceSchemas;
} = {}
): string {
const {
interfaceType = this.faker.helpers.arrayElement(commonInterfaceTypes),
interfaceSchema = this.faker.helpers.objectKey(commonInterfaceSchemas),
} = options;
let suffix: string;
let prefix = '';
switch (interfaceSchema) {
case 'index':
suffix = this.faker.datatype.number(9).toString();
break;
case 'slot':
suffix = `${this.faker.datatype.number(9)}${
this.faker.helpers.maybe(() => `f${this.faker.datatype.number(9)}`) ??
''
}${
this.faker.helpers.maybe(() => `d${this.faker.datatype.number(9)}`) ??
''
}`;
break;
case 'mac':
suffix = this.faker.internet.mac('');
break;
case 'pci':
prefix =
this.faker.helpers.maybe(() => `P${this.faker.datatype.number(9)}`) ??
'';
suffix = `${this.faker.datatype.number(9)}s${this.faker.datatype.number(
9
)}${
this.faker.helpers.maybe(() => `f${this.faker.datatype.number(9)}`) ??
''
}${
this.faker.helpers.maybe(() => `d${this.faker.datatype.number(9)}`) ??
''
}`;
break;
}
return `${prefix}${interfaceType}${commonInterfaceSchemas[interfaceSchema]}${suffix}`;
}
/**
* Returns a random cron expression.
*
* @param options The optional options to use.
* @param options.includeYear Whether to include a year in the generated expression. Defaults to `false`.
* @param options.includeNonStandard Whether to include a @yearly, @monthly, @daily, etc text labels in the generated expression. Defaults to `false`.
*
* @example
* faker.system.cron() // '45 23 * * 6'
* faker.system.cron({ includeYear: true }) // '45 23 * * 6 2067'
* faker.system.cron({ includeYear: false }) // '45 23 * * 6'
* faker.system.cron({ includeNonStandard: false }) // '45 23 * * 6'
* faker.system.cron({ includeNonStandard: true }) // '@yearly'
*
* @since 7.5.0
*/
cron(
options: {
includeYear?: boolean;
includeNonStandard?: boolean;
} = {}
): string {
const { includeYear = false, includeNonStandard = false } = options;
// create the arrays to hold the available values for each component of the expression
const minutes = [this.faker.datatype.number({ min: 0, max: 59 }), '*'];
const hours = [this.faker.datatype.number({ min: 0, max: 23 }), '*'];
const days = [this.faker.datatype.number({ min: 1, max: 31 }), '*', '?'];
const months = [this.faker.datatype.number({ min: 1, max: 12 }), '*'];
const daysOfWeek = [
this.faker.datatype.number({ min: 0, max: 6 }),
this.faker.helpers.arrayElement(CRON_DAY_OF_WEEK),
'*',
'?',
];
const years = [this.faker.datatype.number({ min: 1970, max: 2099 }), '*'];
const minute = this.faker.helpers.arrayElement(minutes);
const hour = this.faker.helpers.arrayElement(hours);
const day = this.faker.helpers.arrayElement(days);
const month = this.faker.helpers.arrayElement(months);
const dayOfWeek = this.faker.helpers.arrayElement(daysOfWeek);
const year = this.faker.helpers.arrayElement(years);
// create and return the cron expression string
let standardExpression = `${minute} ${hour} ${day} ${month} ${dayOfWeek}`;
if (includeYear) {
standardExpression += ` ${year}`;
}
const nonStandardExpressions = [
'@annually',
'@daily',
'@hourly',
'@monthly',
'@reboot',
'@weekly',
'@yearly',
];
return !includeNonStandard || this.faker.datatype.boolean()
? standardExpression
: this.faker.helpers.arrayElement(nonStandardExpressions);
}
}