Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
fix: don't add to route undefined parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jan 25, 2016
1 parent daa451d commit 00dd55a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
12 changes: 10 additions & 2 deletions modules/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
const getDefinedParams = params =>
Object.keys(params)
.filter(param => params[param] !== undefined)
.reduce(
(acc, param) => ({ ...acc, [param]: params[param] }),
{}
);

const persistentParamsPlugin = (params = {}) => router => {
// Persistent parameters
const persistentParams = Array.isArray(params)
Expand All @@ -14,12 +22,12 @@ const persistentParamsPlugin = (params = {}) => router => {

// Decorators
router.buildPath = function (route, params) {
const routeParams = { ...persistentParams, ...params };
const routeParams = { ...getDefinedParams(persistentParams), ...params };
return buildPath.call(router, route, routeParams);
};

router.buildState = function (route, params) {
const routeParams = { ...persistentParams, ...params };
const routeParams = { ...getDefinedParams(persistentParams), ...params };
return buildState.call(router, route, routeParams);
};

Expand Down
13 changes: 8 additions & 5 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ describe('router5 persistent params plugin', () => {

it('should persist specified parameters', (done) => {
router.start();
router.navigate('route1', { id: '1', mode: 'dev' }, {}, (err, state) => {
expect(state.path).to.equal('/route1/1?mode=dev');
router.navigate('route2', { id: '2' }, {}, (err, state) => {
expect(state.path).to.equal('/route2/2');
router.navigate('route1', { id: '1', mode: 'dev' }, {}, (err, state) => {
expect(state.path).to.equal('/route1/1?mode=dev');

router.navigate('route2', { id: '2' }, {}, (err, state) => {
expect(state.path).to.equal('/route2/2?mode=dev');
done();
router.navigate('route2', { id: '2' }, {}, (err, state) => {
expect(state.path).to.equal('/route2/2?mode=dev');
done();
});
});
});
});
Expand Down

0 comments on commit 00dd55a

Please sign in to comment.