More entities fixed

This commit is contained in:
barsdeveloper
2024-06-04 01:00:41 +02:00
parent 1a8636bb5d
commit c05e6d3cc9
15 changed files with 913 additions and 456 deletions

View File

@@ -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()
}
}