From 59eaa5721745402e580a6d5f35a1b2315479389f Mon Sep 17 00:00:00 2001 From: barsdeveloper Date: Wed, 4 May 2022 21:28:03 +0200 Subject: [PATCH] Minor bug fixes --- dist/ueblueprint.js | 162 +++++++++++++++++--------------- js/Utility.js | 2 +- js/element/PinElement.js | 8 +- js/entity/TypeInitialization.js | 38 +++++--- js/serialization/Grammar.js | 2 +- js/template/RealPinTemplate.js | 4 +- 6 files changed, 122 insertions(+), 94 deletions(-) diff --git a/dist/ueblueprint.js b/dist/ueblueprint.js index 1ba28c6..6c8ae3c 100755 --- a/dist/ueblueprint.js +++ b/dist/ueblueprint.js @@ -214,76 +214,6 @@ class IInput { // @ts-check -/** - * @template T - */ -class TypeInitialization { - - /** @type {T} */ - #value - get value() { - return this.#value - } - set value(v) { - this.#value = v; - } - - #showDefault = true - get showDefault() { - return this.#showDefault - } - set showDefault(v) { - this.#showDefault = v; - } - - /** @type {Constructor|Array} */ - #type - get type() { - return this.#type - } - set type(v) { - this.#type = v; - } - - static sanitize(value, targetType) { - if (targetType === undefined) { - targetType = value?.constructor; - } - let wrongType = false; - if (targetType && value?.constructor !== targetType && !(value instanceof targetType)) { - wrongType = true; - } - if (value instanceof Boolean || value instanceof Number || value instanceof String) { - value = value.valueOf(); // Get the relative primitive value - } - if (wrongType) { - return new targetType(value) - } - return value - } - - /** - * @typedef {new () => T} Constructor - * @param {Constructor|Array} type - * @param {Boolean} showDefault - * @param {any} value - */ - constructor(type, showDefault = true, value = undefined) { - if (value === undefined) { - if (type instanceof Array) { - value = []; - } else { - value = TypeInitialization.sanitize(new type()); - } - } - this.#value = value; - this.#showDefault = showDefault; - this.#type = type; - } -} - -// @ts-check - class Utility { static sigmoid(x, curvature = 1.7) { @@ -485,13 +415,91 @@ class Utility { static formatStringName(value) { return value .trim() - .replace(/(?} */ + #type + get type() { + return this.#type + } + set type(v) { + this.#type = v; + } + + #showDefault = true + get showDefault() { + return this.#showDefault + } + set showDefault(v) { + this.#showDefault = v; + } + + /** @type {T} */ + #value + get value() { + return this.#value + } + set value(v) { + this.#value = v; + } + + #decodeString + get decodeString() { + return this.#decodeString + } + set decodeString(v) { + this.#decodeString = v; + } + + static sanitize(value, targetType) { + if (targetType === undefined) { + targetType = value?.constructor; + } + let wrongType = false; + if (targetType && value?.constructor !== targetType && !(value instanceof targetType)) { + wrongType = true; + } + if (value instanceof Boolean || value instanceof Number || value instanceof String) { + value = value.valueOf(); // Get the relative primitive value + } + if (wrongType) { + return new targetType(value) + } + return value + } + + /** + * @typedef {(new () => T) | StringConstructor | NumberConstructor | BooleanConstructor} Constructor + * @param {Constructor|Array} type + * @param {Boolean} showDefault + * @param {any} value + */ + constructor(type, showDefault = true, value = undefined) { + if (value === undefined) { + if (type instanceof Array) { + value = []; + } else { + value = TypeInitialization.sanitize(new type()); + } + } + this.#value = type === String ? Utility.decodeString(value) : value; + this.#showDefault = showDefault; + this.#type = type; + } +} + +// @ts-check + class IEntity { static attributes = {} @@ -1139,7 +1147,7 @@ class Grammar { Word = r => P.regex(/[a-zA-Z]+/).desc("a word") - String = r => P.regex(/(?:[^"\\]|\\.)*/).wrap(P.string('"'), P.string('"')) + String = r => P.regex(/(?:[^"\\]|\\.)*/).wrap(P.string('"'), P.string('"')).map(Utility.decodeString) .desc('string (with possibility to escape the quote using \")') ReferencePath = r => P.seq( @@ -3144,7 +3152,7 @@ class RealPinTemplate extends IInputPinTemplate { setInputs(pin, values = []) { let num = parseFloat(values.length ? values[0] : this.getInput(pin)); if (isNaN(num)) { - num = parseFloat(pin.entity.AutogeneratedDefaultValue); + num = parseFloat(pin.entity.DefaultValue != "" ? pin.entity.DefaultValue : pin.entity.AutogeneratedDefaultValue); } values[0] = Utility.minDecimals(num); super.setInputs(pin, values); @@ -3234,8 +3242,12 @@ class PinElement extends IElement { * @returns {String} */ getPinDisplayName() { - if (this.entity.PinToolTip) { - const matchResult = this.entity.PinToolTip.match(/\s*(.+?(?=\\n)|.+\S)\s*/); // Match up until the first \n + let matchResult = null; + if ( + this.entity.PinToolTip + // Match up until the first \n excluded or last character + && (matchResult = this.entity.PinToolTip.match(/\s*(.+?(?=\n)|.+\S)\s*/)) + ) { return Utility.formatStringName(matchResult[1]) } return Utility.formatStringName(this.entity.PinName) diff --git a/js/Utility.js b/js/Utility.js index de9ebfe..eca2af7 100755 --- a/js/Utility.js +++ b/js/Utility.js @@ -203,7 +203,7 @@ export default class Utility { static formatStringName(value) { return value .trim() - .replace(/(?} */ + #type + get type() { + return this.#type } - set value(v) { - this.#value = v + set type(v) { + this.#type = v } #showDefault = true @@ -22,13 +24,21 @@ export default class TypeInitialization { this.#showDefault = v } - /** @type {Constructor|Array} */ - #type - get type() { - return this.#type + /** @type {T} */ + #value + get value() { + return this.#value } - set type(v) { - this.#type = v + set value(v) { + this.#value = v + } + + #decodeString + get decodeString() { + return this.#decodeString + } + set decodeString(v) { + this.#decodeString = v } static sanitize(value, targetType) { @@ -49,7 +59,7 @@ export default class TypeInitialization { } /** - * @typedef {new () => T} Constructor + * @typedef {(new () => T) | StringConstructor | NumberConstructor | BooleanConstructor} Constructor * @param {Constructor|Array} type * @param {Boolean} showDefault * @param {any} value @@ -62,7 +72,7 @@ export default class TypeInitialization { value = TypeInitialization.sanitize(new type()) } } - this.#value = value + this.#value = type === String ? Utility.decodeString(value) : value this.#showDefault = showDefault this.#type = type } diff --git a/js/serialization/Grammar.js b/js/serialization/Grammar.js index 335543f..90e8195 100755 --- a/js/serialization/Grammar.js +++ b/js/serialization/Grammar.js @@ -129,7 +129,7 @@ export default class Grammar { Word = r => P.regex(/[a-zA-Z]+/).desc("a word") - String = r => P.regex(/(?:[^"\\]|\\.)*/).wrap(P.string('"'), P.string('"')) + String = r => P.regex(/(?:[^"\\]|\\.)*/).wrap(P.string('"'), P.string('"')).map(Utility.decodeString) .desc('string (with possibility to escape the quote using \")') ReferencePath = r => P.seq( diff --git a/js/template/RealPinTemplate.js b/js/template/RealPinTemplate.js index 0d01a11..7da2960 100644 --- a/js/template/RealPinTemplate.js +++ b/js/template/RealPinTemplate.js @@ -16,7 +16,9 @@ export default class RealPinTemplate extends IInputPinTemplate { setInputs(pin, values = []) { let num = parseFloat(values.length ? values[0] : this.getInput(pin)) if (isNaN(num)) { - num = parseFloat(pin.entity.AutogeneratedDefaultValue) + num = parseFloat(pin.entity.DefaultValue != "" + ? pin.entity.DefaultValue + : pin.entity.AutogeneratedDefaultValue) } values[0] = Utility.minDecimals(num) super.setInputs(pin, values)