UNPKG

@mixtape/core

Version:

Supercharged fixture library for organizing and generating test data

107 lines 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("./utils"); exports.decorators = Symbol('decorators'); /** * Class for bundling builders * Bundle builders in an extension to make it easy to add to a given `Fixture`. */ var Extension = /** @class */ (function () { /** * Create a new `Extension` */ function Extension() { this._builders = {}; this._typeAliases = {}; this._decorators = []; } Object.defineProperty(Extension.prototype, "builders", { /** * All builders added to the extension */ get: function () { var _this = this; return Object.keys(this._builders).map(function (k) { return _this._builders[k]; }); }, enumerable: true, configurable: true }); Object.defineProperty(Extension.prototype, exports.decorators, { /** * Decorators to apply on every addition of a builder * * Note: Use feature cautiously as one broken decorator can have a large impact */ set: function (value) { this._decorators = value; }, enumerable: true, configurable: true }); /** * Add builder * @param builder - builder to add * @returns `this` * @throws if builder with same type or alias already exists */ Extension.prototype.add = function (builder) { var _this = this; utils_1.ensure(function () { return _this._builders[builder.type] === undefined; }, "Builder for type '" + builder.type + "' already exists"); builder = this._decorators.reduce(function (b, d) { return new d(b); }, builder); this._builders[builder.type] = builder; if (builder.aliases) { builder.aliases.forEach(function (a) { utils_1.ensure(function () { return _this._typeAliases[a] === undefined; }, "Builder for type '" + _this._typeAliases[a] + "' also contains alias '" + a + "'"); _this._typeAliases[a] = builder.type; }); } return this; }; /** * Get builder * @param typeOrAlias - type or alias for builder * @returns `object` */ Extension.prototype.get = function (typeOrAlias) { var builder = this._builders[typeOrAlias]; var typeAlias = this._typeAliases[typeOrAlias]; if (!builder && !typeAlias) return undefined; return builder || this._builders[typeAlias]; }; /** * Remove builder * @param type - builder with type to remove * @returns `this` */ Extension.prototype.remove = function (type) { var _this = this; delete this._builders[type]; Object.keys(this._typeAliases).forEach(function (k) { if (_this._typeAliases[k] === type) { delete _this._typeAliases[k]; } }); return this; }; /** * Merge data from another extension * @param extension - extension to merge to this extension * @returns `this` */ Extension.prototype.merge = function (extension) { var _this = this; extension.builders.forEach(function (b) { return _this.add(b); }); return this; }; /** * Remove all builders (including aliases) */ Extension.prototype.clear = function () { this._builders = {}; this._typeAliases = {}; }; return Extension; }()); exports.Extension = Extension; //# sourceMappingURL=extension.js.map