From 7b20b387a60e7bd1e8ef63d3bf7d76d6531692fe Mon Sep 17 00:00:00 2001 From: Tommy Smith Date: Tue, 2 Sep 2025 10:41:36 +0100 Subject: [PATCH] Ensure objects are copied so that modifying by ref doesn't happen --- src/collections/filters/classes.ts | 15 +++++++------- src/collections/filters/unit.test.ts | 30 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/collections/filters/classes.ts b/src/collections/filters/classes.ts index b98eb89c..036cc043 100644 --- a/src/collections/filters/classes.ts +++ b/src/collections/filters/classes.ts @@ -81,7 +81,6 @@ export class FilterBase { : undefined, }); } - let target = this.target; while (target.target !== undefined) { if (TargetGuards.isTargetRef(target.target)) { @@ -222,32 +221,32 @@ export class FilterRef implements Filter { public byRef & string>(linkOn: K): Filter> { this.target.target = { type_: 'single', linkOn: linkOn }; - return new FilterRef>(this.target); + return new FilterRef>(Object.assign({}, this.target)); } public byRefMultiTarget & string>(linkOn: K, targetCollection: string) { this.target.target = { type_: 'multi', linkOn: linkOn, targetCollection: targetCollection }; - return new FilterRef>(this.target); + return new FilterRef>(Object.assign({}, this.target)); } public byProperty & string>(name: K, length = false) { - return new FilterProperty(name, length, this.target); + return new FilterProperty(name, length, Object.assign({}, this.target)); } public byRefCount & string>(linkOn: K) { - return new FilterCount(linkOn, this.target); + return new FilterCount(linkOn, Object.assign({}, this.target)); } public byId() { - return new FilterId(this.target); + return new FilterId(Object.assign({}, this.target)); } public byCreationTime() { - return new FilterCreationTime(this.target); + return new FilterCreationTime(Object.assign({}, this.target)); } public byUpdateTime() { - return new FilterUpdateTime(this.target); + return new FilterUpdateTime(Object.assign({}, this.target)); } } diff --git a/src/collections/filters/unit.test.ts b/src/collections/filters/unit.test.ts index b56580a5..a06f0f70 100644 --- a/src/collections/filters/unit.test.ts +++ b/src/collections/filters/unit.test.ts @@ -313,6 +313,36 @@ describe('Unit testing of filters', () => { }); }); + it('should create two filters through a single ref used multiple times', () => { + const f = filter.byRef('self'); + const f1 = f.byProperty('name').equal('Jim'); + const f2 = f.byProperty('name').equal('Bob'); + expect(f1).toEqual>({ + operator: 'Equal', + target: { + singleTarget: { + on: 'self', + target: { + property: 'name', + }, + }, + }, + value: 'Jim', + }); + expect(f2).toEqual>({ + operator: 'Equal', + target: { + singleTarget: { + on: 'self', + target: { + property: 'name', + }, + }, + }, + value: 'Bob', + }); + }); + it('should create a nested reference filter', () => { const f = filter.byRef('self').byRef('self').byProperty('name').isNull(true); expect(f).toEqual>({