UNPKG

@mixtape/core

Version:

Supercharged fixture library for organizing and generating test data

121 lines 4.14 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("./utils"); /** * Class for creating object(s) with custom values. * The class makes it easy to update/overwrite/remove properties or property values from objects. */ var TypeComposer = /** @class */ (function () { /** * Create a new `TypeComposer` * @param type - type to compose * @param context - fixture context use when generating data * @param generator - number generator to use */ function TypeComposer(type, context, generator) { this._context = context; this._type = type; this._generator = generator; this._modifications = []; } /** * Perform action on object * @param action - function to apply on object * @returns `this` */ TypeComposer.prototype.do = function (action) { this._modifications.push({ type: 'do', action: action }); return this; }; /** * Overwrite or update value on object * @param property - property to overwrite/update * @param value - function returning the new value * @returns `this` */ TypeComposer.prototype.with = function (property, value) { this._modifications.push({ type: 'with', property: property, action: value }); return this; }; /** * Remove property from object * @param property - property to remove from object * @returns `this` */ TypeComposer.prototype.without = function (property) { this._modifications.push({ type: 'without', property: property }); return this; }; /** * Create custom type * @returns single custom type * @throws if input is invalid */ TypeComposer.prototype.create = function () { var _this = this; var object = this._context.create(this._type); utils_1.ensure(function () { return utils_1.isObject(object); }, "TypeComposer can only be used with type 'object'", TypeError); this._modifications.forEach(function (a) { switch (a.type) { case 'do': a.action(object); break; case 'with': var currentValue_1 = object[a.property]; utils_1.ensure(function () { return currentValue_1 !== undefined; }, "Property '" + a.property + "' does not exist on type '" + _this._type + "'", ReferenceError); if (utils_1.isArray(currentValue_1)) { object[a.property] = a.action(currentValue_1.slice()); } else if (utils_1.isObject(currentValue_1)) { object[a.property] = a.action(__assign({}, currentValue_1)); } else { object[a.property] = a.action(currentValue_1); } break; case 'without': delete object[a.property]; break; } }); return object; }; /** * Create array of custom types * @param size - size of array to create (optional) * @returns `Array` of custom types * @throws if input is invalid */ TypeComposer.prototype.createMany = function (size) { var list = []; size = size ? size : this._generator.generate(); for (var i = 0; i < size; i++) { list.push(this.create()); } return list; }; return TypeComposer; }()); exports.default = TypeComposer; //# sourceMappingURL=type-composer.js.map