UNPKG

@brunoss/mock-builder

Version:
184 lines (182 loc) 5.97 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; 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); // src/index.ts var src_exports = {}; __export(src_exports, { Builder: () => Builder }); module.exports = __toCommonJS(src_exports); var Builder = class _Builder { /** * The shape of the object, where each key maps to a value or a function * that returns the value. * * @internal */ _shape; /** * Creates a new instance of `Builder`. * * @param shape - The shape of the object to be built. Each key in the shape can * be a value or a function that returns the value. * * @example * ```ts * const builder = new Builder({ * id: faker.number.int, * name: faker.lorem.word * }); * ``` * * @example * ```ts * const builder = new Builder({ * id: 1, * name: "foo" * }); * ``` */ constructor(shape) { this._shape = { ...shape }; } build(count) { let entitiesCount = 1; if (typeof count === "number") { entitiesCount = count; } else if (typeof count === "object" && count.min !== void 0 && count.max !== void 0) { entitiesCount = this.getRandomInt(count.min, count.max); } if (entitiesCount === 1) { return this.buildSingle(); } const entities = []; for (let i = 0; i < entitiesCount; i++) { entities.push(this.buildSingle()); } return entities; } buildSingle() { const data = Object.entries(this._shape).reduce((acc, [key, value]) => { acc[key] = typeof value === "function" ? value() : value; return acc; }, {}); return data; } getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } /** * Sets a specific property of the shape to always return a given value. * * @param prop - The property name to set. * @param value - The value that the property should always return. * * @returns The current `Builder` instance for chaining. * * @example * ```ts * builder.withValue('id', 123).withValue('name', 'Alice'); * const result = builder.build(); // { id: 123, name: "Alice" } * ``` */ withValue(prop, value) { const newShape = { ...this._shape, [prop]: () => value }; return new _Builder(newShape); } /** * Sets a property based on a condition. * * @param prop - The property name to set. * @param condition - The condition to evaluate. * @param trueValue - The value if the condition is true. * @param falseValue - The value if the condition is false. * * @returns The current `Builder` instance for chaining. * * @example * ```ts * builder.setConditionalValue('isAdmin', userType === 'admin', true, false); * builder.setConditionalValue("permissions", userType === "admin", adminPermissions, userPermissions); * ``` */ setConditionalValue(prop, condition, trueValue, falseValue) { const newShape = { ...this._shape, [prop]: () => condition ? trueValue : falseValue }; return new _Builder(newShape); } /** * Applies default values to properties that are not yet defined in the shape. * * If a default value is a function, it will be executed during the `build()` method * to determine the final value. * * @param defaults - An object containing default values for properties. * @returns The current `Builder` instance for chaining. * * @example * ```ts * builder.applyDefaultValues({ age: 30, isActive: true }); * builder.applyDefaultValues({ age: () => Math.floor(Math.random() * 100), isActive: true }); * * const result = builder.build(); * // Result might be { age: 45, isActive: true } depending on the random value generated. * ``` */ applyDefaultValues(defaults) { const newShape = { ...this._shape, ...defaults }; return new _Builder(newShape); } /** * Transforms the value of a specific property in the shape using a provided mapping function. * * The `mapValue` method allows you to apply a transformation function to a property in the shape. * The original value or function associated with the property is retrieved and passed to the mapping * function (`mapFn`). The result of the mapping function is then set as the new value for the property. * * @template K - The type of the property key in the object shape. * @param prop - The property name in the shape that you want to transform. * @param mapFn - A function that takes the current value of the property and returns a new value. * * @returns The current `Builder` instance for chaining. * * @example * ```ts * const builder = new Builder<Foo>({ id: 1, name: "foo" }); * builder.mapValue('name', (name) => name.toUpperCase()); * const result = builder.build(); // { id: 1, name: "FOO" } * ``` */ mapValue(prop, mapFn) { const original = this._shape[prop]; const resolveValue = () => { return typeof original === "function" ? original() : original; }; const newShape = { ...this._shape, [prop]: () => mapFn(resolveValue()) }; return new _Builder(newShape); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Builder });