@@ -2,12 +2,12 @@ const ROUTE_PARAMETER_REGEXP = /:(\w+)/g
22const URL_FRAGMENT_REGEXP = '([^\\/]+)'
33
44const extractUrlParams = ( route , windowHash ) => {
5+ const params = { }
6+
57 if ( route . params . length === 0 ) {
6- return { }
8+ return params
79 }
810
9- const params = { }
10-
1111 const matches = windowHash
1212 . match ( route . testRegExp )
1313
@@ -28,16 +28,22 @@ export default () => {
2828 const router = { }
2929
3030 const checkRoutes = ( ) => {
31+ const { hash } = window . location
32+
3133 const currentRoute = routes . find ( route => {
32- return route . testRegExp . test ( window . location . hash )
34+ const { testRegExp } = route
35+ return testRegExp . test ( hash )
3336 } )
3437
3538 if ( ! currentRoute ) {
3639 notFound ( )
3740 return
3841 }
3942
40- const urlParams = extractUrlParams ( currentRoute , window . location . hash )
43+ const urlParams = extractUrlParams (
44+ currentRoute ,
45+ window . location . hash
46+ )
4147
4248 currentRoute . component ( urlParams )
4349 }
@@ -46,10 +52,15 @@ export default () => {
4652 const params = [ ]
4753
4854 const parsedFragment = fragment
49- . replace ( ROUTE_PARAMETER_REGEXP , ( match , paramName ) => {
50- params . push ( paramName )
51- return URL_FRAGMENT_REGEXP
52- } ) . replace ( / \/ / g, '\\/' )
55+ . replace (
56+ ROUTE_PARAMETER_REGEXP ,
57+ ( match , paramName ) => {
58+ params . push ( paramName )
59+ return URL_FRAGMENT_REGEXP
60+ } )
61+ . replace ( / \/ / g, '\\/' )
62+
63+ console . log ( `^${ parsedFragment } $` )
5364
5465 routes . push ( {
5566 testRegExp : new RegExp ( `^${ parsedFragment } $` ) ,
@@ -71,7 +82,11 @@ export default () => {
7182
7283 router . start = ( ) => {
7384 window
74- . addEventListener ( 'hashchange' , checkRoutes )
85+ . addEventListener (
86+ 'hashchange' ,
87+ checkRoutes
88+ )
89+
7590 if ( ! window . location . hash ) {
7691 window . location . hash = '#/'
7792 }
0 commit comments