Entities cleanup, Primitive concept introduced

This commit is contained in:
barsdeveloper
2021-10-25 15:49:09 +02:00
parent bee57a1402
commit 92828705d3
21 changed files with 292 additions and 284 deletions

View File

@@ -0,0 +1,32 @@
import Primitive from "./Primitive";
export default class Guid extends Primitive {
static generateGuid(random) {
let values = new Uint32Array(4);
if (random === true) {
crypto.getRandomValues(values)
}
let result = ""
values.forEach(n => {
result += ('00000000' + n.toString(16).toUpperCase()).slice(-8)
})
return result
}
constructor(guid) {
super()
// Using constructor equality and not instanceof in order to consider both primitives and objects
if (guid?.constructor === Boolean) {
guid = Guid.generateGuid(guid == true)
}
if (guid instanceof Guid) {
guid = guid.value
}
this.value = guid
}
toString() {
return this.value.toString()
}
}

View File

@@ -0,0 +1,25 @@
import Primitive from "./Primitive"
export default class Integer extends Primitive {
constructor(value) {
super()
// Using constructor equality and not instanceof in order to consider both primitives and objects
if (value?.constructor === String) {
value = Number(value)
}
if (value?.constructor === Number) {
value = Math.round(value)
}
/** @type {number} */
this.value = value
}
valueOf() {
this.value
}
toString() {
return this.value.toString()
}
}

View File

@@ -0,0 +1,22 @@
import Primitive from "./Primitive"
export default class LocalizedTextEntity extends Primitive {
/**
*
* @param {String} namespace
* @param {String} key
* @param {String} value
*/
constructor(namespace, key, value) {
super()
this.namespace = namespace
this.key = key
this.value = value
}
toString() {
"NSLOCTEXT(" + `"${this.namespace}"` + ", " + `"${this.key}"` + ", " + `"${this.value}"` + ")"
}
}

View File

@@ -0,0 +1,23 @@
import Primitive from "./Primitive"
export default class ObjectReference extends Primitive {
/**
*
* @param {String} type
* @param {String} path
*/
constructor(type, path) {
super()
this.type = type
this.path = path
}
toString() {
return (this.type ?? "") + (
this.path
? this.type ? `'"${this.path}"'` : this.path
: ""
)
}
}

View File

@@ -0,0 +1,5 @@
export default class Primitive {
toString() {
return "Unimplemented for " + this.constructor.name
}
}