@mixtape/core
Version:
Supercharged fixture library for organizing and generating test data
48 lines (47 loc) • 1.58 kB
TypeScript
import { FixtureContext } from './fixture';
import { ValueGenerator } from './generators';
/**
* Data object created from a template
* @type {object}
*/
declare type TemplateObject<T> = {
[P in keyof T]: TemplateProperty<T[P]>;
};
/**
* Property of a TemplateObject
* @type {object}
*/
declare type TemplateProperty<T> = T extends any[] ? any[] : T extends object ? TemplateObject<T> : any;
/**
* Class for generating object(s) from a template.
* From a template the class can be used to generate one or more objects
* with random data based on the types defined in the template.
*/
export default class ObjectBuilder<T extends object> {
private readonly _template;
private readonly _context;
private readonly _generator;
/**
* Create a new `ObjectBuilder`
* @param template - object template to generate object from
* @param context - fixture context to use when generating data
* @param generator - generator to use when generating numbers
* @throws if template is not an object
*/
constructor(template: T, context: FixtureContext, generator: ValueGenerator<number>);
/**
* Create single object
* @returns single `object` based on template
* @throws on invalid template syntax
*/
create(): TemplateObject<T>;
/**
* Create array of objects
* @param size - size of array to create
* @returns `Array` of `objects` based on template
* @throws on invalid template syntax
*/
createMany(size?: number): Array<TemplateObject<T>>;
private build;
}
export {};