Skip to content

Commit 1801d0c

Browse files
rubenschuckitalxhub
authored andcommitted
fix(router): properly compare array queryParams for equality (#37709) (#37860)
queryParams will be judged different if the arrays do not contain the same frequency of elements and the same otherwise. PR Close #37860
1 parent a5c3073 commit 1801d0c

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

packages/router/src/url_tree.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ function equalSegmentGroups(container: UrlSegmentGroup, containee: UrlSegmentGro
3939
}
4040

4141
function containsQueryParams(container: Params, containee: Params): boolean {
42-
// TODO: This does not handle array params correctly.
4342
return Object.keys(containee).length <= Object.keys(container).length &&
4443
Object.keys(containee).every(key => equalArraysOrString(container[key], containee[key]));
4544
}

packages/router/src/utils/collection.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {NgModuleFactory, ɵisObservable as isObservable, ɵisPromise as isPromise} from '@angular/core';
9+
import {ɵisObservable as isObservable, ɵisPromise as isPromise} from '@angular/core';
1010
import {from, Observable, of} from 'rxjs';
1111
import {concatAll, last as lastValue, map} from 'rxjs/operators';
1212

@@ -45,8 +45,10 @@ export function shallowEqual(a: Params, b: Params): boolean {
4545
*/
4646
export function equalArraysOrString(a: string|string[], b: string|string[]) {
4747
if (Array.isArray(a) && Array.isArray(b)) {
48-
if (a.length != b.length) return false;
49-
return a.every(aItem => b.indexOf(aItem) > -1);
48+
if (a.length !== b.length) return false;
49+
const aSorted = [...a].sort();
50+
const bSorted = [...b].sort();
51+
return aSorted.every((val, index) => bSorted[index] === val);
5052
} else {
5153
return a === b;
5254
}

packages/router/test/url_tree.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ describe('UrlTree', () => {
6767
expect(containsTree(t1, t2, true)).toBe(false);
6868
});
6969

70+
it('should return false when queryParams are not the same', () => {
71+
const t1 = serializer.parse('/one/two?test=4&test=4&test=2');
72+
const t2 = serializer.parse('/one/two?test=4&test=3&test=2');
73+
expect(containsTree(t1, t2, false)).toBe(false);
74+
});
75+
76+
it('should return true when queryParams are the same in different order', () => {
77+
const t1 = serializer.parse('/one/two?test=4&test=3&test=2');
78+
const t2 = serializer.parse('/one/two?test=2&test=3&test=4');
79+
expect(containsTree(t1, t2, false)).toBe(true);
80+
});
81+
82+
it('should return true when queryParams are the same in different order', () => {
83+
const t1 = serializer.parse('/one/two?test=4&test=4&test=1');
84+
const t2 = serializer.parse('/one/two?test=1&test=4&test=4');
85+
expect(containsTree(t1, t2, false)).toBe(true);
86+
});
87+
7088
it('should return false when containee is missing queryParams', () => {
7189
const t1 = serializer.parse('/one/two?page=5');
7290
const t2 = serializer.parse('/one/two');

0 commit comments

Comments
 (0)