From 4558cc12375325b73a0391cfcbefcaf7222a7704 Mon Sep 17 00:00:00 2001 From: stewones Date: Wed, 17 Aug 2022 18:08:26 -0300 Subject: [PATCH 1/3] fix(livequery): add support for containedIn but with the object key as array #2088 --- spec/QueryTools.spec.js | 13 +++++++++++++ src/LiveQuery/QueryTools.js | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/spec/QueryTools.spec.js b/spec/QueryTools.spec.js index 7e09078dad..6e4da26a8d 100644 --- a/spec/QueryTools.spec.js +++ b/spec/QueryTools.spec.js @@ -581,6 +581,19 @@ describe('matchesQuery', function () { Parse.Object.fromJSON({ className: 'Profile', objectId: 'def' }), ]); expect(matchesQuery(message, q)).toBe(false); + + const messageWithKeyAsArray = { + id: new Id('Message', 'O1'), + profiles: [pointer('Profile', 'abc'), pointer('Profile', 'def')], + }; + + const qWithKeyAsArray = new Parse.Query('Message'); + qWithKeyAsArray.containedIn('profiles', [ + Parse.Object.fromJSON({ className: 'Profile', objectId: 'ghi' }), + Parse.Object.fromJSON({ className: 'Profile', objectId: 'def' }), + ]); + + expect(matchesQuery(messageWithKeyAsArray, qWithKeyAsArray)).toBe(true); }); it('should support notContainedIn with pointers', () => { diff --git a/src/LiveQuery/QueryTools.js b/src/LiveQuery/QueryTools.js index 905919ef61..d2dbf5b9ec 100644 --- a/src/LiveQuery/QueryTools.js +++ b/src/LiveQuery/QueryTools.js @@ -105,6 +105,11 @@ function contains(haystack: Array, needle: any): boolean { } return false; } + + if (Array.isArray(needle)) { + return needle.map(need => contains(haystack, need)); + } + return haystack.indexOf(needle) > -1; } /** From fd7e460264b54a109bccfa3c73a21272863ed23e Mon Sep 17 00:00:00 2001 From: stewones Date: Wed, 17 Aug 2022 18:52:08 -0300 Subject: [PATCH 2/3] fix: improve contains logic --- spec/QueryTools.spec.js | 26 ++++++++++++++++++-------- src/LiveQuery/QueryTools.js | 7 ++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/spec/QueryTools.spec.js b/spec/QueryTools.spec.js index 6e4da26a8d..1bb97990f2 100644 --- a/spec/QueryTools.spec.js +++ b/spec/QueryTools.spec.js @@ -581,19 +581,29 @@ describe('matchesQuery', function () { Parse.Object.fromJSON({ className: 'Profile', objectId: 'def' }), ]); expect(matchesQuery(message, q)).toBe(false); + }); - const messageWithKeyAsArray = { - id: new Id('Message', 'O1'), - profiles: [pointer('Profile', 'abc'), pointer('Profile', 'def')], + it('should support containedIn in with array of pointers', () => { + const message = { + id: new Id('Message', 'O2'), + profiles: [pointer('Profile', 'yeahaw'), pointer('Profile', 'yes')], }; - const qWithKeyAsArray = new Parse.Query('Message'); - qWithKeyAsArray.containedIn('profiles', [ - Parse.Object.fromJSON({ className: 'Profile', objectId: 'ghi' }), - Parse.Object.fromJSON({ className: 'Profile', objectId: 'def' }), + let q = new Parse.Query('Message'); + q.containedIn('profiles', [ + Parse.Object.fromJSON({ className: 'Profile', objectId: 'no' }), + Parse.Object.fromJSON({ className: 'Profile', objectId: 'yes' }), ]); - expect(matchesQuery(messageWithKeyAsArray, qWithKeyAsArray)).toBe(true); + expect(matchesQuery(message, q)).toBe(true); + + q = new Parse.Query('Message'); + q.containedIn('profiles', [ + Parse.Object.fromJSON({ className: 'Profile', objectId: 'no' }), + Parse.Object.fromJSON({ className: 'Profile', objectId: 'nope' }), + ]); + + expect(matchesQuery(message, q)).toBe(false); }); it('should support notContainedIn with pointers', () => { diff --git a/src/LiveQuery/QueryTools.js b/src/LiveQuery/QueryTools.js index d2dbf5b9ec..a50699f03e 100644 --- a/src/LiveQuery/QueryTools.js +++ b/src/LiveQuery/QueryTools.js @@ -103,11 +103,16 @@ function contains(haystack: Array, needle: any): boolean { return true; } } + return false; } if (Array.isArray(needle)) { - return needle.map(need => contains(haystack, need)); + for (const need of needle) { + if (contains(haystack, need)) { + return true; + } + } } return haystack.indexOf(needle) > -1; From 019205f3248741a4a7aa3a5c1719233dd9b3aedf Mon Sep 17 00:00:00 2001 From: stewones Date: Wed, 17 Aug 2022 19:01:38 -0300 Subject: [PATCH 3/3] fix: spec typo --- spec/QueryTools.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/QueryTools.spec.js b/spec/QueryTools.spec.js index 1bb97990f2..dbd3c9a5d3 100644 --- a/spec/QueryTools.spec.js +++ b/spec/QueryTools.spec.js @@ -583,7 +583,7 @@ describe('matchesQuery', function () { expect(matchesQuery(message, q)).toBe(false); }); - it('should support containedIn in with array of pointers', () => { + it('should support containedIn with array of pointers', () => { const message = { id: new Id('Message', 'O2'), profiles: [pointer('Profile', 'yeahaw'), pointer('Profile', 'yes')],