From 6de9684048481daf6ad074e7b18598a0f8be3ced Mon Sep 17 00:00:00 2001 From: waynewyang Date: Mon, 25 Dec 2023 21:36:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20release=20version=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BREAKING CHANGE: 🧨 release version 1 --- src/aggregate/index.ts | 10 ++++------ src/entity/index.ts | 16 ++++++++++++++++ test/entity/index.test.ts | 2 ++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/aggregate/index.ts b/src/aggregate/index.ts index daf974e..492811c 100644 --- a/src/aggregate/index.ts +++ b/src/aggregate/index.ts @@ -93,7 +93,7 @@ export class Aggregate { * @param key - The key of the entity. * @returns The entity with the specified key, or undefined if not found. */ - getEntityByKey(key: string): Entity | undefined { + getEntityByKey(key: string): Entity | undefined { return this.entities[key] } @@ -102,9 +102,7 @@ export class Aggregate { * @param key - The key of the entity collections. * @returns The entity collections with the specified key, or undefined if not found. */ - getEntityCollectionsByKey( - key: string - ): Entities> | undefined { + getEntityCollectionsByKey(key: string): Entities> | undefined { return this.entityCollections[key] } @@ -113,7 +111,7 @@ export class Aggregate { * @param key - The key of the entity. * @param entity - The entity to add. */ - addEntity(key: string, entity: Entity): void { + addEntity(key: string, entity: Entity): void { this.entities[key] = entity } @@ -130,7 +128,7 @@ export class Aggregate { * @param key - The key of the entity collections. * @param entity - The entity collections to add. */ - addEntityCollections(key: string, entity: Entities>): void { + addEntityCollections(key: string, entity: Entities>): void { this.entityCollections[key] = entity } diff --git a/src/entity/index.ts b/src/entity/index.ts index dc964f7..e4bf540 100644 --- a/src/entity/index.ts +++ b/src/entity/index.ts @@ -156,4 +156,20 @@ export abstract class Entity implements EntityInterface { setId(id: any) { this.id = id } + + /** + * Gets the value of the entity. + * @returns ValueFields. + */ + value(): ValueFields { + const result: Record = {} + + Object.entries(this).forEach(([key, value]) => { + if (this.hasOwnProperty(key) && typeof value !== "function") { + result[key] = value instanceof Entity ? value.clone() : value + } + }) + + return result as ValueFields + } } diff --git a/test/entity/index.test.ts b/test/entity/index.test.ts index 0028f4c..c07ad3d 100644 --- a/test/entity/index.test.ts +++ b/test/entity/index.test.ts @@ -22,6 +22,7 @@ import { expect } from "chai" import { it } from "mocha" import { Entity } from "../../src/entity" import { ValueFields } from "@unipackage/utils" +import assert from "assert" // Sample class extending Entity for testing class SampleEntity extends Entity<{ @@ -53,6 +54,7 @@ describe("Entity", () => { } const entity = new SampleEntity(data) expect(entity).to.be.an.instanceOf(SampleEntity) + assert.deepStrictEqual(entity.value(), data) }) it("[clone test]: should clone the entity", () => {