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