UNPKG

@mdf.js/faker

Version:

MMS - API - Faker

276 lines 12.1 kB
/** Type for attribute dependencies */ export type Dependencies<T, K extends keyof T> = (K | string)[]; /** Type for function for attribute builder function */ export type Builder<T, K extends keyof T> = (...args: any) => T[K] | undefined; /** Type for attribute default value */ export type DefaultValue<T, K extends keyof T> = T[K]; /** Interface for default object */ export interface DefaultObject { [key: string]: any; } /** Interface for default options */ export interface DefaultOptions { likelihood?: number; [key: string]: any; } /** Factory for building JavaScript objects, mostly useful for setting up test data */ export declare class Factory<T extends DefaultObject = DefaultObject, R extends DefaultOptions = DefaultOptions> { /** Options for programmatic generation of attributes */ private _opts; /** Attributes of this factory, based on a interface */ private _attrs; /** Auto incrementing sequence attribute */ private _seques; /** Callback function array */ private readonly _callbacks; /** Chance object for probabilistic wrong value generation */ private readonly _chance; /** Create a new factory instance */ constructor(); /** * Define an attribute on this factory * @param attr - Name of attribute * @example * ```typescript * factory.attr('name'); * ``` */ attr<K extends keyof T>(attr: K): Factory<T, R>; /** * Define an attribute on this factory using a default value (e.g. a string or number) * @param attr - Name of attribute * @param defaultValue - Default value of attribute * @example * ```typescript * factory.attr('name', 'John Doe'); * ``` */ attr<K extends keyof T>(attr: K, defaultValue: DefaultValue<T, K>): Factory<T, R>; /** * Define an attribute on this factory using a generator function * @param attr - Name of attribute * @param generator - Value generator function * @example * ```typescript * factory.attr('name', () => function() { return 'John Doe'; }); * ``` */ attr<K extends keyof T>(attr: K, generator: Builder<T, K>): Factory<T, R>; /** * Define an attribute on this factory using a generator function and dependencies on options or * other attributes * @param attr - Name of attribute * @param dependencies - Array of dependencies as option or attribute names that are used by the * generator function to generate the value of this attribute * @param generator - Value generator function. The generator function will be called with the * resolved values of the dependencies as arguments. * @example * ```typescript * factory.attr('name', ['firstName', 'lastName'], (firstName, lastName) => { * return `${firstName} ${lastName}`; * }); * ``` */ attr<K extends keyof T>(attr: K, dependencies: Dependencies<T, K>, generator: Builder<T, K>): Factory<T, R>; /** * 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. * @param attributes - Object with multiple attributes * @example * ```typescript * factory.attrs({ * name: 'John Doe', * age: function() { return 21; }, * }); * ``` */ attrs(attributes: { [K in keyof T]: DefaultValue<T, K> | Builder<T, K>; }): 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. * @param opt - Name of option * @param defaultValue - Default value of option * @example * ```typescript * factory.option('withAddress', false); * ``` */ option<K extends keyof R>(opt: K, defaultValue: DefaultValue<R, K>): Factory<T, R>; /** * 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. * @param opt - Name of option * @param generator - Value generator function * @example * ```typescript * factory.option('withAddress', () => function() { return false; }); * ``` */ option<K extends keyof R>(opt: K, generator: Builder<R, K>): Factory<T, R>; /** * 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. * @param opt - Name of option * @param dependencies - Array of dependencies as option names that are used by the generator * function to generate the value of this option * @param generator - Value generator function with dependencies in other options. The generator * function will be called with the resolved values of the dependencies as arguments. */ option<K extends keyof R>(opt: K, dependencies: Dependencies<R, K>, generator: Builder<R, K>): Factory<T, R>; /** * Define an auto incrementing sequence attribute of the object. Default value is 1. * @param attr - Name of attribute * @example * ```typescript * factory.sequence('id'); * ``` */ sequence<K extends keyof T>(attr: K): Factory<T, R>; /** * 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. * @param attr - Name of attribute * @param generator - Value generator function * @example * ```typescript * factory.sequence('id', (i) => function() { return i + 11; }); * ``` */ sequence<K extends keyof T>(attr: K, generator: Builder<T, K>): Factory<T, R>; /** * 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. * @param attr - Name of attribute * @param dependencies - Array of dependencies as option or attribute names that are used by the * generator function to generate the value of the sequence attribute * @param generator - Value generator function * @example * ```typescript * factory.sequence('id', ['idPrefix'], (i, idPrefix) => function() { * return `${idPrefix}${i}`; * }); * ``` */ sequence<K extends keyof T>(attr: K, dependencies: (K | string)[], generator: Builder<T, K>): Factory<T, R>; /** * 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. * @param callback - Callback function * @example * ```typescript * factory.after((user) => { * user.name = user.name.toUpperCase(); * }); * ``` */ after(callback: (object: T, options: R) => T): Factory<T, R>; /** * 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. * @param attributes - object containing attribute override key value pairs * @param options - object containing option key value pairs */ build(attributes?: { [K in keyof T]?: T[K]; }, options?: { likelihood?: number; [key: string]: any; }): 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. * @param size - number of objects to generate * @param attributes - object containing attribute override key value pairs * @param options - object containing option key value pairs * @example * ```typescript * factory.buildList(3, { name: 'John Doe' }); * ``` */ buildList(size: number, attributes?: { [K in keyof T]?: T[K]; }, options?: { likelihood?: number; [key: string]: any; }): T[]; /** * 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. * @param factory - Factory to extend this factory with */ extend<P extends Partial<T>, J extends Partial<R>>(factory: Factory<P, J>): Factory<T, R>; /** Reset all the sequences of this factory */ reset(): void; /** * Wrap a callback function to add type safety and avoid lost of data of extended factories * @param callback - Callback function */ private readonly wrapCallback; /** * Create an object with standard Options from a key-value pairs object * @param options - object containing option key value pairs */ private _ConvertToOptions; /** * Resolve the value of the options or attributes * @param meta - metadata information of option or attribute * @param object - object with resolved attributes * @param resolvedOptions - object with resolved options * @param attributes - attributes object * @param options - options object * @param stack - stack of recursive calls */ private _Build; /** * Resolve the value of the options or attributes if this has dependencies * @param dependencies - option or attribute dependencies * @param object - object with resolved attributes * @param resolvedOptions - object with resolved options * @param attributes - attributes object * @param options - options object * @param stack - stack of recursive calls */ private _BuildWithDependencies; /** * Return Generator function if the argument is not a function * @param generator - Generator function or value */ private _ReturnFunction; /** * Return a Entry object with dependencies and builder function * @param generatorOptions - Default value or generator function or dependencies * @param generator - Generator function or value */ private _SafeType; /** * Generate wrong data, excluding the correct data type * @param type - type of good data */ private _wrongData; /** * Create an object with standard Attributes from a key-value pairs object * @param attributes - object containing attribute override key value pairs */ private _convertToAttributes; } //# sourceMappingURL=Factory.d.ts.map