@factory-js/factory
Version:
🏭 The object generator for testing
251 lines (242 loc) • 7.37 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __typeError = (msg) => {
throw TypeError(msg);
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
// src/index.ts
var index_exports = {};
__export(index_exports, {
factory: () => factory,
later: () => later,
seq: () => seq
});
module.exports = __toCommonJS(index_exports);
// src/helpers/later.ts
var later = () => {
return () => {
throw new Error(
"The 'later' function was called. Did you forget to override it?"
);
};
};
// src/helpers/seq.ts
var seq = (initialCount, iteratee) => {
let count = initialCount;
return (...args) => iteratee(count++, ...args);
};
// src/utils/map-values.ts
var mapValues = (record, iteratee) => {
const entries = Object.entries(record).map(([key, value]) => {
const newValue = iteratee(value, key);
return [key, newValue];
});
return Object.fromEntries(entries);
};
// src/utils/map-values-async.ts
var mapValuesAsync = async (record, iteratee) => {
const promises = Object.entries(record).map(async ([key, value]) => {
const newValue = await iteratee(value, key);
return [key, newValue];
});
return Object.fromEntries(await Promise.all(promises));
};
// src/utils/memo.ts
var memo = (lazy) => {
let evaluated = false;
let value = void 0;
return (...args) => {
if (!evaluated) value = lazy(...args);
evaluated = true;
return value;
};
};
// src/utils/proxy-deps.ts
var proxyDeps = (deps) => {
const memos = mapValues(deps, (value) => memo(value));
const proxy = new Proxy(memos, {
get: (target, key) => target[key]?.(proxy)
});
return proxy;
};
// src/factory.ts
var _initProps, _initVars, _props, _vars, _create, _afterHooks, _traits, _Factory_instances, clone_get, proxyProps_fn;
var _Factory = class _Factory {
constructor({
initProps,
initVars,
props,
vars,
create,
traits,
afterHooks
}) {
__privateAdd(this, _Factory_instances);
__privateAdd(this, _initProps);
__privateAdd(this, _initVars);
__privateAdd(this, _props);
__privateAdd(this, _vars);
__privateAdd(this, _create);
__privateAdd(this, _afterHooks);
__privateAdd(this, _traits);
__privateSet(this, _initProps, initProps);
__privateSet(this, _initVars, initVars);
__privateSet(this, _props, props);
__privateSet(this, _vars, vars);
__privateSet(this, _create, create);
__privateSet(this, _traits, traits);
__privateSet(this, _afterHooks, afterHooks);
}
props(props) {
return new _Factory({
...__privateGet(this, _Factory_instances, clone_get),
props: {
...__privateGet(this, _props),
...props
}
});
}
vars(vars) {
return new _Factory({
...__privateGet(this, _Factory_instances, clone_get),
vars: {
...__privateGet(this, _vars),
...vars
}
});
}
traits(traits) {
return new _Factory({
...__privateGet(this, _Factory_instances, clone_get),
traits: {
...__privateGet(this, _traits),
...traits
}
});
}
use(pick) {
const { props = {}, vars = {}, after } = pick(__privateGet(this, _traits));
return new _Factory({
...__privateGet(this, _Factory_instances, clone_get),
props: {
...__privateGet(this, _props),
...props
},
vars: {
...__privateGet(this, _vars),
...vars
},
afterHooks: [
...__privateGet(this, _afterHooks),
...after !== void 0 ? [after] : []
]
});
}
after(after) {
return new _Factory({
...__privateGet(this, _Factory_instances, clone_get),
afterHooks: [...__privateGet(this, _afterHooks), after]
});
}
async build() {
const vars = proxyDeps(__privateGet(this, _vars));
const props = __privateMethod(this, _Factory_instances, proxyProps_fn).call(this, vars);
return await mapValuesAsync(props, (prop) => prop);
}
async buildList(count) {
const objects = [];
for (let i = 0; i < count; i++) {
objects.push(await this.build());
}
return objects;
}
async create() {
const vars = proxyDeps(__privateGet(this, _vars));
const props = __privateMethod(this, _Factory_instances, proxyProps_fn).call(this, vars);
const object = await __privateGet(this, _create).call(this, await mapValuesAsync(props, (prop) => prop));
for (const after of __privateGet(this, _afterHooks)) await after(object, vars);
return object;
}
async createList(count) {
const objects = [];
for (let i = 0; i < count; i++) {
objects.push(await this.create());
}
return objects;
}
get def() {
return {
props: __privateGet(this, _initProps),
vars: __privateGet(this, _initVars)
};
}
};
_initProps = new WeakMap();
_initVars = new WeakMap();
_props = new WeakMap();
_vars = new WeakMap();
_create = new WeakMap();
_afterHooks = new WeakMap();
_traits = new WeakMap();
_Factory_instances = new WeakSet();
clone_get = function() {
return {
initProps: __privateGet(this, _initProps),
initVars: __privateGet(this, _initVars),
props: __privateGet(this, _props),
create: __privateGet(this, _create),
traits: __privateGet(this, _traits),
afterHooks: __privateGet(this, _afterHooks),
vars: __privateGet(this, _vars)
};
};
proxyProps_fn = function(vars) {
return proxyDeps(
mapValues(
__privateGet(this, _props),
(prop) => (props) => prop({ props, vars })
)
);
};
var Factory = _Factory;
var noCreate = () => {
throw new Error(
"You must specify the 2nd argument to use the create method."
);
};
var define = ({ props, vars }, create = noCreate) => new Factory({
initProps: props,
initVars: vars,
traits: {},
afterHooks: [],
props,
vars,
create
});
var factory = { define };
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
factory,
later,
seq
});