Skip to content

Commit 57fd3d7

Browse files
timdeschryverbrandonroberts
authored andcommitted
feat(router-store): add selectQueryParam and selectRouteParam (#2014)
Select a specific query param or route param Usage: export const selectId = selectRouteParam('id'); export const selectRef = selectQueryParam('ref');
1 parent c00a9c2 commit 57fd3d7

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

modules/router-store/spec/router_selectors.spec.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ const mockData = {
6161
routeConfig: {
6262
path: ':id',
6363
},
64-
queryParams: {},
64+
queryParams: {
65+
ref: 'ngrx.io',
66+
},
6567
queryParamMap: {
66-
params: {},
68+
params: {
69+
ref: 'ngrx.io',
70+
},
6771
},
6872
children: [],
6973
},
@@ -115,11 +119,13 @@ describe('Router State Selectors', () => {
115119

116120
selectors = getSelectors((state: State) => state.router);
117121
});
122+
118123
it('should create selectCurrentRoute selector for selecting the current route', () => {
119124
const result = selectors.selectCurrentRoute(state);
120125

121126
expect(result).toEqual(state.router.state.root.firstChild.firstChild);
122127
});
128+
123129
it('should return undefined from selectCurrentRoute if routerState does not exist', () => {
124130
interface State {
125131
router: any;
@@ -134,27 +140,47 @@ describe('Router State Selectors', () => {
134140

135141
expect(result).toEqual(undefined);
136142
});
143+
137144
it('should create a selector for selecting the query params', () => {
138145
const result = selectors.selectQueryParams(state);
139146

140147
expect(result).toEqual(
141148
state.router.state.root.firstChild.firstChild.queryParams
142149
);
143150
});
151+
152+
it('should create a selector for selecting a specific query param', () => {
153+
const result = selectors.selectQueryParam('ref')(state);
154+
155+
expect(result).toEqual(
156+
state.router.state.root.firstChild.firstChild.queryParams.ref
157+
);
158+
});
159+
144160
it('should create a selector for selecting the route params', () => {
145161
const result = selectors.selectRouteParams(state);
146162

147163
expect(result).toEqual(
148164
state.router.state.root.firstChild.firstChild.params
149165
);
150166
});
167+
168+
it('should create a selector for selecting a specific route param', () => {
169+
const result = selectors.selectRouteParam('id')(state);
170+
171+
expect(result).toEqual(
172+
state.router.state.root.firstChild.firstChild.params.id
173+
);
174+
});
175+
151176
it('should create a selector for selecting the route data', () => {
152177
const result = selectors.selectRouteData(state);
153178

154179
expect(result).toEqual(
155180
state.router.state.root.firstChild.firstChild.data
156181
);
157182
});
183+
158184
it('should create a selector for selecting the url', () => {
159185
const result = selectors.selectUrl(state);
160186

modules/router-store/src/models.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { Data, Params } from '@angular/router';
33
export interface RouterStateSelectors<V> {
44
selectCurrentRoute: (state: V) => any;
55
selectQueryParams: (state: V) => Params;
6+
selectQueryParam: (param: string) => (state: V) => string | undefined;
67
selectRouteParams: (state: V) => Params;
8+
selectRouteParam: (param: string) => (state: V) => string | undefined;
79
selectRouteData: (state: V) => Data;
810
selectUrl: (state: V) => string;
911
}

modules/router-store/src/router_selectors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ export function getSelectors<V>(
2626
selectCurrentRoute,
2727
route => route && route.queryParams
2828
);
29+
const selectQueryParam = (param: string) =>
30+
createSelector(selectQueryParams, params => params && params[param]);
2931
const selectRouteParams = createSelector(
3032
selectCurrentRoute,
3133
route => route && route.params
3234
);
35+
const selectRouteParam = (param: string) =>
36+
createSelector(selectRouteParams, params => params && params[param]);
3337
const selectRouteData = createSelector(
3438
selectCurrentRoute,
3539
route => route && route.data
@@ -42,7 +46,9 @@ export function getSelectors<V>(
4246
return {
4347
selectCurrentRoute,
4448
selectQueryParams,
49+
selectQueryParam,
4550
selectRouteParams,
51+
selectRouteParam,
4652
selectRouteData,
4753
selectUrl,
4854
};

projects/ngrx.io/content/guide/router-store/selectors.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Usage:
1212
`reducers/index.ts`
1313

1414
```ts
15-
import { getSelectors, RouterReducerState } from '@ngrx/router-store';
15+
import * as fromRouter from '@ngrx/router-store';
1616

1717
export interface State {
1818
router: fromRouter.RouterReducerState<any>;
@@ -25,9 +25,13 @@ export const selectRouter = createFeatureSelector<
2525

2626
const {
2727
selectQueryParams, // select the current route query params
28+
selectQueryParam, // factory function to select a query param
2829
selectRouteParams, // select the current route params
30+
selectRouteParam, // factory function to select a route param
2931
selectRouteData, // select the current route data
3032
selectUrl, // select the current url
31-
} = getSelectors(selectRouter);
33+
} = fromRouter.getSelectors(selectRouter);
3234

35+
export const selectRouteId = selectRouteParam('id');
36+
export const selectStatus = selectQueryParam('status');
3337
```

0 commit comments

Comments
 (0)