Minor bug fixes

This commit is contained in:
barsdeveloper
2022-05-04 21:28:03 +02:00
parent 5f4b35427e
commit 59eaa57217
6 changed files with 122 additions and 94 deletions

162
dist/ueblueprint.js vendored
View File

@@ -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<Constructor>} */
#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<Constructor>} 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(/(?<!\n)^(b|\n+|(?:\\r\\n)+|(?:\\n)+)/, "") // Remove leading b (for boolean values) or newlines
.replace(/^b/, "") // Remove leading b (for boolean values) or newlines
.replaceAll(/(?<=[a-z])(?=[A-Z])|_|\s+/g, " ") // Insert a space between a lowercase and uppercase letter, instead of an underscore or multiple spaces
}
}
// @ts-check
/**
* @template T
*/
class TypeInitialization {
/** @type {Constructor|Array<Constructor>} */
#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<Constructor>} 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)

View File

@@ -203,7 +203,7 @@ export default class Utility {
static formatStringName(value) {
return value
.trim()
.replace(/(?<!\n)^(b|\n+|(?:\\r\\n)+|(?:\\n)+)/, "") // Remove leading b (for boolean values) or newlines
.replace(/^b/, "") // Remove leading b (for boolean values) or newlines
.replaceAll(/(?<=[a-z])(?=[A-Z])|_|\s+/g, " ") // Insert a space between a lowercase and uppercase letter, instead of an underscore or multiple spaces
}
}

View File

@@ -74,8 +74,12 @@ export default 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)

View File

@@ -1,17 +1,19 @@
// @ts-check
import Utility from "../Utility"
/**
* @template T
*/
export default class TypeInitialization {
/** @type {T} */
#value
get value() {
return this.#value
/** @type {Constructor|Array<Constructor>} */
#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<Constructor>} */
#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<Constructor>} 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
}

View File

@@ -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(

View File

@@ -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)