mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-03-06 23:57:30 +08:00
Tests for various entity classes and update entity class implementations
This commit is contained in:
@@ -28,7 +28,9 @@ export default class AlternativesEntity extends IEntity {
|
||||
* @param {Types} types
|
||||
*/
|
||||
static accepting(...types) {
|
||||
const result = /** @type {typeof AlternativesEntity & { alternatives: Types }} */(this.asUniqueClass())
|
||||
const result = /** @type {typeof AlternativesEntity & { alternatives: Types }} */(
|
||||
this.asUniqueClass()
|
||||
)
|
||||
result.alternatives = types
|
||||
result.grammar = result.createGrammar()
|
||||
return result
|
||||
|
||||
@@ -2,19 +2,20 @@ import P from "parsernostrum"
|
||||
import Grammar from "../serialization/Grammar.js"
|
||||
import IEntity from "./IEntity.js"
|
||||
|
||||
/** @template {typeof IEntity} T */
|
||||
/** @template {IEntity} T */
|
||||
export default class ArrayEntity extends IEntity {
|
||||
|
||||
/** @type {typeof IEntity} */
|
||||
static type
|
||||
static grammar = this.createGrammar()
|
||||
|
||||
/** @param {ExtractType<T>[]} values */
|
||||
/** @param {T[]} values */
|
||||
constructor(values = []) {
|
||||
super()
|
||||
this.values = values
|
||||
}
|
||||
|
||||
/** @returns {P<ArrayEntity<IEntity>>} */
|
||||
static createGrammar(elementGrammar = this.type?.grammar ?? P.lazy(() => Grammar.unknownValue)) {
|
||||
return this.inlined
|
||||
? elementGrammar
|
||||
@@ -35,10 +36,10 @@ export default class ArrayEntity extends IEntity {
|
||||
|
||||
/**
|
||||
* @template {typeof IEntity} T
|
||||
* @param {NonNullable<T>} type
|
||||
* @param {T} type
|
||||
*/
|
||||
static of(type) {
|
||||
const result = /** @type {typeof ArrayEntity<T> & { type: ExtractType<T>, grammar: P<ArrayEntity<T>> }} */(
|
||||
const result = /** @type {{type: T, grammar: P<ArrayEntity<ExtractType<T>>> } & typeof ArrayEntity<ExtractType<T>>} */(
|
||||
this.asUniqueClass()
|
||||
)
|
||||
result.type = /** @type {ExtractType<T>} */(type)
|
||||
@@ -49,4 +50,16 @@ export default class ArrayEntity extends IEntity {
|
||||
valueOf() {
|
||||
return this.values
|
||||
}
|
||||
|
||||
toString(
|
||||
insideString = false,
|
||||
indentation = "",
|
||||
printKey = this.Self().printKey,
|
||||
) {
|
||||
let result = this.values.map(v => v?.toString()).join(this.Self().attributeSeparator)
|
||||
if (this.Self().trailing) {
|
||||
result += this.Self().attributeSeparator
|
||||
}
|
||||
return `(${result})`
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ export default class ByteEntity extends IntegerEntity {
|
||||
|
||||
static grammar = P.numberByte.map(v => new this(v))
|
||||
|
||||
get value() {
|
||||
return super.value
|
||||
}
|
||||
set value(value) {
|
||||
if (value % 1 == 0 && value >= 0 && value < 1 << 8) {
|
||||
super.value = value
|
||||
|
||||
@@ -6,6 +6,7 @@ import StringEntity from "./StringEntity.js"
|
||||
|
||||
export default class FormatTextEntity extends IPrintableEntity {
|
||||
|
||||
static attributeSeparator = ", "
|
||||
static lookbehind = ["LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED"]
|
||||
/** @type {P<FormatTextEntity>} */
|
||||
static grammar = P.lazy(() => P.seq(
|
||||
@@ -53,4 +54,16 @@ export default class FormatTextEntity extends IPrintableEntity {
|
||||
: ""
|
||||
return result
|
||||
}
|
||||
|
||||
toString(
|
||||
insideString = false,
|
||||
indentation = "",
|
||||
printKey = this.Self().printKey,
|
||||
) {
|
||||
const separator = this.Self().attributeSeparator
|
||||
return this.lookbehind + "("
|
||||
+ this.values.map(v => v.toString(insideString)).join(separator)
|
||||
+ (this.Self().trailing ? separator : "")
|
||||
+ ")"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ export default class IEntity {
|
||||
static wrap = this.defaultWrapped
|
||||
|
||||
static attributeSeparator = ","
|
||||
static keySeparator = "="
|
||||
|
||||
/** @type {(k: String) => String} */
|
||||
static printKey = k => k
|
||||
@@ -103,7 +104,6 @@ export default class IEntity {
|
||||
*/
|
||||
static asUniqueClass() {
|
||||
if (this.name.length) {
|
||||
// @ts-expect-error
|
||||
return class extends this { }
|
||||
}
|
||||
return this
|
||||
@@ -281,9 +281,9 @@ export default class IEntity {
|
||||
if (Self.quoted) {
|
||||
keyValue = `"${keyValue}"`
|
||||
}
|
||||
result += Self.attributeSeparator.includes("\n") ? indentation : ""
|
||||
result += (Self.attributeSeparator.includes("\n") ? indentation : "") + keyValue + Self.keySeparator
|
||||
}
|
||||
let serialization = value.toString(insideString, indentation, printKey)
|
||||
let serialization = value?.toString(insideString, indentation, printKey)
|
||||
if (Self.serialized) {
|
||||
serialization = `"${serialization.replaceAll(/(?<=(?:[^\\]|^)(?:\\\\)*?)"/, '\\"')}"`
|
||||
}
|
||||
|
||||
@@ -5,8 +5,11 @@ export default class IntegerEntity extends NumberEntity {
|
||||
|
||||
static grammar = P.numberInteger.map(v => new this(v))
|
||||
|
||||
get value() {
|
||||
return super.value
|
||||
}
|
||||
set value(value) {
|
||||
if (value >= -(1 << 31) && value < 1 << 31) {
|
||||
if (value >= 1 << 31 && value < -(1 << 31)) {
|
||||
value = Math.floor(value)
|
||||
super.value = value
|
||||
}
|
||||
|
||||
@@ -2,64 +2,44 @@ import P from "parsernostrum"
|
||||
import Utility from "../Utility.js"
|
||||
import Grammar from "../serialization/Grammar.js"
|
||||
import IPrintableEntity from "./IPrintableEntity.js"
|
||||
import StringEntity from "./StringEntity.js"
|
||||
|
||||
export default class LocalizedTextEntity extends IPrintableEntity {
|
||||
|
||||
static attributeSeparator = ", "
|
||||
static printKey = k => ""
|
||||
static lookbehind = "NSLOCTEXT"
|
||||
static attributes = {
|
||||
...super.attributes,
|
||||
namespace: StringEntity.withDefault(),
|
||||
key: StringEntity.withDefault(),
|
||||
value: StringEntity.withDefault(),
|
||||
}
|
||||
static grammar = P.regArray(new RegExp(
|
||||
String.raw`${this.lookbehind}\s*\(`
|
||||
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*,`
|
||||
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*,`
|
||||
+ String.raw`\s*"(${Grammar.Regex.InsideString.source})"\s*`
|
||||
+ String.raw`(,\s+)?`
|
||||
String.raw`${LocalizedTextEntity.lookbehind}\s*\(`
|
||||
+ String.raw`\s*"(?<namespace>${Grammar.Regex.InsideString.source})"\s*,`
|
||||
+ String.raw`\s*"(?<key>${Grammar.Regex.InsideString.source})"\s*,`
|
||||
+ String.raw`\s*"(?<value>${Grammar.Regex.InsideString.source})"\s*`
|
||||
+ String.raw`(?<trailing>,\s+)?`
|
||||
+ String.raw`\)`,
|
||||
"m"
|
||||
)).map(matchResult => {
|
||||
const self = matchResult[4] ? this.flagTrailing() : this
|
||||
return new self(
|
||||
Utility.unescapeString(matchResult[1]),
|
||||
Utility.unescapeString(matchResult[2]),
|
||||
Utility.unescapeString(matchResult[3]),
|
||||
)
|
||||
)).map(({ groups: { namespace, key, value, trailing } }) => {
|
||||
const self = trailing ? this.flagTrailing() : this
|
||||
return new self({
|
||||
namespace: new (this.attributes.namespace)(Utility.unescapeString(namespace)),
|
||||
key: new (this.attributes.namespace)(Utility.unescapeString(key)),
|
||||
value: new (this.attributes.namespace)(Utility.unescapeString(value)),
|
||||
})
|
||||
}).label("LocalizedTextEntity")
|
||||
|
||||
#namespace
|
||||
get namespace() {
|
||||
return this.#namespace
|
||||
}
|
||||
set namespace(value) {
|
||||
this.#namespace = value
|
||||
}
|
||||
|
||||
#key
|
||||
get key() {
|
||||
return this.#key
|
||||
}
|
||||
set key(value) {
|
||||
this.#key = value
|
||||
}
|
||||
|
||||
#value
|
||||
get value() {
|
||||
return this.#value
|
||||
}
|
||||
set value(value) {
|
||||
this.#value = value
|
||||
}
|
||||
|
||||
constructor(namespace = "", key = "", value = "") {
|
||||
super()
|
||||
this.namespace = namespace
|
||||
this.key = key
|
||||
this.value = value
|
||||
constructor(values = {}) {
|
||||
super(values)
|
||||
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.namespace>} */ this.namespace
|
||||
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.key>} */ this.key
|
||||
/** @type {InstanceType<typeof LocalizedTextEntity.attributes.value>} */ this.value
|
||||
}
|
||||
|
||||
print() {
|
||||
return Utility.capitalFirstLetter(this.value)
|
||||
}
|
||||
|
||||
toString() {
|
||||
const trailer = this.Self().trailing ? ", " : ""
|
||||
return `${this.lookbehind}(${this.namespace}, ${this.key}, ${this.value}${trailer})`
|
||||
return Utility.capitalFirstLetter(this.value.valueOf())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,11 @@ export default class NaturalNumberEntity extends IntegerEntity {
|
||||
|
||||
static grammar = P.numberNatural.map(v => new this(v))
|
||||
|
||||
get value() {
|
||||
return super.value
|
||||
}
|
||||
set value(value) {
|
||||
value = Math.round(Utility.clamp(this.value, 0))
|
||||
super.value = value
|
||||
}
|
||||
|
||||
constructor(value = 0) {
|
||||
super(value)
|
||||
}
|
||||
}
|
||||
|
||||
16
js/entity/NullEntity.js
Normal file
16
js/entity/NullEntity.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import P from "parsernostrum"
|
||||
import IEntity from "./IEntity.js"
|
||||
|
||||
export default class NullEntity extends IEntity {
|
||||
|
||||
static grammar = P.reg(new RegExp(String.raw`\(${P.whitespaceInlineOpt.getParser().regexp.source}\)`))
|
||||
.map(v => new this())
|
||||
|
||||
toString(
|
||||
insideString = false,
|
||||
indentation = "",
|
||||
printKey = this.Self().printKey,
|
||||
) {
|
||||
return "()"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user