Various improvements (#13)

* Union lookbehind

* Fix quoted inline array
This commit is contained in:
barsdeveloper
2023-09-02 14:08:29 +02:00
committed by GitHub
parent 11f819e6d9
commit fd991b94b3
17 changed files with 313 additions and 144 deletions

View File

@@ -1,14 +1,14 @@
import IEntity from "./IEntity.js"
import InvariantTextEntity from "./InvariantTextEntity.js"
import LocalizedTextEntity from "./LocalizedTextEntity.js"
import UnionType from "./UnionType.js"
import Union from "./Union.js"
export default class FormatTextEntity extends IEntity {
static lookbehind = "LOCGEN_FORMAT_NAMED"
static lookbehind = new Union("LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED")
static attributes = {
value: {
type: [new UnionType(String, LocalizedTextEntity, InvariantTextEntity, FormatTextEntity)],
type: [new Union(String, LocalizedTextEntity, InvariantTextEntity, FormatTextEntity)],
default: [],
},
}
@@ -28,11 +28,20 @@ export default class FormatTextEntity extends IEntity {
return ""
}
const values = this.value.slice(1).map(v => v.toString())
return pattern.replaceAll(/\{([a-zA-Z]\w*)\}/g, (substring, arg) => {
const argLocation = values.indexOf(arg) + 1
return argLocation > 0 && argLocation < values.length
? values[argLocation]
: substring
})
return this.lookbehind == "LOCGEN_FORMAT_NAMED"
? pattern.replaceAll(/\{([a-zA-Z]\w*)\}/g, (substring, arg) => {
const argLocation = values.indexOf(arg) + 1
return argLocation > 0 && argLocation < values.length
? values[argLocation]
: substring
})
: this.lookbehind == "LOCGEN_FORMAT_ORDERED"
? pattern.replaceAll(/\{(\d+)\}/g, (substring, arg) => {
const argValue = Number(arg)
return argValue < values.length
? values[argValue]
: substring
})
: ""
}
}