UNPKG

cooky-cutter

Version:

Object factories for testing in TypeScript

22 lines (21 loc) 1.21 kB
import { Config } from "./define"; declare const DERIVE_FUNCTION_KEY = "derived"; interface DerivedFunction<Base, Output> { (result: Base, values: Config<Base>, invocations: number, path: (keyof Base)[], override: Partial<Base>, computedKeys: Array<keyof Base>): Output; __cooky_cutter: typeof DERIVE_FUNCTION_KEY; } /** * Compute a single value and assign it to the attribute based off any number * of other attributes defined in the factory. This is useful for deriving a * fields value off of other dynamic field(s) that are not known until a factory * is invoked. A derived field can reference other derived fields, but they * cannot be circularly referenced. * * @param fn a function to reduce all of the dependent keys into a single * derived value. The return value will be assigned to the attribute. * @param dependentKeys a list of all keys that the derive function is dependent * on. If the key is not defined in this list, it is not guaranteed to be * defined. */ declare function derive<Base extends object, Output>(fn: (input: Partial<Base>) => Output, ...dependentKeys: (keyof Base)[]): DerivedFunction<Base, Output>; export { derive, DerivedFunction, DERIVE_FUNCTION_KEY };