UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

47 lines (45 loc) 1.57 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/dictionary-builder.ts import { TemplateBuilder } from "../template-builder.mjs"; import { TsBaseBuilder } from "./base-builder.mjs"; var DICT_TEMPLATE = new TemplateBuilder("Record<string | number, {{type}}>"); var EXPORTED_DICT_TEMPLATE = new TemplateBuilder(`{{description}} {{export}}type {{name}} = Record<string | number, {{type}}>`); var TsDictBuilder = class extends TsBaseBuilder { constructor() { super(); __publicField(this, "types", /* @__PURE__ */ new Set()); } getTypes(indent) { const types = []; for (const type of this.types) { types.push(type.build(indent)); } return types; } /** Sets what types are allowed as the dictionary elements. */ setTypes(type) { this.types = new Set(type); } build(indent) { const types = this.getTypes(indent); return DICT_TEMPLATE.build({ type: types.join(" | ") }); } buildExport(type) { var _a; const types = this.getTypes(""); return EXPORTED_DICT_TEMPLATE.build({ name: (_a = this.name) != null ? _a : this.generateName("Dict"), description: this.description, type: types.join(" | "), export: this.parseExportType(type) }); } }; export { TsDictBuilder };