UNPKG

@platform/cell.typesystem

Version:

The 'strongly typed sheets' system of the CellOS.

79 lines (78 loc) 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var common_1 = require("../common"); var TypeSystem_core_1 = require("../TypeSystem.core"); var TypeBuilderProp = (function () { function TypeBuilderProp(args) { this._column = 0; this.column(args.column) .name(args.name) .type(common_1.defaultValue(args.type, 'string')) .target(args.target) .default(args.default); this._onChange = args.onChange; } Object.defineProperty(TypeBuilderProp.prototype, "columnKey", { get: function () { return common_1.coord.cell.toColumnKey(this._column) || 'A'; }, enumerable: true, configurable: true }); TypeBuilderProp.prototype.toObject = function () { return common_1.value.deleteUndefined({ column: this.columnKey, name: this._name, type: this._type, target: this._target, default: this._default, }); }; TypeBuilderProp.prototype.column = function (value) { var index = typeof value === 'number' ? value : common_1.coord.cell.toAxisIndex('COLUMN', value); if (index < 0) { var err = "The column \"" + value + "\" is invalid."; throw new Error(err); } this._column = index; return this.fireChange('column', this.columnKey); }; TypeBuilderProp.prototype.name = function (value) { this._name = value; return this.fireChange('name', value); }; TypeBuilderProp.prototype.type = function (value) { value = (value || '').trim(); if (!value.startsWith('/')) { var parsed = TypeSystem_core_1.TypeValue.parse(value); if (parsed.type.kind === 'UNKNOWN') { var err = "The type '" + value + "' is UNKNOWN"; throw new Error(err); } } this._type = value; return this.fireChange('type', value); }; TypeBuilderProp.prototype.target = function (value) { var errors = TypeSystem_core_1.TypeTarget.parse(value).errors; if (errors.length > 0) { var message = errors.map(function (err) { return err.message; }).join('\n'); throw new Error(message); } this._target = value; return this.fireChange('target', value); }; TypeBuilderProp.prototype.default = function (value) { this._default = value; return this.fireChange('default', value); }; TypeBuilderProp.prototype.fireChange = function (prop, value) { if (this._onChange) { this._onChange({ prop: prop, value: value }); } return this; }; TypeBuilderProp.create = function (args) { return new TypeBuilderProp(args); }; return TypeBuilderProp; }()); exports.TypeBuilderProp = TypeBuilderProp;