import * as pathToRegexp from 'path-to-regexp' ; import { ProuterPath , ProuterPathExp , ProuterRequestProcessor , ProuterRequest , ProuterParsedHandler , ProuterPathKey } from './entity' ; export const routerHelper = { getPath ( ) { return decodeURI ( location . pathname + location . search ) ; } , stringToRegexp ( str ) { const keys = [ ] ; const resp = pathToRegexp ( str , keys ) as ProuterPathExp ; resp . keys = keys ; return resp ; } , parseQuery ( str ) { const searchObj = { } ; if ( str === '' ) { return searchObj ; } const qs = str . slice ( 1 ) ; const args = qs . split ( '&' ) ; for ( const arg of args ) { const paramKv = arg . split ( '=' ) ; searchObj [ decodeURIComponent ( paramKv [ 0 ] ) ] = decodeURIComponent ( paramKv [ 1 ] ) ; } return searchObj ; } , parsePath ( path ) { let url ; if ( typeof URL === 'function' ) { url = new URL ( path , 'http://example.com' ) ; } else { url = document . createElement ( 'a' ) ; url . href = 'http://example.com' + path ; } const parsedPath = { path : url . pathname , queryString : url . search , query : routerHelper . parseQuery ( url . search ) } ; return parsedPath ; } , /**
     * Obtain the request processors for the given path according to the handlers in the router.
     */ obtainRequestProcessors ( path , handlers ) { const parsedPath = routerHelper . parsePath ( path ) ; const requestProcessors = [ ] ; const req = parsedPath as ProuterRequest ; req . params = { } ; for ( const handler of handlers ) { const result = handler . pathExp . exec ( req . path ) ; if ( result ) { const params = result . slice ( 1 ) ; const keys = handler . pathExp . keys ; for ( let i = 0 ; i < params . length ; i ++ ) { req . params [ keys [ i ] . name ] = decodeURIComponent ( params [ i ] ) ; } requestProcessors . push ( { callback : handler . callback , request : req } ) ; } } return requestProcessors ; } } ;