Skip to content

Commit 48a7af4

Browse files
authored
refactor: simplify module creation (#2485)
1 parent 358572d commit 48a7af4

File tree

27 files changed

+80
-177
lines changed

27 files changed

+80
-177
lines changed

src/internal/module-base.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Faker } from '../faker';
2+
import type { SimpleFaker } from '../simple-faker';
3+
import { bindThisToMemberFunctions } from './bind-this-to-member-functions';
4+
5+
/**
6+
* Base class for all modules that use a `SimpleFaker` instance.
7+
*
8+
* @internal
9+
*/
10+
export abstract class SimpleModuleBase {
11+
constructor(protected readonly faker: SimpleFaker) {
12+
bindThisToMemberFunctions(this);
13+
}
14+
}
15+
16+
/**
17+
* Base class for all modules that use a `Faker` instance.
18+
*
19+
* @internal
20+
*/
21+
export abstract class ModuleBase extends SimpleModuleBase {
22+
constructor(protected readonly faker: Faker) {
23+
super(faker);
24+
}
25+
}

src/modules/airline/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
* responsible for setting standards relating to many aspects of airline
55
* operations.
66
*/
7-
import type { Faker } from '../..';
8-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
7+
import { ModuleBase } from '../../internal/module-base';
98

109
export enum Aircraft {
1110
Narrowbody = 'narrowbody',
@@ -78,11 +77,7 @@ const aircraftTypeSeats: Record<AircraftType, string[]> = {
7877
*
7978
* - To generate sample passenger data, you can use the methods of the [`faker.person`](https://fakerjs.dev/api/person.html) module.
8079
*/
81-
export class AirlineModule {
82-
constructor(private readonly faker: Faker) {
83-
bindThisToMemberFunctions(this);
84-
}
85-
80+
export class AirlineModule extends ModuleBase {
8681
/**
8782
* Generates a random airport.
8883
*

src/modules/animal/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { Faker } from '../..';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
1+
import { ModuleBase } from '../../internal/module-base';
32

43
/**
54
* Module to generate animal related entries.
@@ -12,11 +11,7 @@ import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-fu
1211
*
1312
* All values may be localized.
1413
*/
15-
export class AnimalModule {
16-
constructor(private readonly faker: Faker) {
17-
bindThisToMemberFunctions(this);
18-
}
19-
14+
export class AnimalModule extends ModuleBase {
2015
/**
2116
* Returns a random dog breed.
2217
*

src/modules/color/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { Faker } from '../../faker';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
1+
import { ModuleBase } from '../../internal/module-base';
32

43
/**
54
* Color space names supported by CSS.
@@ -171,11 +170,7 @@ function toColorFormat(
171170
*
172171
* For a hex color like `#ff0000` used in HTML/CSS, use [`rgb()`](https://fakerjs.dev/api/color.html#rgb). There are also methods for other color formats such as [`hsl()`](https://fakerjs.dev/api/color.html#hsl), [`cmyk()`](https://fakerjs.dev/api/color.html#cmyk), [`hwb()`](https://fakerjs.dev/api/color.html#hwb), [`lab()`](https://fakerjs.dev/api/color.html#lab), and [`lch()`](https://fakerjs.dev/api/color.html#lch).
173172
*/
174-
export class ColorModule {
175-
constructor(private readonly faker: Faker) {
176-
bindThisToMemberFunctions(this);
177-
}
178-
173+
export class ColorModule extends ModuleBase {
179174
/**
180175
* Returns a random human-readable color name.
181176
*

src/modules/commerce/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { Faker } from '../../faker';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
31
import { deprecated } from '../../internal/deprecated';
2+
import { ModuleBase } from '../../internal/module-base';
43

54
// Source for official prefixes: https://www.isbn-international.org/range_file_generation
65
const ISBN_LENGTH_RULES: Record<
@@ -86,11 +85,7 @@ const ISBN_LENGTH_RULES: Record<
8685
*
8786
* You can also create a price using [`price()`](https://fakerjs.dev/api/commerce.html#price).
8887
*/
89-
export class CommerceModule {
90-
constructor(private readonly faker: Faker) {
91-
bindThisToMemberFunctions(this);
92-
}
93-
88+
export class CommerceModule extends ModuleBase {
9489
/**
9590
* Returns a department inside a shop.
9691
*

src/modules/company/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { Faker } from '../..';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
31
import { deprecated } from '../../internal/deprecated';
2+
import { ModuleBase } from '../../internal/module-base';
43

54
/**
65
* Module to generate company related entries.
@@ -16,11 +15,7 @@ import { deprecated } from '../../internal/deprecated';
1615
* - For products and commerce, use [`faker.commerce`](https://fakerjs.dev/api/commerce.html).
1716
* - For finance-related entries, use [`faker.finance`](https://fakerjs.dev/api/finance.html).
1817
*/
19-
export class CompanyModule {
20-
constructor(private readonly faker: Faker) {
21-
bindThisToMemberFunctions(this);
22-
}
23-
18+
export class CompanyModule extends ModuleBase {
2419
/**
2520
* Returns an array with possible company name suffixes.
2621
*

src/modules/database/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { Faker } from '../..';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
1+
import { ModuleBase } from '../../internal/module-base';
32

43
/**
54
* Module to generate database related entries.
@@ -10,11 +9,7 @@ import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-fu
109
*
1110
* For the NoSQL database MongoDB, [`mongodbObjectId()`](https://fakerjs.dev/api/database.html#mongodbobjectid) provides a random ID.
1211
*/
13-
export class DatabaseModule {
14-
constructor(private readonly faker: Faker) {
15-
bindThisToMemberFunctions(this);
16-
}
17-
12+
export class DatabaseModule extends ModuleBase {
1813
/**
1914
* Returns a random database column name.
2015
*

src/modules/datatype/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { SimpleFaker } from '../..';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
31
import { deprecated } from '../../internal/deprecated';
2+
import { SimpleModuleBase } from '../../internal/module-base';
43

54
/**
65
* Module to generate various primitive values and data types.
@@ -11,11 +10,7 @@ import { deprecated } from '../../internal/deprecated';
1110
*
1211
* For a simple random true or false value, use [`boolean()`](https://fakerjs.dev/api/datatype.html#boolean).
1312
*/
14-
export class DatatypeModule {
15-
constructor(private readonly faker: SimpleFaker) {
16-
bindThisToMemberFunctions(this);
17-
}
18-
13+
export class DatatypeModule extends SimpleModuleBase {
1914
/**
2015
* Returns a single random number between zero and the given max value or the given range with the specified precision.
2116
* The bounds are inclusive.

src/modules/date/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { Faker, SimpleFaker } from '../..';
1+
import type { Faker } from '../..';
22
import type { DateEntryDefinition } from '../../definitions';
33
import { FakerError } from '../../errors/faker-error';
4-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
54
import { deprecated } from '../../internal/deprecated';
5+
import { SimpleModuleBase } from '../../internal/module-base';
66

77
/**
88
* Converts date passed as a string, number or Date to a Date object.
@@ -26,11 +26,7 @@ function toDate(
2626
/**
2727
* Module to generate dates (without methods requiring localized data).
2828
*/
29-
export class SimpleDateModule {
30-
constructor(protected readonly faker: SimpleFaker) {
31-
bindThisToMemberFunctions(this);
32-
}
33-
29+
export class SimpleDateModule extends SimpleModuleBase {
3430
/**
3531
* Generates a random date that can be either in the past or in the future.
3632
*

src/modules/finance/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { Faker } from '../..';
21
import { FakerError } from '../../errors/faker-error';
3-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
42
import { deprecated } from '../../internal/deprecated';
3+
import { ModuleBase } from '../../internal/module-base';
54
import iban from './iban';
65

76
/**
@@ -37,11 +36,7 @@ export interface Currency {
3736
*
3837
* For blockchain related methods, use: [`bitcoinAddress()`](https://fakerjs.dev/api/finance.html#bitcoinaddress), [`ethereumAddress()`](https://fakerjs.dev/api/finance.html#ethereumaddress) and [`litecoinAddress()`](https://fakerjs.dev/api/finance.html#litecoinaddress).
3938
*/
40-
export class FinanceModule {
41-
constructor(private readonly faker: Faker) {
42-
bindThisToMemberFunctions(this);
43-
}
44-
39+
export class FinanceModule extends ModuleBase {
4540
/**
4641
* Generates a random account number.
4742
*

src/modules/git/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { Faker } from '../..';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
31
import { deprecated } from '../../internal/deprecated';
2+
import { ModuleBase } from '../../internal/module-base';
43

54
const nbsp = '\u00A0';
65

@@ -11,11 +10,7 @@ const nbsp = '\u00A0';
1110
*
1211
* [`commitEntry()`](https://fakerjs.dev/api/git.html#commitentry) generates a random commit entry as printed by `git log`. This includes a commit hash [`commitSha()`](https://fakerjs.dev/api/git.html#commitsha), author, date [`commitDate()`](https://fakerjs.dev/api/git.html#commitdate), and commit message [`commitMessage()`](https://fakerjs.dev/api/git.html#commitmessage). You can also generate a random branch name with [`branch()`](https://fakerjs.dev/api/git.html#branch).
1312
*/
14-
export class GitModule {
15-
constructor(private readonly faker: Faker) {
16-
bindThisToMemberFunctions(this);
17-
}
18-
13+
export class GitModule extends ModuleBase {
1914
/**
2015
* Generates a random branch name.
2116
*

src/modules/hacker/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { Faker } from '../..';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
1+
import { ModuleBase } from '../../internal/module-base';
32

43
/**
54
* Module to generate hacker/IT words and phrases.
@@ -16,11 +15,7 @@ import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-fu
1615
* - [faker.lorem](https://fakerjs.dev/api/lorem.html) uses faux-Latin "lorem ipsum" text.
1716
* - [faker.company](https://fakerjs.dev/api/company.html) includes corporate catchphrases and buzzwords.
1817
*/
19-
export class HackerModule {
20-
constructor(private readonly faker: Faker) {
21-
bindThisToMemberFunctions(this);
22-
}
23-
18+
export class HackerModule extends ModuleBase {
2419
/**
2520
* Returns a random hacker/IT abbreviation.
2621
*

src/modules/helpers/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Faker, SimpleFaker } from '../..';
22
import { FakerError } from '../../errors/faker-error';
3-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
43
import { deprecated } from '../../internal/deprecated';
4+
import { SimpleModuleBase } from '../../internal/module-base';
55
import { luhnCheckValue } from './luhn-check';
66
import type { RecordKey } from './unique';
77
import * as uniqueExec from './unique';
@@ -161,7 +161,7 @@ function legacyRegexpStringParse(
161161
/**
162162
* Module with various helper methods providing basic (seed-dependent) operations useful for implementing faker methods (without methods requiring localized data).
163163
*/
164-
export class SimpleHelpersModule {
164+
export class SimpleHelpersModule extends SimpleModuleBase {
165165
/**
166166
* Global store of unique values.
167167
* This means that faker should *never* return duplicate values across all API methods when using `faker.helpers.unique` without passing `options.store`.
@@ -170,10 +170,6 @@ export class SimpleHelpersModule {
170170
*/
171171
private readonly uniqueStore: Record<RecordKey, RecordKey> = {};
172172

173-
constructor(protected readonly faker: SimpleFaker) {
174-
bindThisToMemberFunctions(this);
175-
}
176-
177173
/**
178174
* Slugifies the given string.
179175
* For that all spaces (` `) are replaced by hyphens (`-`)

src/modules/image/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Faker } from '../..';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
32
import { deprecated } from '../../internal/deprecated';
3+
import { ModuleBase } from '../../internal/module-base';
44
import type { MethodsOf } from '../../utils/types';
55
import { LoremPicsum } from './providers/lorempicsum';
66
import { Placeholder } from './providers/placeholder';
@@ -19,7 +19,7 @@ import { Unsplash } from './providers/unsplash';
1919
*
2020
* This module previously also contained methods for specifically themed images like "fashion" or "food", but these are now deprecated. If you need more control over image type, you can request categorized images using [`urlLoremFlickr()`](https://fakerjs.dev/api/image.html#urlloremflickr), use an image provider directly or provide your own set of placeholder images.
2121
*/
22-
export class ImageModule {
22+
export class ImageModule extends ModuleBase {
2323
/**
2424
* @deprecated Use `faker.image` instead.
2525
*/
@@ -38,8 +38,8 @@ export class ImageModule {
3838
// eslint-disable-next-line deprecation/deprecation
3939
readonly placeholder: Placeholder;
4040

41-
constructor(private readonly faker: Faker) {
42-
bindThisToMemberFunctions(this);
41+
constructor(faker: Faker) {
42+
super(faker);
4343

4444
// eslint-disable-next-line deprecation/deprecation
4545
this.unsplash = new Unsplash(this.faker);

src/modules/internet/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { Faker } from '../..';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
31
import { deprecated } from '../../internal/deprecated';
2+
import { ModuleBase } from '../../internal/module-base';
43
import { charMapping } from './char-mappings';
54
import * as random_ua from './user-agent';
65

@@ -38,11 +37,7 @@ export type HTTPProtocolType = 'http' | 'https';
3837
*
3938
* You also have access to a number of the more technical elements of web requests, such as [`httpMethod`](https://fakerjs.dev/api/internet.html#httpmethod), [`httpStatusCode`](https://fakerjs.dev/api/internet.html#httpstatuscode), [`ip`](https://fakerjs.dev/api/internet.html#ip), [`mac`](https://fakerjs.dev/api/internet.html#mac), [`userAgent`](https://fakerjs.dev/api/internet.html#useragent), and [`port`](https://fakerjs.dev/api/internet.html#port).
4039
*/
41-
export class InternetModule {
42-
constructor(private readonly faker: Faker) {
43-
bindThisToMemberFunctions(this);
44-
}
45-
40+
export class InternetModule extends ModuleBase {
4641
/**
4742
* Returns a random avatar url.
4843
*

src/modules/location/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { Faker } from '../..';
21
import { FakerError } from '../../errors/faker-error';
3-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
42
import { deprecated } from '../../internal/deprecated';
3+
import { ModuleBase } from '../../internal/module-base';
54

65
/**
76
* Module to generate addresses and locations. Prior to Faker 8.0.0, this module was known as `faker.address`.
@@ -14,11 +13,7 @@ import { deprecated } from '../../internal/deprecated';
1413
*
1514
* For a random country, you can use [`country()`](https://fakerjs.dev/api/location.html#country) or [`countryCode()`](https://fakerjs.dev/api/location.html#countrycode).
1615
*/
17-
export class LocationModule {
18-
constructor(private readonly faker: Faker) {
19-
bindThisToMemberFunctions(this);
20-
}
21-
16+
export class LocationModule extends ModuleBase {
2217
/**
2318
* Generates random zip code from specified format. If format is not specified,
2419
* the locale's zip format is used.

src/modules/lorem/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { Faker } from '../..';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
1+
import { ModuleBase } from '../../internal/module-base';
32
import { filterWordListByLength } from '../word/filter-word-list-by-length';
43

54
/**
@@ -13,11 +12,7 @@ import { filterWordListByLength } from '../word/filter-word-list-by-length';
1312
*
1413
* The generic [`text()`](https://fakerjs.dev/api/lorem.html#text) method can be used to generate some text between one sentence and multiple paragraphs, while [`slug()`](https://fakerjs.dev/api/lorem.html#slug) generates an URL-friendly hyphenated string.
1514
*/
16-
export class LoremModule {
17-
constructor(private readonly faker: Faker) {
18-
bindThisToMemberFunctions(this);
19-
}
20-
15+
export class LoremModule extends ModuleBase {
2116
/**
2217
* Generates a word of a specified length.
2318
*

src/modules/music/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { Faker } from '../..';
2-
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
1+
import { ModuleBase } from '../../internal/module-base';
32

43
/**
54
* Module to generate music related entries.
@@ -8,11 +7,7 @@ import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-fu
87
*
98
* Generate a random music genre with [`genre()`](https://fakerjs.dev/api/music.html#genre) or song name with [`songName()`](https://fakerjs.dev/api/music.html#songname). Both may be localized.
109
*/
11-
export class MusicModule {
12-
constructor(private readonly faker: Faker) {
13-
bindThisToMemberFunctions(this);
14-
}
15-
10+
export class MusicModule extends ModuleBase {
1611
/**
1712
* Returns a random music genre.
1813
*

0 commit comments

Comments
 (0)