mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-15 17:54:52 +08:00
Small refactoring
This commit is contained in:
@@ -1,28 +1,48 @@
|
||||
import P from "parsernostrum"
|
||||
import Utility from "../Utility.js"
|
||||
import IEntity from "./IEntity.js"
|
||||
|
||||
export default class StringEntity extends IEntity {
|
||||
|
||||
static grammar = this.createGrammar()
|
||||
static escapedCharacters = /['"\\]/g
|
||||
static unescapedBackslash = /(?<=(?:[^\\]|^)(?:\\\\)*)\\(?!\\)/
|
||||
|
||||
constructor(value = "") {
|
||||
super()
|
||||
this.value = value
|
||||
}
|
||||
|
||||
/** @returns {P<StringEntity>} */
|
||||
static createGrammar() {
|
||||
return /** @type {P<StringEntity>} */(
|
||||
P.doubleQuotedString
|
||||
.map(insideString => new this(Utility.unescapeString(insideString)))
|
||||
.label("StringEntity")
|
||||
)
|
||||
return P.doubleQuotedString
|
||||
.map(insideString => new this(StringEntity.unescape(insideString)))
|
||||
.label("StringEntity")
|
||||
}
|
||||
|
||||
|
||||
/** @param {String} value */
|
||||
static escape(value, inline = true) {
|
||||
let result = value.replaceAll(new RegExp(`(${StringEntity.escapedCharacters.source})`, "g"), '\\$1')
|
||||
if (inline) {
|
||||
result = result
|
||||
.replaceAll("\n", "\\n") // Replace newline with \n
|
||||
.replaceAll("\t", "\\t") // Replace tab with \t
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/** @param {String} value */
|
||||
static unescape(value) {
|
||||
return value
|
||||
.replaceAll(new RegExp(StringEntity.unescapedBackslash.source + "t", "g"), "\t") // Replace tab with \t
|
||||
.replaceAll(new RegExp(StringEntity.unescapedBackslash.source + "n", "g"), "\n") // Replace newline with \n
|
||||
.replaceAll(new RegExp(`\\\\(${StringEntity.escapedCharacters.source})`, "g"), "$1")
|
||||
}
|
||||
|
||||
doSerialize(insideString = false) {
|
||||
let result = `"${Utility.escapeString(this.value)}"`
|
||||
let result = `"${StringEntity.escape(this.value)}"`
|
||||
if (insideString) {
|
||||
result = Utility.escapeString(result, false)
|
||||
result = StringEntity.escape(result, false)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user