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
|
import type { Faker } from '../..';
import { deprecated } from '../../internal/deprecated';
import type { LiteralUnion } from '../../utils/types';
import type {
AlphaChar,
AlphaNumericChar,
Casing,
NumericChar,
} from '../string';
/**
* Generates random values of different kinds.
*/
export class RandomModule {
constructor(private readonly faker: Faker) {
// Bind `this` so namespaced is working correctly
for (const name of Object.getOwnPropertyNames(RandomModule.prototype)) {
if (name === 'constructor' || typeof this[name] !== 'function') {
continue;
}
this[name] = this[name].bind(this);
}
}
/**
* Returns random word.
*
* @example
* faker.random.word() // 'Seamless'
*
* @since 3.1.0
*/
word(): string {
const wordMethods = [
this.faker.location.cardinalDirection,
this.faker.location.cityName,
this.faker.location.country,
this.faker.location.county,
this.faker.location.direction,
this.faker.location.ordinalDirection,
this.faker.location.state,
this.faker.location.street,
this.faker.color.human,
this.faker.commerce.department,
this.faker.commerce.product,
this.faker.commerce.productAdjective,
this.faker.commerce.productMaterial,
this.faker.commerce.productName,
this.faker.company.bsAdjective,
this.faker.company.bsBuzz,
this.faker.company.bsNoun,
this.faker.company.catchPhraseAdjective,
this.faker.company.catchPhraseDescriptor,
this.faker.company.catchPhraseNoun,
this.faker.finance.accountName,
this.faker.finance.currencyName,
this.faker.finance.transactionType,
this.faker.hacker.abbreviation,
this.faker.hacker.adjective,
this.faker.hacker.ingverb,
this.faker.hacker.noun,
this.faker.hacker.verb,
this.faker.lorem.word,
this.faker.music.genre,
this.faker.person.gender,
this.faker.person.jobArea,
this.faker.person.jobDescriptor,
this.faker.person.jobTitle,
this.faker.person.jobType,
this.faker.person.sex,
() => this.faker.science.chemicalElement().name,
() => this.faker.science.unit().name,
this.faker.vehicle.bicycle,
this.faker.vehicle.color,
this.faker.vehicle.fuel,
this.faker.vehicle.manufacturer,
this.faker.vehicle.type,
this.faker.word.adjective,
this.faker.word.adverb,
this.faker.word.conjunction,
this.faker.word.interjection,
this.faker.word.noun,
this.faker.word.preposition,
this.faker.word.verb,
];
const bannedChars = [
'!',
'#',
'%',
'&',
'*',
')',
'(',
'+',
'=',
'.',
'<',
'>',
'{',
'}',
'[',
']',
':',
';',
"'",
'"',
'_',
'-',
];
let result: string;
do {
// randomly pick from the many faker methods that can generate words
const randomWordMethod = this.faker.helpers.arrayElement(wordMethods);
try {
result = randomWordMethod();
} catch {
// catch missing locale data potentially required by randomWordMethod
continue;
}
} while (!result || bannedChars.some((char) => result.includes(char)));
return this.faker.helpers.arrayElement(result.split(' '));
}
/**
* Returns string with set of random words.
*
* @param count Number of words. Defaults to a random value between `1` and `3`.
*
* @example
* faker.random.words() // 'neural'
* faker.random.words(5) // 'copy Handcrafted bus client-server Point'
*
* @since 3.1.0
*/
words(count?: number): string {
const words: string[] = [];
if (count == null) {
count = this.faker.number.int({ min: 1, max: 3 });
}
for (let i = 0; i < count; i++) {
words.push(this.word());
}
return words.join(' ');
}
/**
* Returns a random locale, that is available in this faker instance.
* You can use the returned locale with `faker.setLocale(result)`.
*
* @example
* faker.random.locale() // 'el'
*
* @since 3.1.0
*/
locale(): string {
return this.faker.helpers.arrayElement(Object.keys(this.faker.locales));
}
/**
* Generating a string consisting of letters in the English alphabet.
*
* @param options Either the number of characters or an options instance. Defaults to `{ count: 1, casing: 'mixed', bannedChars: [] }`.
* @param options.count The number of characters to generate. Defaults to `1`.
* @param options.casing The casing of the characters. Defaults to `'mixed'`.
* @param options.bannedChars An array with characters to exclude. Defaults to `[]`.
*
* @see faker.string.alpha()
*
* @example
* faker.random.alpha() // 'b'
* faker.random.alpha(10) // 'qccrabobaf'
* faker.random.alpha({ count: 5, casing: 'upper', bannedChars: ['A'] }) // 'DTCIC'
*
* @since 5.0.0
*
* @deprecated Use faker.string.alpha() instead.
*/
alpha(
options:
| number
| {
count?: number;
casing?: Casing;
bannedChars?: readonly LiteralUnion<AlphaChar>[] | string;
} = {}
): string {
deprecated({
deprecated: 'faker.random.alpha()',
proposed: 'faker.string.alpha()',
since: '8.0',
until: '9.0',
});
if (typeof options === 'number') {
return this.faker.string.alpha(options);
}
return this.faker.string.alpha({
length: options.count,
casing: options.casing,
exclude: options.bannedChars,
});
}
/**
* Generating a string consisting of alpha characters and digits.
*
* @param count The number of characters and digits to generate. Defaults to `1`.
* @param options The options to use. Defaults to `{ bannedChars: [] }`.
* @param options.casing The casing of the characters. Defaults to `'lower'`.
* @param options.bannedChars An array of characters and digits which should be banned in the generated string. Defaults to `[]`.
*
* @see faker.string.alphanumeric()
*
* @example
* faker.random.alphaNumeric() // '2'
* faker.random.alphaNumeric(5) // '3e5v7'
* faker.random.alphaNumeric(5, { bannedChars: ["a"] }) // 'xszlm'
*
* @since 3.1.0
*
* @deprecated Use faker.string.alphanumeric() instead.
*/
alphaNumeric(
count: number = 1,
options: {
casing?: Casing;
bannedChars?: readonly LiteralUnion<AlphaNumericChar>[] | string;
} = {}
): string {
deprecated({
deprecated: 'faker.random.alphaNumeric()',
proposed: 'faker.string.alphanumeric()',
since: '8.0',
until: '9.0',
});
return this.faker.string.alphanumeric({
length: count,
exclude: options.bannedChars,
casing: options.casing,
});
}
/**
* Generates a given length string of digits.
*
* @param length The number of digits to generate. Defaults to `1`.
* @param options The options to use. Defaults to `{}`.
* @param options.allowLeadingZeros If true, leading zeros will be allowed. Defaults to `false`.
* @param options.bannedDigits An array of digits which should be banned in the generated string. Defaults to `[]`.
*
* @see faker.string.numeric()
*
* @example
* faker.random.numeric() // '2'
* faker.random.numeric(5) // '31507'
* faker.random.numeric(42) // '56434563150765416546479875435481513188548'
* faker.random.numeric(42, { allowLeadingZeros: true }) // '00564846278453876543517840713421451546115'
* faker.random.numeric(6, { bannedDigits: ['0'] }) // '943228'
*
* @since 6.3.0
*
* @deprecated Use faker.string.numeric() instead.
*/
numeric(
length: number = 1,
options: {
allowLeadingZeros?: boolean;
bannedDigits?: readonly LiteralUnion<NumericChar>[] | string;
} = {}
): string {
deprecated({
deprecated: 'faker.random.numeric()',
proposed: 'faker.string.numeric()',
since: '8.0',
until: '9.0',
});
return this.faker.string.numeric({
length,
allowLeadingZeros: options.allowLeadingZeros,
exclude: options.bannedDigits,
});
}
}
|