Class Factory<T, R>

Factory for building JavaScript objects, mostly useful for setting up test data

Type Parameters

Constructors

Methods

  • Register a callback function to be called after the object is generated. The callback function receives the generated object as first argument and the resolved options as second argument.

    Parameters

    • callback: (object: T, options: R) => T

      Callback function

    Returns Factory<T, R>

    factory.after((user) => {
    user.name = user.name.toUpperCase();
    });
  • Define an attribute on this factory

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • attr: K

      Name of attribute

    Returns Factory<T, R>

    factory.attr('name');
    
  • Define an attribute on this factory using a default value (e.g. a string or number)

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • attr: K

      Name of attribute

    • defaultValue: DefaultValue<T, K>

      Default value of attribute

    Returns Factory<T, R>

    factory.attr('name', 'John Doe');
    
  • Define an attribute on this factory using a generator function

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • attr: K

      Name of attribute

    • generator: Builder<T, K>

      Value generator function

    Returns Factory<T, R>

    factory.attr('name', () => function() { return 'John Doe'; });
    
  • Define an attribute on this factory using a generator function and dependencies on options or other attributes

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • attr: K

      Name of attribute

    • dependencies: Dependencies<T, K>

      Array of dependencies as option or attribute names that are used by the generator function to generate the value of this attribute

    • generator: Builder<T, K>

      Value generator function. The generator function will be called with the resolved values of the dependencies as arguments.

    Returns Factory<T, R>

    factory.attr('name', ['firstName', 'lastName'], (firstName, lastName) => {
    return `${firstName} ${lastName}`;
    });
  • Define multiple attributes on this factory using a default value (e.g. a string or number) or generator function. If you need to define dependencies on options or other attributes, use the attr method instead.

    Parameters

    • attributes: { [K in string | number | symbol]: DefaultValue<T, K> | Builder<T, K> }

      Object with multiple attributes

    Returns Factory<T, R>

    factory.attrs({
    name: 'John Doe',
    age: function() { return 21; },
    });
  • Returns an object that is generated by the factory. The optional option likelihood is a number between 0 and 100 that defines the probability that the generated object contains wrong data. This is useful for testing if your code can handle wrong data. The default value is 100, which means that the generated object always contains correct data.

    Parameters

    • attributes: { [K in string | number | symbol]?: T[K] } = {}

      object containing attribute override key value pairs

    • options: { likelihood?: number; [key: string]: any } = ...

      object containing option key value pairs

    Returns T

  • Returns an array of objects that are generated by the factory. The optional option likelihood is a number between 0 and 100 that defines the probability that the generated object contains wrong data. This is useful for testing if your code can handle wrong data. The default value is 100, which means that the generated object always contains correct data.

    Parameters

    • size: number

      number of objects to generate

    • attributes: { [K in string | number | symbol]?: T[K] } = {}

      object containing attribute override key value pairs

    • options: { likelihood?: number; [key: string]: any } = ...

      object containing option key value pairs

    Returns T[]

    factory.buildList(3, { name: 'John Doe' });
    
  • Extend this factory with another factory. The attributes and options of the other factory are merged into this factory. If an attribute or option with the same name already exists, it is overwritten.

    Type Parameters

    • P extends Partial<T>
    • J extends Partial<R>

    Parameters

    • factory: Factory<P, J>

      Factory to extend this factory with

    Returns Factory<T, R>

  • Define an option for this factory using a default value. Options are values that are not directly used in the generated object, but can be used to influence the generation process. For example, you could define an option withAddress that, when set to true, would generate an address and add it to the generated object. Like attributes, options can have dependencies on other options but not on attributes.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • opt: K

      Name of option

    • defaultValue: DefaultValue<R, K>

      Default value of option

    Returns Factory<T, R>

    factory.option('withAddress', false);
    
  • Define an option for this factory using a generator function. Options are values that are not directly used in the generated object, but can be used to influence the generation process. For example, you could define an option withAddress that, when set to true, would generate an address and add it to the generated object. Like attributes, options can have dependencies on other options but not on attributes.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • opt: K

      Name of option

    • generator: Builder<R, K>

      Value generator function

    Returns Factory<T, R>

    factory.option('withAddress', () => function() { return false; });
    
  • Define an option for this factory using a generator function with dependencies in other options. Options are values that are not directly used in the generated object, but can be used to influence the generation process. For example, you could define an option withAddress that, when set to true, would generate an address and add it to the generated object. Like attributes, options can have dependencies on other options but not on attributes.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • opt: K

      Name of option

    • dependencies: Dependencies<R, K>

      Array of dependencies as option names that are used by the generator function to generate the value of this option

    • generator: Builder<R, K>

      Value generator function with dependencies in other options. The generator function will be called with the resolved values of the dependencies as arguments.

    Returns Factory<T, R>

  • Reset all the sequences of this factory

    Returns void

  • Define an auto incrementing sequence attribute of the object. Default value is 1.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • attr: K

      Name of attribute

    Returns Factory<T, R>

    factory.sequence('id');
    
  • Define an auto incrementing sequence attribute of the object where the sequence value is generated by a generator function that is called with the current sequence value as argument.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • attr: K

      Name of attribute

    • generator: Builder<T, K>

      Value generator function

    Returns Factory<T, R>

    factory.sequence('id', (i) => function() { return i + 11; });
    
  • Define an auto incrementing sequence attribute of the object where the sequence value is generated by a generator function that is called with the current sequence value as argument and dependencies on options or other attributes.

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • attr: K

      Name of attribute

    • dependencies: (string | K)[]

      Array of dependencies as option or attribute names that are used by the generator function to generate the value of the sequence attribute

    • generator: Builder<T, K>

      Value generator function

    Returns Factory<T, R>

    factory.sequence('id', ['idPrefix'], (i, idPrefix) => function() {
    return `${idPrefix}${i}`;
    });