Skip to content

Commit 4a3a6b9

Browse files
authored
docs: extend Faker class jsdocs (#1960)
1 parent 17f0488 commit 4a3a6b9

File tree

1 file changed

+179
-4
lines changed

1 file changed

+179
-4
lines changed

src/faker.ts

Lines changed: 179 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ import { VehicleModule } from './modules/vehicle';
3333
import { WordModule } from './modules/word';
3434
import { mergeLocales } from './utils/merge-locales';
3535

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+
*/
3662
export class Faker {
3763
readonly definitions: LocaleDefinition;
3864
private _defaultRefDate: () => Date = () => new Date();
@@ -50,6 +76,30 @@ export class Faker {
5076
* @param dateOrSource The function or the static value used to generate the `refDate` date instance.
5177
* The function must return a new valid `Date` instance for every call.
5278
* 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
53103
*/
54104
setDefaultRefDate(
55105
dateOrSource: string | Date | number | (() => Date) = () => new Date()
@@ -124,8 +174,26 @@ export class Faker {
124174
/**
125175
* Creates a new instance of Faker.
126176
*
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+
*
127183
* @param options The options to use.
128184
* @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`
129197
*/
130198
constructor(options: {
131199
/**
@@ -139,9 +207,16 @@ export class Faker {
139207
/**
140208
* Creates a new instance of Faker.
141209
*
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+
*
142216
* @param options The options to use.
143217
* @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.
145220
*
146221
* @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
147222
*/
@@ -151,12 +226,65 @@ export class Faker {
151226
localeFallback?: string;
152227
});
153228
// 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+
*/
154255
constructor(
155256
options:
156-
| { locale: LocaleDefinition | LocaleDefinition[] }
157257
| {
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+
*/
158272
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+
*/
159280
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+
*/
160288
localeFallback?: string;
161289
}
162290
);
@@ -221,6 +349,9 @@ export class Faker {
221349
* @param seed The seed to use. Defaults to a random number.
222350
* @returns The seed that was set.
223351
*
352+
* @see [Reproducible Results](https://next.fakerjs.dev/guide/usage.html#reproducible-results)
353+
* @see faker.setDefaultRefDate() when generating relative dates.
354+
*
224355
* @example
225356
* // Consistent values for tests:
226357
* faker.seed(42)
@@ -231,7 +362,6 @@ export class Faker {
231362
* faker.number.int(10); // 4
232363
* faker.number.int(10); // 8
233364
*
234-
* @example
235365
* // Random but reproducible tests:
236366
* // Simply log the seed, and if you need to reproduce it, insert the seed here
237367
* console.log('Running test with seed:', faker.seed());
@@ -253,6 +383,9 @@ export class Faker {
253383
* @param seedArray The seed array to use.
254384
* @returns The seed array that was set.
255385
*
386+
* @see [Reproducible Results](https://next.fakerjs.dev/guide/usage.html#reproducible-results)
387+
* @see faker.setDefaultRefDate() when generating relative dates.
388+
*
256389
* @example
257390
* // Consistent values for tests:
258391
* faker.seed([42, 13, 17])
@@ -263,12 +396,54 @@ export class Faker {
263396
* faker.number.int(10); // 4
264397
* faker.number.int(10); // 8
265398
*
266-
* @example
267399
* // Random but reproducible tests:
268400
* // Simply log the seed, and if you need to reproduce it, insert the seed here
269401
* console.log('Running test with seed:', faker.seed());
270402
*/
271403
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[];
272447
seed(
273448
seed: number | number[] = Math.ceil(Math.random() * Number.MAX_SAFE_INTEGER)
274449
): number | number[] {

0 commit comments

Comments
 (0)