Skip to content

Commit c2ddb72

Browse files
committed
fix(@ddd-framework/collections): type improvements | now attempting to unpack entity IDs
1 parent e11e5f9 commit c2ddb72

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

lib/collections/src/__tests__/entity-collection.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ describe('EntityCollection', () => {
8686

8787
collection.add(user);
8888

89-
expect(collection.contains(user.id)).toBeTruthy();
89+
expect(collection.contains(new UserId(user.id.unpack()))).toBeTruthy();
9090

9191
collection.remove(user);
9292

93-
expect(collection.contains(user.id)).toBeFalsy();
93+
expect(collection.contains(new UserId(user.id.unpack()))).toBeFalsy();
9494
});
9595
});
9696

@@ -138,11 +138,11 @@ describe('EntityCollection', () => {
138138

139139
collection.add(user);
140140

141-
expect(collection.get(user.id)).toBeTruthy();
141+
expect(collection.get(new UserId(user.id.unpack()))).toBeTruthy();
142142

143143
collection.remove(user);
144144

145-
expect(collection.get(user.id)).toBe(undefined);
145+
expect(collection.get(new UserId(user.id.unpack()))).toBe(undefined);
146146
});
147147
});
148148

@@ -178,7 +178,7 @@ describe('EntityCollection', () => {
178178

179179
expect(collection.contains(user)).toBeTruthy();
180180

181-
collection.remove(user.id);
181+
collection.remove(new UserId(user.id.unpack()));
182182

183183
expect(collection.contains(user)).toBeFalsy();
184184
});

lib/collections/src/entity-collection.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
NotFoundException
77
} from '@ddd-framework/core';
88

9-
type EntityCollectionMap<E extends Entity> = Map<symbol, E>;
9+
type EntityCollectionMap<E extends Entity> = Map<PropertyKey, E>;
1010

1111
/**
1212
* Represents a collection of Entity objects on the "many" end of a relationship.
@@ -43,7 +43,7 @@ export class EntityCollection<E extends Entity, K extends keyof E = keyof E>
4343
'Entity is already in the collection.'
4444
);
4545

46-
this.map.set(EntityId.getId(entity), entity);
46+
this.map.set(this.getUnpackedEntityId(entity), entity);
4747

4848
this._last = entity;
4949

@@ -63,8 +63,7 @@ export class EntityCollection<E extends Entity, K extends keyof E = keyof E>
6363
public contains(entity: E): boolean;
6464
public contains(entityId: E[K]): boolean;
6565
public contains(arg: E | E[K]): boolean {
66-
if (arg instanceof Entity) return this.map.has(EntityId.getId(arg));
67-
return this.map.has(arg as symbol);
66+
return this.map.has(this.getUnpackedEntityId(arg));
6867
}
6968

7069
/**
@@ -80,8 +79,7 @@ export class EntityCollection<E extends Entity, K extends keyof E = keyof E>
8079
public get(entity: E): E | undefined;
8180
public get(entityId: E[K]): E | undefined;
8281
public get(arg: E | E[K]): E | undefined {
83-
if (arg instanceof Entity) return this.map.get(EntityId.getId(arg));
84-
return this.map.get(arg as symbol);
82+
return this.map.get(this.getUnpackedEntityId(arg));
8583
}
8684

8785
/**
@@ -90,14 +88,13 @@ export class EntityCollection<E extends Entity, K extends keyof E = keyof E>
9088
public remove(entity: E): boolean;
9189
public remove(entityId: E[K]): boolean;
9290
public remove(arg: E | E[K]): boolean {
93-
if (arg instanceof Entity) return this.map.delete(EntityId.getId(arg));
94-
return this.map.delete(arg as symbol);
91+
return this.map.delete(this.getUnpackedEntityId(arg));
9592
}
9693

9794
/**
9895
* Returns a new Array object that contains the keys for each element in the EntityCollection in insertion order.
9996
*/
100-
public identities<Identity>(): Array<Identity> {
97+
public identities<Identity = E[K]>(): Array<Identity> {
10198
return Array.from(this.map.values(), (entity) =>
10299
EntityId.getId<Identity>(entity)
103100
);
@@ -113,7 +110,7 @@ export class EntityCollection<E extends Entity, K extends keyof E = keyof E>
113110
/**
114111
* Returns a new Array object that contains a two-member array of [key, value] for each element in the EntityCollection in insertion order.
115112
*/
116-
public entries<Identity>(): Array<[Identity, E]> {
113+
public entries<Identity = E[K]>(): Array<[Identity, E]> {
117114
return Array.from(this.map.values(), (entity) => [
118115
EntityId.getId<Identity>(entity),
119116
entity
@@ -134,13 +131,7 @@ export class EntityCollection<E extends Entity, K extends keyof E = keyof E>
134131
): Record<PropertyKey, Value> {
135132
const dictionary = this.entities().reduce<Record<PropertyKey, Value>>(
136133
(dictionary, entity) => {
137-
let dictionaryKey: PropertyKey;
138-
139-
const entityId = EntityId.getId(entity);
140-
141-
if (entityId instanceof DomainPrimitive)
142-
dictionaryKey = entityId.unpack();
143-
else dictionaryKey = entityId as PropertyKey;
134+
const dictionaryKey = this.getUnpackedEntityId(entity);
144135

145136
if (reducer) dictionary[dictionaryKey] = reducer(entity);
146137
else dictionary[dictionaryKey] = entity as unknown as Value;
@@ -198,8 +189,8 @@ export class EntityCollection<E extends Entity, K extends keyof E = keyof E>
198189
arg1:
199190
| Iterable<E>
200191
| Iterable<Value>
201-
| Record<string, E>
202-
| Record<string, Value>,
192+
| Record<PropertyKey, E>
193+
| Record<PropertyKey, Value>,
203194
arg2?:
204195
| ((value: Value) => E)
205196
| ((entry: [string | number | symbol, Value]) => E)
@@ -217,13 +208,13 @@ export class EntityCollection<E extends Entity, K extends keyof E = keyof E>
217208
}
218209
} else {
219210
if (arg2) {
220-
const dictionary = arg1 as Record<string, Value>;
221-
const reducer = arg2 as (entry: [string, Value]) => E;
211+
const dictionary = arg1 as Record<PropertyKey, Value>;
212+
const reducer = arg2 as (entry: [PropertyKey, Value]) => E;
222213
Array.from(Object.entries(dictionary)).forEach((entry) =>
223214
collection.add(reducer(entry))
224215
);
225216
} else {
226-
const dictionary = arg1 as Record<string, E>;
217+
const dictionary = arg1 as Record<PropertyKey, E>;
227218
Array.from(Object.values(dictionary)).forEach((entity) =>
228219
collection.add(entity)
229220
);
@@ -232,4 +223,18 @@ export class EntityCollection<E extends Entity, K extends keyof E = keyof E>
232223

233224
return collection;
234225
}
226+
227+
/**
228+
* Returns the unpacked EntityId value of an Entity or DomainPrimitive.
229+
*/
230+
private getUnpackedEntityId(arg: E | E[K]): PropertyKey {
231+
if (arg instanceof Entity) {
232+
const id = EntityId.getId(arg);
233+
if (id instanceof DomainPrimitive) return id.unpack() as PropertyKey;
234+
else return id as PropertyKey;
235+
}
236+
237+
if (arg instanceof DomainPrimitive) return arg.unpack() as PropertyKey;
238+
else return arg as PropertyKey;
239+
}
235240
}

0 commit comments

Comments
 (0)