UNPKG

dilswer

Version:

Blazingly fast data validation library with TypeScript integration.

49 lines (47 loc) 1.55 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/array-builder.ts import { TemplateBuilder } from "../template-builder.mjs"; import { TsBaseBuilder } from "./base-builder.mjs"; var ARRAY_TEMPLATE = new TemplateBuilder("Array<{{types}}>"); var EXPORT_ARRAY_TEMPLATE = new TemplateBuilder( `{{description}} {{export}}type {{name}} = Array<{{types}}>;` ); var TsArrayBuilder = class extends TsBaseBuilder { constructor() { super(...arguments); __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 array elements. */ setTypes(types) { this.types = new Set(types); } build(indent) { const types = this.getTypes(indent); return ARRAY_TEMPLATE.build({ types: types.join(" | ") }); } buildExport(type) { var _a; const types = this.getTypes(""); return EXPORT_ARRAY_TEMPLATE.build({ types: types.join(" | "), description: this.description, name: (_a = this.name) != null ? _a : this.generateName("Array"), export: this.parseExportType(type) }); } }; export { TsArrayBuilder };