File tree Expand file tree Collapse file tree 4 files changed +28
-5
lines changed Expand file tree Collapse file tree 4 files changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -989,6 +989,18 @@ describe('Collection', () => {
989
989
expect ( collection . getOne ( 0 ) ) . toEqual ( { id : 0 , name : 'bar' } ) ;
990
990
expect ( collection . getOne ( 1 ) ) . toEqual ( { id : 1 , name : 'baz' } ) ;
991
991
} ) ;
992
+
993
+ it ( 'should update the original item' , ( ) => {
994
+ const items = [ { name : 'foo' } , { name : 'baz' } ] ;
995
+ const collection = new Collection < CollectionItem > ( {
996
+ items,
997
+ } ) ;
998
+ collection . updateOne ( 0 , { id : 0 , name : 'bar' } ) ;
999
+ expect ( collection . getOne ( 0 ) ) . toEqual ( { id : 0 , name : 'bar' } ) ;
1000
+ expect ( collection . getOne ( 1 ) ) . toEqual ( { id : 1 , name : 'baz' } ) ;
1001
+ expect ( items [ 0 ] ) . toEqual ( { name : 'foo' } ) ;
1002
+ expect ( items [ 1 ] ) . toEqual ( { name : 'baz' } ) ;
1003
+ } ) ;
992
1004
} ) ;
993
1005
994
1006
describe ( 'removeOne' , ( ) => {
Original file line number Diff line number Diff line change 1
1
import get from 'lodash/get.js' ;
2
2
import matches from 'lodash/matches.js' ;
3
+ import cloneDeep from 'lodash/cloneDeep.js' ;
3
4
import type { Database } from './Database.ts' ;
4
5
import type {
5
6
CollectionItem ,
@@ -196,7 +197,8 @@ export class Collection<T extends CollectionItem = CollectionItem> {
196
197
}
197
198
198
199
addOne ( item : T ) {
199
- const identifier = item [ this . identifierName ] ;
200
+ const clone = cloneDeep ( item ) ;
201
+ const identifier = clone [ this . identifierName ] ;
200
202
if ( identifier != null ) {
201
203
if ( this . getIndex ( identifier ) !== - 1 ) {
202
204
throw new Error (
@@ -208,10 +210,10 @@ export class Collection<T extends CollectionItem = CollectionItem> {
208
210
}
209
211
} else {
210
212
// @ts -expect-error - For some reason, TS does not accept writing a generic types with the index signature
211
- item [ this . identifierName ] = this . getNewId ( ) ;
213
+ clone [ this . identifierName ] = this . getNewId ( ) ;
212
214
}
213
- this . items . push ( item ) ;
214
- return Object . assign ( { } , item ) ; // clone item to avoid returning the original;
215
+ this . items . push ( clone ) ;
216
+ return clone ; // clone item to avoid returning the original;
215
217
}
216
218
217
219
updateOne ( identifier : number | string , item : T ) {
Original file line number Diff line number Diff line change @@ -144,5 +144,13 @@ describe('Single', () => {
144
144
single . updateOnly ( { name : 'bar' } ) ;
145
145
expect ( single . getOnly ( ) ) . toEqual ( { name : 'bar' } ) ;
146
146
} ) ;
147
+
148
+ it ( 'should not update the original item' , ( ) => {
149
+ const data = { name : 'foo' } ;
150
+ const single = new Single ( data ) ;
151
+ single . updateOnly ( { name : 'bar' } ) ;
152
+ expect ( single . getOnly ( ) ) . toEqual ( { name : 'bar' } ) ;
153
+ expect ( data ) . toEqual ( { name : 'foo' } ) ;
154
+ } ) ;
147
155
} ) ;
148
156
} ) ;
Original file line number Diff line number Diff line change
1
+ import cloneDeep from 'lodash/cloneDeep.js' ;
1
2
import type { Database } from './Database.ts' ;
2
3
import type { CollectionItem , Embed , Query } from './types.ts' ;
3
4
@@ -12,7 +13,7 @@ export class Single<T extends CollectionItem = CollectionItem> {
12
13
"Can't initialize a Single with anything except an object" ,
13
14
) ;
14
15
}
15
- this . obj = obj ;
16
+ this . obj = cloneDeep ( obj ) ;
16
17
}
17
18
18
19
/**
You can’t perform that action at this time.
0 commit comments