@@ -33,6 +33,32 @@ import { VehicleModule } from './modules/vehicle';
33
33
import { WordModule } from './modules/word' ;
34
34
import { mergeLocales } from './utils/merge-locales' ;
35
35
36
+ /**
37
+ * The is Faker's main class containing all modules that can be used to generate data.
38
+ *
39
+ * Please have a look at the individual modules and methods for more information and examples.
40
+ *
41
+ * @example
42
+ * import { faker } from '@faker-js/faker';
43
+ * // const { faker } = require('@faker-js/faker');
44
+ *
45
+ * // faker.seed(1234);
46
+ *
47
+ * faker.person.firstName(); // 'John'
48
+ * faker.person.lastName(); // 'Doe'
49
+ *
50
+ * @example
51
+ * import { Faker, es } from '@faker-js/faker';
52
+ * // const { Faker, es } = require('@faker-js/faker');
53
+ *
54
+ * // create a Faker instance with only es data and no en fallback (=> smaller bundle size)
55
+ * const customFaker = new Faker({ locale: [es] });
56
+ *
57
+ * customFaker.person.firstName(); // 'Javier'
58
+ * customFaker.person.lastName(); // 'Ocampo Corrales'
59
+ *
60
+ * customFaker.music.genre(); // throws Error as this data is not available in `es`
61
+ */
36
62
export class Faker {
37
63
readonly definitions : LocaleDefinition ;
38
64
private _defaultRefDate : ( ) => Date = ( ) => new Date ( ) ;
@@ -50,6 +76,30 @@ export class Faker {
50
76
* @param dateOrSource The function or the static value used to generate the `refDate` date instance.
51
77
* The function must return a new valid `Date` instance for every call.
52
78
* Defaults to `() => new Date()`.
79
+ *
80
+ * @see [Reproducible Results](https://next.fakerjs.dev/guide/usage.html#reproducible-results)
81
+ * @see faker.seed() for reproducible results.
82
+ *
83
+ * @example
84
+ * faker.seed(1234);
85
+ *
86
+ * // Default behavior
87
+ * // faker.setDefaultRefDate();
88
+ * faker.date.past(); // Changes based on the current date/time
89
+ *
90
+ * // Use a static ref date
91
+ * faker.setDefaultRefDate(new Date('2020-01-01'));
92
+ * faker.date.past(); // Reproducible '2019-07-03T08:27:58.118Z'
93
+ *
94
+ * // Use a ref date that changes every time it is used
95
+ * let clock = new Date("2020-01-01").getTime();
96
+ * faker.setDefaultRefDate(() => {
97
+ * clock += 1000; // +1s
98
+ * return new Date(clock);
99
+ * });
100
+ *
101
+ * faker.defaultRefDate() // 2020-01-01T00:00:01Z
102
+ * faker.defaultRefDate() // 2020-01-01T00:00:02Z
53
103
*/
54
104
setDefaultRefDate (
55
105
dateOrSource : string | Date | number | ( ( ) => Date ) = ( ) => new Date ( )
@@ -124,8 +174,26 @@ export class Faker {
124
174
/**
125
175
* Creates a new instance of Faker.
126
176
*
177
+ * In most cases you should use one of the prebuilt Faker instances instead of the constructor, for example `fakerDE`, `fakerFR`, ...
178
+ *
179
+ * You only need to use the constructor if you need custom fallback logic or a custom locale.
180
+ *
181
+ * For more information see our [Localization Guide](https://next.fakerjs.dev/guide/localization.html).
182
+ *
127
183
* @param options The options to use.
128
184
* @param options.locale The locale data to use.
185
+ *
186
+ * @example
187
+ * import { Faker, es } from '@faker-js/faker';
188
+ * // const { Faker, es } = require('@faker-js/faker');
189
+ *
190
+ * // create a Faker instance with only es data and no en fallback (=> smaller bundle size)
191
+ * const customFaker = new Faker({ locale: [es] });
192
+ *
193
+ * customFaker.person.firstName(); // 'Javier'
194
+ * customFaker.person.lastName(); // 'Ocampo Corrales'
195
+ *
196
+ * customFaker.music.genre(); // throws Error as this data is not available in `es`
129
197
*/
130
198
constructor ( options : {
131
199
/**
@@ -139,9 +207,16 @@ export class Faker {
139
207
/**
140
208
* Creates a new instance of Faker.
141
209
*
210
+ * In most cases you should use one of the prebuilt Faker instances instead of the constructor, for example `fakerDE`, `fakerFR`, ...
211
+ *
212
+ * You only need to use the constructor if you need custom fallback logic or a custom locale.
213
+ *
214
+ * For more information see our [Localization Guide](https://next.fakerjs.dev/guide/localization.html).
215
+ *
142
216
* @param options The options to use.
143
217
* @param options.locales The locale data to use.
144
- * @param options.locale The locale data to use.
218
+ * @param options.locale The name of the main locale to use.
219
+ * @param options.localeFallback The name of the fallback locale to use.
145
220
*
146
221
* @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
147
222
*/
@@ -151,12 +226,65 @@ export class Faker {
151
226
localeFallback ?: string ;
152
227
} ) ;
153
228
// This is somehow required for `ConstructorParameters<typeof Faker>[0]` to work
229
+ /**
230
+ * Creates a new instance of Faker.
231
+ *
232
+ * In most cases you should use one of the prebuilt Faker instances instead of the constructor, for example `fakerDE`, `fakerFR`, ...
233
+ *
234
+ * You only need to use the constructor if you need custom fallback logic or a custom locale.
235
+ *
236
+ * For more information see our [Localization Guide](https://next.fakerjs.dev/guide/localization.html).
237
+ *
238
+ * @param options The options to use.
239
+ * @param options.locale The locale data to use or the name of the main locale.
240
+ * @param options.locales The locale data to use.
241
+ * @param options.localeFallback The name of the fallback locale to use.
242
+ *
243
+ * @example
244
+ * import { Faker, es } from '@faker-js/faker';
245
+ * // const { Faker, es } = require('@faker-js/faker');
246
+ *
247
+ * // create a Faker instance with only es data and no en fallback (=> smaller bundle size)
248
+ * const customFaker = new Faker({ locale: [es] });
249
+ *
250
+ * customFaker.person.firstName(); // 'Javier'
251
+ * customFaker.person.lastName(); // 'Ocampo Corrales'
252
+ *
253
+ * customFaker.music.genre(); // throws Error as this data is not available in `es`
254
+ */
154
255
constructor (
155
256
options :
156
- | { locale : LocaleDefinition | LocaleDefinition [ ] }
157
257
| {
258
+ /**
259
+ * The locale data to use for this instance.
260
+ * If an array is provided, the first locale that has a definition for a given property will be used.
261
+ *
262
+ * @see mergeLocales
263
+ */
264
+ locale : LocaleDefinition | LocaleDefinition [ ] ;
265
+ }
266
+ | {
267
+ /**
268
+ * The locale data to use for this instance.
269
+ *
270
+ * @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
271
+ */
158
272
locales : Record < string , LocaleDefinition > ;
273
+ /**
274
+ * The name of the main locale to use.
275
+ *
276
+ * @default 'en'
277
+ *
278
+ * @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
279
+ */
159
280
locale ?: string ;
281
+ /**
282
+ * The name of the fallback locale to use.
283
+ *
284
+ * @default 'en'
285
+ *
286
+ * @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
287
+ */
160
288
localeFallback ?: string ;
161
289
}
162
290
) ;
@@ -221,6 +349,9 @@ export class Faker {
221
349
* @param seed The seed to use. Defaults to a random number.
222
350
* @returns The seed that was set.
223
351
*
352
+ * @see [Reproducible Results](https://next.fakerjs.dev/guide/usage.html#reproducible-results)
353
+ * @see faker.setDefaultRefDate() when generating relative dates.
354
+ *
224
355
* @example
225
356
* // Consistent values for tests:
226
357
* faker.seed(42)
@@ -231,7 +362,6 @@ export class Faker {
231
362
* faker.number.int(10); // 4
232
363
* faker.number.int(10); // 8
233
364
*
234
- * @example
235
365
* // Random but reproducible tests:
236
366
* // Simply log the seed, and if you need to reproduce it, insert the seed here
237
367
* console.log('Running test with seed:', faker.seed());
@@ -253,6 +383,9 @@ export class Faker {
253
383
* @param seedArray The seed array to use.
254
384
* @returns The seed array that was set.
255
385
*
386
+ * @see [Reproducible Results](https://next.fakerjs.dev/guide/usage.html#reproducible-results)
387
+ * @see faker.setDefaultRefDate() when generating relative dates.
388
+ *
256
389
* @example
257
390
* // Consistent values for tests:
258
391
* faker.seed([42, 13, 17])
@@ -263,12 +396,54 @@ export class Faker {
263
396
* faker.number.int(10); // 4
264
397
* faker.number.int(10); // 8
265
398
*
266
- * @example
267
399
* // Random but reproducible tests:
268
400
* // Simply log the seed, and if you need to reproduce it, insert the seed here
269
401
* console.log('Running test with seed:', faker.seed());
270
402
*/
271
403
seed ( seedArray : number [ ] ) : number [ ] ;
404
+ /**
405
+ * Sets the seed or generates a new one.
406
+ *
407
+ * Please note that generated values are dependent on both the seed and the
408
+ * number of calls that have been made since it was set.
409
+ *
410
+ * This method is intended to allow for consistent values in a tests, so you
411
+ * might want to use hardcoded values as the seed.
412
+ *
413
+ * In addition to that it can be used for creating truly random tests
414
+ * (by passing no arguments), that still can be reproduced if needed,
415
+ * by logging the result and explicitly setting it if needed.
416
+ *
417
+ * @param seed The seed or seed array to use.
418
+ * @returns The seed that was set.
419
+ *
420
+ * @see [Reproducible Results](https://next.fakerjs.dev/guide/usage.html#reproducible-results)
421
+ * @see faker.setDefaultRefDate() when generating relative dates.
422
+ *
423
+ * @example
424
+ * // Consistent values for tests (using a number):
425
+ * faker.seed(42)
426
+ * faker.number.int(10); // 4
427
+ * faker.number.int(10); // 8
428
+ *
429
+ * faker.seed(42)
430
+ * faker.number.int(10); // 4
431
+ * faker.number.int(10); // 8
432
+ *
433
+ * // Consistent values for tests (using an array):
434
+ * faker.seed([42, 13, 17])
435
+ * faker.number.int(10); // 4
436
+ * faker.number.int(10); // 8
437
+ *
438
+ * faker.seed([42, 13, 17])
439
+ * faker.number.int(10); // 4
440
+ * faker.number.int(10); // 8
441
+ *
442
+ * // Random but reproducible tests:
443
+ * // Simply log the seed, and if you need to reproduce it, insert the seed here
444
+ * console.log('Running test with seed:', faker.seed());
445
+ */
446
+ seed ( seed ?: number | number [ ] ) : number | number [ ] ;
272
447
seed (
273
448
seed : number | number [ ] = Math . ceil ( Math . random ( ) * Number . MAX_SAFE_INTEGER )
274
449
) : number | number [ ] {
0 commit comments