mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-03-06 23:57:30 +08:00
Integer => IntegerEntity, No more primitive
This commit is contained in:
21
js/entity/IntegerEntity.js
Executable file
21
js/entity/IntegerEntity.js
Executable file
@@ -0,0 +1,21 @@
|
||||
import Entity from "./Entity"
|
||||
|
||||
export default class IntegerEntity extends Entity {
|
||||
|
||||
static attributes = {
|
||||
value: Number
|
||||
}
|
||||
|
||||
getAttributes() {
|
||||
return IntegerEntity.attributes
|
||||
}
|
||||
|
||||
constructor(options = {}) {
|
||||
super(options)
|
||||
this.value = Math.round(value)
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.value.toString()
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,12 @@
|
||||
import Integer from "./primitive/Integer"
|
||||
|
||||
export default class TypeInitialization {
|
||||
|
||||
static sanitize(value) {
|
||||
if (!(value instanceof Object)) {
|
||||
return value // Is already primitive
|
||||
}
|
||||
if (value instanceof Boolean || value instanceof Integer || value instanceof Number) {
|
||||
if (value instanceof Boolean || value instanceof Number || value instanceof String) {
|
||||
return value.valueOf()
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return value.toString()
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import Primitive from "./Primitive"
|
||||
|
||||
export default class Integer extends Primitive {
|
||||
|
||||
constructor(value) {
|
||||
super()
|
||||
// Using constructor equality and not instanceof in order to consider both primitives and objects
|
||||
if (value?.constructor === String) {
|
||||
value = Number(value)
|
||||
}
|
||||
if (value?.constructor === Number) {
|
||||
value = Math.round(value)
|
||||
}
|
||||
/** @type {number} */
|
||||
this.value = value
|
||||
}
|
||||
|
||||
valueOf() {
|
||||
this.value
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.value.toString()
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
export default class Primitive {
|
||||
|
||||
toString() {
|
||||
return "Unimplemented for " + this.constructor.name
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user