mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-27 02:34:45 +08:00
More entities fixed
This commit is contained in:
@@ -4,40 +4,59 @@ import IEntity from "./IEntity.js"
|
||||
|
||||
export default class NumberEntity extends IEntity {
|
||||
|
||||
static numberRegexSource = String.raw`${Grammar.numberRegexSource}(?<=(?:\.(\d*0+))?)`
|
||||
static grammar = P.regArray(
|
||||
new RegExp(`(${Grammar.numberRegexSource})|(\\+?inf)|(-inf)`)
|
||||
).map(([_0, n, plusInf, minusInf]) => new this(
|
||||
n ? Number(n) : plusInf ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY
|
||||
)).label("NumberEntity")
|
||||
new RegExp(`(?<n>${this.numberRegexSource})|(?<posInf>\\+?inf)|(?<negInf>-inf)`)
|
||||
).map(({ 2: precision, groups: { n, posInf, negInf } }) => new this(
|
||||
n ? Number(n) : posInf ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY,
|
||||
precision?.length
|
||||
)
|
||||
).label("NumberEntity")
|
||||
|
||||
/** @type {Number} */
|
||||
#value
|
||||
#precision = 0
|
||||
get precision() {
|
||||
return this.#precision
|
||||
}
|
||||
set precision(value) {
|
||||
this.#precision = value
|
||||
}
|
||||
|
||||
/**
|
||||
* @protected
|
||||
* @type {Number}
|
||||
*/
|
||||
_value
|
||||
get value() {
|
||||
return this.#value
|
||||
return this._value
|
||||
}
|
||||
set value(value) {
|
||||
if (value === -0) {
|
||||
value = 0
|
||||
}
|
||||
this.#value = value
|
||||
this._value = value
|
||||
}
|
||||
|
||||
constructor(value = 0) {
|
||||
constructor(value = 0, precision = 0) {
|
||||
super()
|
||||
this.value = value
|
||||
this.value = Number(value)
|
||||
this.#precision = Number(precision)
|
||||
}
|
||||
|
||||
valueOf() {
|
||||
return this.value
|
||||
}
|
||||
|
||||
toString() {
|
||||
toString(
|
||||
insideString = false,
|
||||
indentation = "",
|
||||
printKey = this.Self().printKey,
|
||||
) {
|
||||
if (this.value === Number.POSITIVE_INFINITY) {
|
||||
return "+inf"
|
||||
}
|
||||
if (this.value === Number.NEGATIVE_INFINITY) {
|
||||
return "-inf"
|
||||
}
|
||||
return this.value.toString()
|
||||
return this.#precision ? this.value.toFixed(this.#precision) : this.value.toString()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user