UNPKG

@opra/common

Version:
68 lines (67 loc) 2.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataType = void 0; const objects_1 = require("@jsopen/objects"); const ts_gems_1 = require("ts-gems"); const document_element_js_1 = require("../common/document-element.js"); const constants_js_1 = require("../constants.js"); const inspect_util_js_1 = require("../utils/inspect.util.js"); const test_scope_match_js_1 = require("../utils/test-scope-match.js"); /** * DataType constructor */ exports.DataType = function (owner, initArgs, // eslint-disable-next-line @typescript-eslint/no-unused-vars context) { if (!this) throw new TypeError('"this" should be passed to call class constructor'); if (initArgs?.name && !constants_js_1.CLASS_NAME_PATTERN.test(initArgs.name)) { throw new TypeError(`"${initArgs.name}" is not a valid DataType name`); } document_element_js_1.DocumentElement.call(this, owner); const _this = (0, ts_gems_1.asMutable)(this); _this.kind = initArgs.kind; _this.scopePattern = initArgs.scopePattern ? Array.isArray(initArgs.scopePattern) ? initArgs.scopePattern : [initArgs.scopePattern] : undefined; _this.name = initArgs.name; _this.description = initArgs.description; _this.abstract = initArgs.abstract; _this.examples = initArgs.examples; }; /** * * @class DataType */ class DataTypeClass extends document_element_js_1.DocumentElement { get embedded() { return !this.name; } inScope(scope) { return (0, test_scope_match_js_1.testScopeMatch)(scope, this.scopePattern); } toJSON(options) { /** Locate base model which is not available for given scope */ const outOfScope = this._locateBase(dt => !dt.inScope(options?.scope)); if (outOfScope) { const baseName = this.node.getDataTypeNameWithNs(outOfScope); throw new TypeError(`"${baseName}" model is not available for "${options?.scope || 'null'}" scope`); } return (0, objects_1.omitUndefined)({ kind: this.kind, description: this.description, abstract: this.abstract, examples: this.examples, }); } toString() { return `[${Object.getPrototypeOf(this).constructor.name} ${this.name || '#Embedded'}]`; } [inspect_util_js_1.nodeInspectCustom]() { return (`[${inspect_util_js_1.colorFgYellow + Object.getPrototypeOf(this).constructor.name + inspect_util_js_1.colorReset}` + ` ${inspect_util_js_1.colorFgMagenta + this.name + inspect_util_js_1.colorReset}]`); } } exports.DataType.prototype = DataTypeClass.prototype;