Skip to content

Commit

Permalink
Simplify path parsing and normalization
Browse files Browse the repository at this point in the history
Disregard unused query parameter parsing, format in single pass
  • Loading branch information
aduth committed Nov 4, 2017
1 parent bd11cbd commit 757dede
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import wayfarer from 'wayfarer';
import { parse } from 'querystringify';
import memoize from 'memize';

/**
Expand All @@ -14,7 +13,7 @@ import TagsRoute from './tags';
import TagRoute from './tag';
import NotFoundRoute from './not-found';

const withParams = ( Route ) => ( params ) => [ params, Route ];
const withParams = ( Route ) => ( params ) => ( { params, Route } );

const router = wayfarer();
router.on( '/', withParams( HomeRoute ) );
Expand All @@ -25,12 +24,8 @@ router.on( '/tags', withParams( TagsRoute ) );
router.on( '*', withParams( NotFoundRoute ) );

export const getRouteByPath = memoize( ( path ) => {
const [ pathname, search = '' ] = path.split( '?' );
const [ params, Route ] = router( pathname.replace( /\/$/, '' ) );
// Strip query parameters, hash, trailing slash
path = path.replace( /\/?[\?|#].*$/g, '' );

return {
params,
Route,
query: parse( search ),
};
return router( path.replace( /\/$/, '' ) );
} );

0 comments on commit 757dede

Please sign in to comment.