Skip to content

Commit

Permalink
fix: 🐛 delete key:any
Browse files Browse the repository at this point in the history
  • Loading branch information
waynewyang committed Dec 13, 2023
1 parent ae511c8 commit 40f7d4e
Showing 1 changed file with 5 additions and 50 deletions.
55 changes: 5 additions & 50 deletions src/entity/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { ValueObject } from "../valueObject"
import { isEqual } from "lodash"
import { TypeFromProperties } from "@unipackage/utils"

export interface EntityInterface {
clone(): this
compareProperties(entity: this): boolean
deserialize(data: string): void
equal(other: this, fields?: Array<keyof this>): boolean
getName(): string
getType(): string
getId(): any
serialize(): string
}

export abstract class Entity<T extends Object> implements EntityInterface {
id?: any;
[key: string]: any
id?: any

constructor(data: TypeFromProperties<T>) {
if (!data || typeof data !== "object") {
Expand All @@ -30,6 +25,10 @@ export abstract class Entity<T extends Object> implements EntityInterface {
return Object.keys(this)
}

protected validate(): boolean {
return true
}

equal(other: this, fields?: Array<keyof this>): boolean {
const properties = fields ?? (Object.keys(this) as Array<keyof this>)
for (const property of properties) {
Expand All @@ -48,56 +47,12 @@ export abstract class Entity<T extends Object> implements EntityInterface {
return `${this.constructor.name}`
}

protected validate(): boolean {
return true
}

getId(): any {
return this.id
}

serialize(): string {
return JSON.stringify(this)
}

deserialize(data: string): void {
try {
this.properties = JSON.parse(data)
} catch (error: any) {
throw new Error("Failed to deserialize the data. " + error.message)
}
}

clone(): this {
const clonedData = JSON.parse(JSON.stringify(this))
return new (this.constructor as any)(clonedData)
}

//no use
compareProperties(entity: this): boolean {
const thisKeys = Object.keys(this)
const entityKeys = Object.keys(entity)

if (thisKeys.length !== entityKeys.length) {
return false
}

for (const key of thisKeys) {
const thisValue = this[key]
const entityValue = entity[key]

if (
thisValue instanceof ValueObject &&
entityValue instanceof ValueObject
) {
if (!thisValue.equals(entityValue.immutable())) {
return false
}
} else if (thisValue !== entityValue) {
return false
}
}

return true
}
}

0 comments on commit 40f7d4e

Please sign in to comment.