UNPKG

@lucaspaganini/value-objects

Version:

TypeScript first validation and class creation library

81 lines 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VOSet = void 0; var errors_1 = require("./errors"); var isSetable = function (element) { return ['number', 'string', 'boolean'].includes(typeof element); }; var expectedSetableTypes = function (set) { return Array.from(new Set(set.map(function (v) { return typeof v; }))); }; /** * Function to create a set of elements value object constructor. * * @template Element Literal type of the set elements. * @template Strict Literal boolean indicating the `options.strict` flag. * @param elements Elements in the set, they must be {@link Setable}. * @param options Customizations for the returned class constructor. * @returns Class constructor that accepts a set element for instantiation * and returns that element when {@link VOSetInstance.valueOf} is called. * * @example * ```typescript * class TestSet extends VOSet([123, 'abc']) {} * new TestSet('abc'); // OK * new TestSet(''); // Runtime error: Not in set * new TestSet(1); // Runtime error: Not in set * new TestSet(false); // Compilation error: Expects string | number * ``` * * The coolest part of `VOSet` are definitely the conditional types * in {@link VOSetRaw} that decide what is expected for instantiation and * the customization of this behaviour using the `options.strict` flag. * See the examples below. * * @example * ```typescript * class NonStrictSet extends VOSet([123, true]) {} * new NonStrictSet(true); // OK * new NonStrictSet('abc'); // Compilation error: Expects number | boolean * new NonStrictSet(''); // Compilation error: Expects number | boolean * new NonStrictSet(1); // Runtime error: Not in set * new NonStrictSet(false); // Runtime error: Not in set * ``` * * @example * ```typescript * class StrictSet extends VOSet([123, true], { strict: true }) {} * new StrictSet(true); // OK * new StrictSet('abc'); // Compilation error: Expects true | 123 * new StrictSet(''); // Compilation error: Expects true | 123 * new StrictSet(1); // Compilation error: Expects true | 123 * new StrictSet(false); // Compilation error: Expects true | 123 * ``` */ var VOSet = function (elements, options) { var _a; if (options === void 0) { options = {}; } for (var _i = 0, _b = Object.entries(elements); _i < _b.length; _i++) { var _c = _b[_i], i = _c[0], v = _c[1]; if (!isSetable(v)) throw new errors_1.RawTypeError('number | string | boolean', typeof v, "elements[" + i + "]"); } var set = new Set(elements); var stringfiedSet = Array.from(set).map(function (x) { return x.toString(); }); var isInSet = function (v) { return set.has(v); }; var strict = (_a = options.strict) !== null && _a !== void 0 ? _a : false; var nonStrictExpectedTypes = expectedSetableTypes(elements); return /** @class */ (function () { function class_1(raw) { if (!strict && !nonStrictExpectedTypes.includes(typeof raw)) throw new errors_1.RawTypeError(nonStrictExpectedTypes.join(' | '), typeof raw); if (!isInSet(raw)) throw new errors_1.NotInSetError(stringfiedSet, raw.toString(), ''); this._value = raw; } class_1.prototype.valueOf = function () { return this._value; }; return class_1; }()); }; exports.VOSet = VOSet; //# sourceMappingURL=set.js.map