UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

49 lines (47 loc) 1.54 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // src/ts-type-generator/type-builders/union-builder.ts import { TemplateBuilder } from "../template-builder.mjs"; import { TsBaseBuilder } from "./base-builder.mjs"; var UNION_TEMPLATE = new TemplateBuilder("{{types}}"); var EXPORT_UNION_TEMPLATE = new TemplateBuilder( `{{description}} {{export}}type {{name}} = {{types}};` ); var TsUnionBuilder = class extends TsBaseBuilder { constructor() { super(...arguments); __publicField(this, "types", /* @__PURE__ */ new Set()); } getUnionTypes(indent) { const types = []; for (const type of this.types) { types.push(type.build(indent)); } return types; } /** Sets types that are part of the union. */ setTypes(type) { this.types = new Set(type); } build(indent) { const types = this.getUnionTypes(indent); return UNION_TEMPLATE.build({ types: types.join(" | ") }); } buildExport(type) { var _a; const types = this.getUnionTypes(""); return EXPORT_UNION_TEMPLATE.build({ types: types.join(" | "), description: this.description, name: (_a = this.name) != null ? _a : this.generateName("Union"), export: this.parseExportType(type) }); } }; export { TsUnionBuilder };