interface-forge - v2.5.0
    Preparing search index...

    This is Faker's main class containing all modules that can be used to generate data.

    Please have a look at the individual modules and methods for more information and examples.

    import { faker } from '@faker-js/faker';
    // const { faker } = require('@faker-js/faker');

    // faker.seed(1234);

    faker.person.firstName(); // 'John'
    faker.person.lastName(); // 'Doe'
    import { Faker, es } from '@faker-js/faker';
    // const { Faker, es } = require('@faker-js/faker');

    // create a Faker instance with only es data and no en fallback (=> smaller bundle size)
    const customFaker = new Faker({ locale: [es] });

    customFaker.person.firstName(); // 'Javier'
    customFaker.person.lastName(); // 'Ocampo Corrales'

    customFaker.music.genre(); // throws Error as this data is not available in `es`

    Hierarchy (View Summary)

    Index

    Constructors

    • Creates a new instance of Faker.

      In most cases you should use one of the prebuilt Faker instances instead of the constructor, for example fakerDE, fakerFR, ...

      You only need to use the constructor if you need custom fallback logic or a custom locale.

      For more information see our Localization Guide.

      Parameters

      • options: {
            locale: LocaleDefinition | LocaleDefinition[];
            randomizer?: Randomizer;
            seed?: number;
        }

        The options to use.

        • locale: LocaleDefinition | LocaleDefinition[]

          The locale data to use for this instance. If an array is provided, the first locale that has a definition for a given property will be used. Please make sure that all required locales and their parent locales are present, e.g. [de_AT, de, en, base].

          mergeLocales(): For more information about how the locales are merged.

        • Optionalrandomizer?: Randomizer

          The Randomizer to use. Specify this only if you want to use it to achieve a specific goal, such as sharing the same random generator with other instances/tools.

          generateMersenne53Randomizer()
          
        • Optionalseed?: number

          The initial seed to use. The seed can be used to generate reproducible values.

          Refer to the seed() method for more information.

          Defaults to a random seed.

      Returns Faker

      import { Faker, es } from '@faker-js/faker';
      // const { Faker, es } = require('@faker-js/faker');

      // create a Faker instance with only es data and no en fallback (=> smaller bundle size)
      const customFaker = new Faker({ locale: [es] });

      customFaker.person.firstName(); // 'Javier'
      customFaker.person.lastName(); // 'Ocampo Corrales'

      customFaker.music.genre(); // throws Error as this data is not available in `es`

      8.0.0

    Properties

    _defaultRefDate: () => Date
    airline: AirlineModule
    animal: AnimalModule
    commerce: CommerceModule
    company: CompanyModule
    database: DatabaseModule
    datatype: DatatypeModule
    definitions: LocaleProxy
    finance: FinanceModule
    hacker: HackerModule
    helpers: HelpersModule
    internet: InternetModule
    location: LocationModule
    number: NumberModule
    person: PersonModule
    rawDefinitions: LocaleDefinition
    science: ScienceModule
    string: StringModule
    system: SystemModule
    vehicle: VehicleModule

    Accessors

    • get defaultRefDate(): () => Date

      Gets a new reference date used to generate relative dates.

      Returns () => Date

    • get name(): PersonModule

      Returns PersonModule

      Use Faker#person instead

    Methods

    • Returns an object with metadata about the current locale.

      Returns MetadataDefinition

      import { faker, fakerES_MX } from '@faker-js/faker';
      // const { faker, fakerES_MX } = require("@faker-js/faker")
      faker.getMetadata(); // { title: 'English', code: 'en', language: 'en', endonym: 'English', dir: 'ltr', script: 'Latn' }
      fakerES_MX.getMetadata(); // { title: 'Spanish (Mexico)', code: 'es_MX', language: 'es', endonym: 'Español (México)', dir: 'ltr', script: 'Latn', country: 'MX' }

      8.1.0

    • 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 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.

      Parameters

      • Optionalseed: number

        The seed to use. Defaults to a random number.

      Returns number

      The seed that was set.

      // Consistent values for tests:
      faker.seed(42)
      faker.number.int(10); // 4
      faker.number.int(10); // 10

      faker.seed(42)
      faker.number.int(10); // 4
      faker.number.int(10); // 10

      // 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());

      6.0.0

    • Sets the seed array.

      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.

      Parameters

      • seedArray: number[]

        The seed array to use.

      Returns number[]

      The seed array that was set.

      // Consistent values for tests:
      faker.seed([42, 13, 17])
      faker.number.int(10); // 3
      faker.number.int(10); // 10

      faker.seed([42, 13, 17])
      faker.number.int(10); // 3
      faker.number.int(10); // 10

      // 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());

      6.0.0

    • 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.

      Parameters

      • Optionalseed: number | number[]

        The seed or seed array to use.

      Returns number | number[]

      The seed that was set.

      // Consistent values for tests (using a number):
      faker.seed(42)
      faker.number.int(10); // 4
      faker.number.int(10); // 10

      faker.seed(42)
      faker.number.int(10); // 4
      faker.number.int(10); // 10

      // Consistent values for tests (using an array):
      faker.seed([42, 13, 17])
      faker.number.int(10); // 3
      faker.number.int(10); // 10

      faker.seed([42, 13, 17])
      faker.number.int(10); // 3
      faker.number.int(10); // 10

      // 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());

      6.0.0

    • Sets the refDate source to use if no refDate date is passed to the date methods.

      Parameters

      • OptionaldateOrSource: string | number | Date | (() => Date)

        The function or the static value used to generate the refDate date instance. The function must return a new valid Date instance for every call. Defaults to () => new Date().

      Returns void

      faker.seed(1234);

      // Default behavior
      // faker.setDefaultRefDate();
      faker.date.past(); // Changes based on the current date/time

      // Use a static ref date
      faker.setDefaultRefDate(new Date('2020-01-01'));
      faker.date.past(); // Reproducible '2019-07-03T08:27:58.118Z'

      // Use a ref date that changes every time it is used
      let clock = new Date("2020-01-01").getTime();
      faker.setDefaultRefDate(() => {
      clock += 1000; // +1s
      return new Date(clock);
      });

      faker.defaultRefDate() // 2020-01-01T00:00:01Z
      faker.defaultRefDate() // 2020-01-01T00:00:02Z

      8.0.0