Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify module creation #2485

Merged
merged 12 commits into from
Nov 6, 2023
9 changes: 2 additions & 7 deletions src/modules/airline/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* responsible for setting standards relating to many aspects of airline
* operations.
*/
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { ModuleBase } from '../module-base';

export enum Aircraft {
Narrowbody = 'narrowbody',
Expand Down Expand Up @@ -78,11 +77,7 @@ const aircraftTypeSeats: Record<AircraftType, string[]> = {
*
* - To generate sample passenger data, you can use the methods of the [`faker.person`](https://fakerjs.dev/api/person.html) module.
*/
export class AirlineModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class AirlineModule extends ModuleBase {
/**
* Generates a random airport.
*
Expand Down
9 changes: 2 additions & 7 deletions src/modules/animal/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { ModuleBase } from '../module-base';

/**
* Module to generate animal related entries.
Expand All @@ -12,11 +11,7 @@ import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-fu
*
* All values may be localized.
*/
export class AnimalModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class AnimalModule extends ModuleBase {
/**
* Returns a random dog breed.
*
Expand Down
9 changes: 2 additions & 7 deletions src/modules/color/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Faker } from '../../faker';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { ModuleBase } from '../module-base';

/**
* Color space names supported by CSS.
Expand Down Expand Up @@ -171,11 +170,7 @@ function toColorFormat(
*
* 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).
*/
export class ColorModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class ColorModule extends ModuleBase {
/**
* Returns a random human-readable color name.
*
Expand Down
9 changes: 2 additions & 7 deletions src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Faker } from '../../faker';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../module-base';

// Source for official prefixes: https://www.isbn-international.org/range_file_generation
const ISBN_LENGTH_RULES: Record<
Expand Down Expand Up @@ -86,11 +85,7 @@ const ISBN_LENGTH_RULES: Record<
*
* You can also create a price using [`price()`](https://fakerjs.dev/api/commerce.html#price).
*/
export class CommerceModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class CommerceModule extends ModuleBase {
/**
* Returns a department inside a shop.
*
Expand Down
9 changes: 2 additions & 7 deletions src/modules/company/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../module-base';

/**
* Module to generate company related entries.
Expand All @@ -16,11 +15,7 @@ import { deprecated } from '../../internal/deprecated';
* - For products and commerce, use [`faker.commerce`](https://fakerjs.dev/api/commerce.html).
* - For finance-related entries, use [`faker.finance`](https://fakerjs.dev/api/finance.html).
*/
export class CompanyModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class CompanyModule extends ModuleBase {
/**
* Returns an array with possible company name suffixes.
*
Expand Down
9 changes: 2 additions & 7 deletions src/modules/database/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { ModuleBase } from '../module-base';

/**
* Module to generate database related entries.
Expand All @@ -10,11 +9,7 @@ import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-fu
*
* For the NoSQL database MongoDB, [`mongodbObjectId()`](https://fakerjs.dev/api/database.html#mongodbobjectid) provides a random ID.
*/
export class DatabaseModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class DatabaseModule extends ModuleBase {
/**
* Returns a random database column name.
*
Expand Down
9 changes: 2 additions & 7 deletions src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { SimpleFaker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { SimpleModuleBase } from '../module-base';

/**
* Module to generate various primitive values and data types.
Expand All @@ -11,11 +10,7 @@ import { deprecated } from '../../internal/deprecated';
*
* For a simple random true or false value, use [`boolean()`](https://fakerjs.dev/api/datatype.html#boolean).
*/
export class DatatypeModule {
constructor(private readonly faker: SimpleFaker) {
bindThisToMemberFunctions(this);
}

export class DatatypeModule extends SimpleModuleBase {
/**
* Returns a single random number between zero and the given max value or the given range with the specified precision.
* The bounds are inclusive.
Expand Down
10 changes: 3 additions & 7 deletions src/modules/date/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Faker, SimpleFaker } from '../..';
import type { Faker } from '../..';
import type { DateEntryDefinition } from '../../definitions';
import { FakerError } from '../../errors/faker-error';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { SimpleModuleBase } from '../module-base';

/**
* Converts date passed as a string, number or Date to a Date object.
Expand All @@ -26,11 +26,7 @@ function toDate(
/**
* Module to generate dates (without methods requiring localized data).
*/
export class SimpleDateModule {
constructor(protected readonly faker: SimpleFaker) {
bindThisToMemberFunctions(this);
}

export class SimpleDateModule extends SimpleModuleBase {
/**
* Generates a random date that can be either in the past or in the future.
*
Expand Down
9 changes: 2 additions & 7 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../module-base';
import iban from './iban';

/**
Expand Down Expand Up @@ -37,11 +36,7 @@ export interface Currency {
*
* 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).
*/
export class FinanceModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class FinanceModule extends ModuleBase {
/**
* Generates a random account number.
*
Expand Down
9 changes: 2 additions & 7 deletions src/modules/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../module-base';

const nbsp = '\u00a0';

Expand All @@ -11,11 +10,7 @@ const nbsp = '\u00a0';
*
* [`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).
*/
export class GitModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class GitModule extends ModuleBase {
/**
* Generates a random branch name.
*
Expand Down
9 changes: 2 additions & 7 deletions src/modules/hacker/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { ModuleBase } from '../module-base';

/**
* Module to generate hacker/IT words and phrases.
Expand All @@ -16,11 +15,7 @@ import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-fu
* - [faker.lorem](https://fakerjs.dev/api/lorem.html) uses faux-Latin "lorem ipsum" text.
* - [faker.company](https://fakerjs.dev/api/company.html) includes corporate catchphrases and buzzwords.
*/
export class HackerModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class HackerModule extends ModuleBase {
/**
* Returns a random hacker/IT abbreviation.
*
Expand Down
8 changes: 2 additions & 6 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Faker, SimpleFaker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { SimpleModuleBase } from '../module-base';
import { luhnCheckValue } from './luhn-check';
import type { RecordKey } from './unique';
import * as uniqueExec from './unique';
Expand Down Expand Up @@ -161,7 +161,7 @@ function legacyRegexpStringParse(
/**
* Module with various helper methods providing basic (seed-dependent) operations useful for implementing faker methods (without methods requiring localized data).
*/
export class SimpleHelpersModule {
export class SimpleHelpersModule extends SimpleModuleBase {
/**
* Global store of unique values.
* This means that faker should *never* return duplicate values across all API methods when using `faker.helpers.unique` without passing `options.store`.
Expand All @@ -170,10 +170,6 @@ export class SimpleHelpersModule {
*/
private readonly uniqueStore: Record<RecordKey, RecordKey> = {};

constructor(protected readonly faker: SimpleFaker) {
bindThisToMemberFunctions(this);
}

/**
* Slugifies the given string.
* For that all spaces (` `) are replaced by hyphens (`-`)
Expand Down
8 changes: 4 additions & 4 deletions src/modules/image/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import type { MethodsOf } from '../../utils/types';
import { ModuleBase } from '../module-base';
import { LoremPicsum } from './providers/lorempicsum';
import { Placeholder } from './providers/placeholder';
import { Unsplash } from './providers/unsplash';
Expand All @@ -19,7 +19,7 @@ import { Unsplash } from './providers/unsplash';
*
* 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.
*/
export class ImageModule {
export class ImageModule extends ModuleBase {
/**
* @deprecated Use `faker.image` instead.
*/
Expand All @@ -38,8 +38,8 @@ export class ImageModule {
// eslint-disable-next-line deprecation/deprecation
readonly placeholder: Placeholder;

constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
constructor(faker: Faker) {
super(faker);

// eslint-disable-next-line deprecation/deprecation
this.unsplash = new Unsplash(this.faker);
Expand Down
9 changes: 2 additions & 7 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../module-base';
import { charMapping } from './char-mappings';
import * as random_ua from './user-agent';

Expand Down Expand Up @@ -38,11 +37,7 @@ export type HTTPProtocolType = 'http' | 'https';
*
* 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).
*/
export class InternetModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class InternetModule extends ModuleBase {
/**
* Returns a random avatar url.
*
Expand Down
9 changes: 2 additions & 7 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Faker } from '../..';
import { FakerError } from '../../errors/faker-error';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../module-base';

/**
* Module to generate addresses and locations. Prior to Faker 8.0.0, this module was known as `faker.address`.
Expand All @@ -14,11 +13,7 @@ import { deprecated } from '../../internal/deprecated';
*
* For a random country, you can use [`country()`](https://fakerjs.dev/api/location.html#country) or [`countryCode()`](https://fakerjs.dev/api/location.html#countrycode).
*/
export class LocationModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class LocationModule extends ModuleBase {
/**
* Generates random zip code from specified format. If format is not specified,
* the locale's zip format is used.
Expand Down
9 changes: 2 additions & 7 deletions src/modules/lorem/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { ModuleBase } from '../module-base';
import { filterWordListByLength } from '../word/filterWordListByLength';

/**
Expand All @@ -13,11 +12,7 @@ import { filterWordListByLength } from '../word/filterWordListByLength';
*
* 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.
*/
export class LoremModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class LoremModule extends ModuleBase {
/**
* Generates a word of a specified length.
*
Expand Down
25 changes: 25 additions & 0 deletions src/modules/module-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Faker } from '../faker';
import { bindThisToMemberFunctions } from '../internal/bind-this-to-member-functions';
import type { SimpleFaker } from '../simple-faker';

/**
* Base class for all modules that use a `SimpleFaker` instance.
*
* @internal
*/
export abstract class SimpleModuleBase {
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
constructor(protected readonly faker: SimpleFaker) {
bindThisToMemberFunctions(this);
}
}

/**
* Base class for all modules that use a `Faker` instance.
*
* @internal
*/
export abstract class ModuleBase extends SimpleModuleBase {
constructor(protected readonly faker: Faker) {
super(faker);
}
}
9 changes: 2 additions & 7 deletions src/modules/music/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Faker } from '../..';
import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-functions';
import { ModuleBase } from '../module-base';

/**
* Module to generate music related entries.
Expand All @@ -8,11 +7,7 @@ import { bindThisToMemberFunctions } from '../../internal/bind-this-to-member-fu
*
* 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.
*/
export class MusicModule {
constructor(private readonly faker: Faker) {
bindThisToMemberFunctions(this);
}

export class MusicModule extends ModuleBase {
/**
* Returns a random music genre.
*
Expand Down
Loading