UNPKG

@barchart/common-js

Version:
159 lines (153 loc) 5.97 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var SchemaBuilder_exports = {}; __export(SchemaBuilder_exports, { default: () => SchemaBuilder }); module.exports = __toCommonJS(SchemaBuilder_exports); var assert = __toESM(require("./../../../lang/assert.js")); var import_Component = __toESM(require("./../Component.js")); var import_DataType = __toESM(require("./../DataType.js")); var import_Field = __toESM(require("./../Field.js")); var import_Schema = __toESM(require("./../Schema.js")); var import_ComponentBuilder = __toESM(require("./ComponentBuilder.js")); class SchemaBuilder { #schema; #name; /** * @param {string} name - The name of the schema */ constructor(name) { this.#name = name; this.#schema = new import_Schema.default(name); } /** * The {@link Schema} current schema instance. * * @public * @returns {Schema} */ get schema() { return this.#schema; } /** * Adds a new {@link Field} to the schema and returns the current instance. * * @public * @param {string} name - The name of the new field. * @param {DataType} dataType - The type of the new field. * @param {boolean=} optional - If true, the field is not required and may be omitted. * @returns {SchemaBuilder} */ withField(name, dataType, optional) { assert.argumentIsRequired(name, "name", String); assert.argumentIsRequired(dataType, "dataType", import_DataType.default, "DataType"); assert.argumentIsOptional(optional, "optional", Boolean); const fields = this.#schema.fields.concat([new import_Field.default(name, dataType, optional, false)]); this.#schema = new import_Schema.default(this.#schema.name, fields, this.#schema.components, this.#schema.strict); return this; } /** * Adds a new {@link Field} to the schema (where the field is an array) and returns the current instance. * * @public * @param {string} name - The name of the new field. * @param {DataType} dataType - The type of the new field. * @param {boolean=} optional - If true, the field is not required and may be omitted. * @returns {SchemaBuilder} */ withArray(name, dataType, optional) { assert.argumentIsRequired(name, "name", String); assert.argumentIsRequired(dataType, "dataType", import_DataType.default, "DataType"); assert.argumentIsOptional(optional, "optional", Boolean); const fields = this.#schema.fields.concat([new import_Field.default(name, dataType, optional, true)]); this.#schema = new import_Schema.default(this.#schema.name, fields, this.#schema.components, this.#schema.strict); return this; } /** * Adds a new {@link Component} to the schema and returns the current instance. * * @public * @param {Component} component - The new component to add. * @returns {SchemaBuilder} */ withComponent(component) { assert.argumentIsRequired(component, "component", import_Component.default, "Component"); const components = this.#schema.components.concat([component]); this.#schema = new import_Schema.default(this.#schema.name, this.#schema.fields, components, this.#schema.strict); return this; } /** * Adds a new {@link Component} to the schema, using a {@link ComponentBuilder} * and returns the current instance. * * @public * @param {string} name - The name of the new component. * @param {Function} callback - A callback to which the {@link ComponentBuilder} is passed synchronously. * @returns {SchemaBuilder} */ withComponentBuilder(name, callback) { assert.argumentIsRequired(name, "name", String); const componentBuilder = new import_ComponentBuilder.default(name); callback(componentBuilder); return this.withComponent(componentBuilder.component); } /** * Creates a new {@link SchemaBuilder}. * * @public * @static * @param {string} name * @returns {SchemaBuilder} */ static withName(name) { assert.argumentIsRequired(name, "name", String); return new SchemaBuilder(name); } /** * Returns a string representation. * * @public * @returns {string} */ toString() { return `[SchemaBuilder (name=${this.#name})]`; } } { const cjsExports = module.exports; const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports; if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) { Object.keys(cjsExports).forEach((key) => { if (key !== 'default' && key !== '__esModule') { cjsDefaultExport[key] = cjsExports[key]; } }); } module.exports = cjsDefaultExport; }