cooky-cutter
Version:
Object factories for testing in TypeScript
37 lines (36 loc) • 1.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FACTORY_FUNCTION_KEY = exports.define = void 0;
const compute_1 = require("./compute");
const FACTORY_FUNCTION_KEY = "factory";
exports.FACTORY_FUNCTION_KEY = FACTORY_FUNCTION_KEY;
/**
* Define a new factory function. The return value is a function that can be
* invoked as many times as needed to create a given type of object. Use the
* config param to define how the object is generated on each invocation.
*
* @param config An object that defines how the factory should generate objects.
* Each key can either be a static value, a function that receives the
* invocation count as the only parameter or another factory.
*/
function define(config) {
let invocations = 0;
const factory = (override = {}) => {
invocations++;
let result = {};
let computedKeys = [];
const values = Object.assign({}, config, override);
for (let key in values) {
compute_1.compute(key, values, result, invocations, [], override, computedKeys);
}
return result;
};
// Define a property to differentiate this function during the evaluation
// phase when the factory is later invoked.
factory.__cooky_cutter = FACTORY_FUNCTION_KEY;
factory.resetSequence = () => {
invocations = 0;
};
return factory;
}
exports.define = define;