UNPKG

@mixtape/core

Version:

Supercharged fixture library for organizing and generating test data

52 lines (51 loc) 1.69 kB
import { FixtureContext } from './fixture'; import { ValueGenerator } from './generators'; /** * Class for creating object(s) with custom values. * The class makes it easy to update/overwrite/remove properties or property values from objects. */ export default class TypeComposer<T extends object> { private readonly _context; private readonly _type; private readonly _generator; private readonly _modifications; /** * Create a new `TypeComposer` * @param type - type to compose * @param context - fixture context use when generating data * @param generator - number generator to use */ constructor(type: string, context: FixtureContext, generator: ValueGenerator<number>); /** * Perform action on object * @param action - function to apply on object * @returns `this` */ do(action: (type: T) => any): this; /** * Overwrite or update value on object * @param property - property to overwrite/update * @param value - function returning the new value * @returns `this` */ with<K extends keyof T>(property: K, value: (selected: T[K]) => T[K]): this; /** * Remove property from object * @param property - property to remove from object * @returns `this` */ without<K extends keyof T>(property: K): this; /** * Create custom type * @returns single custom type * @throws if input is invalid */ create(): T; /** * Create array of custom types * @param size - size of array to create (optional) * @returns `Array` of custom types * @throws if input is invalid */ createMany(size?: number): T[]; }