Skip to content

Commit f5b54ad

Browse files
authored
Ensure objects are copied so that modifying by ref doesn't happen (#344)
1 parent 36d6e63 commit f5b54ad

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

src/collections/filters/classes.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export class FilterBase {
8181
: undefined,
8282
});
8383
}
84-
8584
let target = this.target;
8685
while (target.target !== undefined) {
8786
if (TargetGuards.isTargetRef(target.target)) {
@@ -222,32 +221,32 @@ export class FilterRef<T> implements Filter<T> {
222221

223222
public byRef<K extends RefKeys<T> & string>(linkOn: K): Filter<ExtractCrossReferenceType<T[K]>> {
224223
this.target.target = { type_: 'single', linkOn: linkOn };
225-
return new FilterRef<ExtractCrossReferenceType<T[K]>>(this.target);
224+
return new FilterRef<ExtractCrossReferenceType<T[K]>>(Object.assign({}, this.target));
226225
}
227226

228227
public byRefMultiTarget<K extends RefKeys<T> & string>(linkOn: K, targetCollection: string) {
229228
this.target.target = { type_: 'multi', linkOn: linkOn, targetCollection: targetCollection };
230-
return new FilterRef<ExtractCrossReferenceType<T[K]>>(this.target);
229+
return new FilterRef<ExtractCrossReferenceType<T[K]>>(Object.assign({}, this.target));
231230
}
232231

233232
public byProperty<K extends NonRefKeys<T> & string>(name: K, length = false) {
234-
return new FilterProperty<T[K]>(name, length, this.target);
233+
return new FilterProperty<T[K]>(name, length, Object.assign({}, this.target));
235234
}
236235

237236
public byRefCount<K extends RefKeys<T> & string>(linkOn: K) {
238-
return new FilterCount(linkOn, this.target);
237+
return new FilterCount(linkOn, Object.assign({}, this.target));
239238
}
240239

241240
public byId() {
242-
return new FilterId(this.target);
241+
return new FilterId(Object.assign({}, this.target));
243242
}
244243

245244
public byCreationTime() {
246-
return new FilterCreationTime(this.target);
245+
return new FilterCreationTime(Object.assign({}, this.target));
247246
}
248247

249248
public byUpdateTime() {
250-
return new FilterUpdateTime(this.target);
249+
return new FilterUpdateTime(Object.assign({}, this.target));
251250
}
252251
}
253252

src/collections/filters/unit.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,36 @@ describe('Unit testing of filters', () => {
313313
});
314314
});
315315

316+
it('should create two filters through a single ref used multiple times', () => {
317+
const f = filter.byRef('self');
318+
const f1 = f.byProperty('name').equal('Jim');
319+
const f2 = f.byProperty('name').equal('Bob');
320+
expect(f1).toEqual<FilterValue<string>>({
321+
operator: 'Equal',
322+
target: {
323+
singleTarget: {
324+
on: 'self',
325+
target: {
326+
property: 'name',
327+
},
328+
},
329+
},
330+
value: 'Jim',
331+
});
332+
expect(f2).toEqual<FilterValue<string>>({
333+
operator: 'Equal',
334+
target: {
335+
singleTarget: {
336+
on: 'self',
337+
target: {
338+
property: 'name',
339+
},
340+
},
341+
},
342+
value: 'Bob',
343+
});
344+
});
345+
316346
it('should create a nested reference filter', () => {
317347
const f = filter.byRef('self').byRef('self').byProperty('name').isNull(true);
318348
expect(f).toEqual<FilterValue<boolean>>({

0 commit comments

Comments
 (0)