Skip to main content
Version: 5.9

Faker

Index

Constructors

constructor

  • new Faker(opts: FakerOptions): Faker
  • Parameters

    • opts: FakerOptions

    Returns Faker

Properties

readonlyaddress

address: AddressModule

readonlyanimal

animal: AnimalModule

readonlycolor

color: ColorModule

readonlycommerce

commerce: CommerceModule

readonlycompany

company: CompanyModule

readonlydatabase

database: DatabaseModule

readonlydatatype

datatype: DatatypeModule

readonlydate

date: DateModule

readonlydefinitions

definitions: LocaleDefinition

readonlyfake

fake: (str: string) => string

Type declaration

    • (str: string): string
    • Generator for combining faker methods based on a static string input.

      Note: We recommend using string template literals instead of fake(), which are faster and strongly typed (if you are using TypeScript), e.g. `const address = `${faker.address.zipCode()} ${faker.address.city()}`;`

      This method is useful if you have to build a random string from a static, non-executable source (e.g. string coming from a user, stored in a database or a file).

      It checks the given string for placeholders and replaces them by calling faker methods:

      const hello = faker.fake('Hi, my name is {{name.firstName}} {{name.lastName}}!')

      This would use the faker.name.firstName() and faker.name.lastName() method to resolve the placeholders respectively.

      It is also possible to provide parameters. At first, they will be parsed as json, and if that isn't possible, we will fall back to string:

      const message = faker.fake(`You can call me at {{phone.number(+!# !## #### #####!)}}.')

      Currently it is not possible to set more than a single parameter.

      It is also NOT possible to use any non-faker methods or plain javascript in such templates.

      @see
      • faker.helpers.mustache() to use custom functions for resolution.
      • faker.helpers.fake()
      @example
      faker.fake('{{name.lastName}}') // 'Barrows'
      faker.fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}') // 'Durgan, Noe MD'
      faker.fake('This is static test.') // 'This is static test.'
      faker.fake('Good Morning {{name.firstName}}!') // 'Good Morning Estelle!'
      faker.fake('You can call me at {{phone.number(!## ### #####!)}}.') // 'You can call me at 202 555 973722.'
      faker.fake('I flipped the coin and got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin and got: tails'
      @since

      3.0.0

      @deprecated

      Use faker.helpers.fake() instead.


      Parameters

      • str: string

        The template string that will get interpolated. Must not be empty.

      Returns string

readonlyfinance

finance: FinanceModule

readonlygit

git: GitModule

readonlyhacker

hacker: HackerModule

readonlyhelpers

helpers: HelpersModule

readonlyimage

image: ImageModule

readonlyinternet

internet: InternetModule

locales

locales: Partial<Record<UsableLocale, LocaleDefinition>>

readonlylorem

lorem: LoremModule

readonlymersenne

mersenne: MersenneModule
@deprecated

Internal. Use faker.datatype.number() or faker.seed() instead.

readonlymusic

music: MusicModule

readonlyname

name: NameModule

readonlyphone

phone: PhoneModule

readonlyrandom

random: RandomModule

readonlyscience

science: ScienceModule

readonlysystem

system: SystemModule

readonlyunique

unique: <Method>(method: Method, args?: Parameters<Method>, options?: { compare?: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1; currentIterations?: number; exclude?: RecordKey | RecordKey[]; maxRetries?: number; maxTime?: number; startTime?: number; store?: Record<RecordKey, RecordKey> }) => ReturnType<Method>

Type declaration

    • <Method>(method: Method, args?: Parameters<Method>, options?: { compare?: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1; currentIterations?: number; exclude?: RecordKey | RecordKey[]; maxRetries?: number; maxTime?: number; startTime?: number; store?: Record<RecordKey, RecordKey> }): ReturnType<Method>
    • Generates a unique result using the results of the given method. Used unique entries will be stored internally and filtered from subsequent calls.

      @see

      faker.helpers.unique()

      @example
      faker.unique(faker.name.firstName) // 'Corbin'
      @since

      5.0.0

      @deprecated

      Use faker.helpers.unique() instead.


      Type parameters

      • Method: (...parameters: any[]) => RecordKey

        The type of the method to execute.

      Parameters

      • method: Method

        The method used to generate the values.

      • optionalargs: Parameters<Method>

        The arguments used to call the method.

      • optionaloptions: { compare?: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1; currentIterations?: number; exclude?: RecordKey | RecordKey[]; maxRetries?: number; maxTime?: number; startTime?: number; store?: Record<RecordKey, RecordKey> }

        The optional options used to configure this method.

        • optionalcompare: (obj: Record<RecordKey, RecordKey>, key: RecordKey) => 0 | -1
        • optionalcurrentIterations: number

          This parameter does nothing.

        • optionalexclude: RecordKey | RecordKey[]

          The value or values that should be excluded/skipped. Defaults to [].

        • optionalmaxRetries: number

          The total number of attempts to try before throwing an error. Defaults to 50.

        • optionalmaxTime: number

          The time in milliseconds this method may take before throwing an error. Defaults to 50.

        • optionalstartTime: number

          This parameter does nothing.

        • optionalstore: Record<RecordKey, RecordKey>

          The store of unique entries. Defaults to a global store.

      Returns ReturnType<Method>

readonlyvehicle

vehicle: VehicleModule

readonlyword

word: WordModule

Accessors

locale

  • get locale(): UsableLocale
  • set locale(locale: UsableLocale): void
  • Returns UsableLocale

  • Parameters

    • locale: UsableLocale

    Returns void

localeFallback

  • get localeFallback(): UsableLocale
  • set localeFallback(localeFallback: UsableLocale): void
  • Returns UsableLocale

  • Parameters

    • localeFallback: UsableLocale

    Returns void

Methods

seed

  • seed(seed?: number): number
  • seed(seedArray: number[]): number[]
  • Sets the seed or generates a new one.

    Please note that generated values are dependent on both the seed and the number of calls that have been made since it was set.

    This method is intended to allow for consistent values in a tests, so you might want to use hardcoded values as the seed.

    In addition to that it can be used for creating truly random tests (by passing no arguments), that still can be reproduced if needed, by logging the result and explicitly setting it if needed.

    @example
    // Consistent values for tests:
    faker.seed(42)
    faker.datatype.number(10); // 4
    faker.datatype.number(10); // 8

    faker.seed(42)
    faker.datatype.number(10); // 4
    faker.datatype.number(10); // 8
    @example
    // Random but reproducible tests:
    // Simply log the seed, and if you need to reproduce it, insert the seed here
    console.log('Running test with seed:', faker.seed());

    Parameters

    • optionalseed: number

      The seed to use. Defaults to a random number.

    Returns number

    The seed that was set.

setLocale

  • setLocale(locale: UsableLocale): void
  • Set Faker's locale


    Parameters

    • locale: UsableLocale

      The locale to set (e.g. en or en_AU, en_AU_ocker).

    Returns void