Integer => IntegerEntity, No more primitive

This commit is contained in:
barsdeveloper
2021-11-23 21:01:07 +01:00
parent 6a3e2cc36f
commit c774886a2e
8 changed files with 52 additions and 85 deletions

21
js/entity/IntegerEntity.js Executable file
View 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()
}
}

View File

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

View File

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

View File

@@ -1,6 +0,0 @@
export default class Primitive {
toString() {
return "Unimplemented for " + this.constructor.name
}
}