module import tc from '@spaceavocado/type-check' ; import { HISTORY_ACTION } from './history' ; import { hasPrefix , parseURL } from './utils' ; /**
 * Name property has higher priority that path property.
 */ interface LocationBase { /** Name name of the route. */ name ? : string ; /** Path route URL. */ path : string ; /** Hash name of the route. */ hash : string ; } export interface RawLocation extends LocationBase { /** Query parameters. */ query ? : { [ k ] : string ; } | null ; /** Route parameters */ params ? : { [ k ] : string ; } | null ; /** Replace in the history. */ replace : boolean ; } export interface Location extends LocationBase { /** Query parameters. */ query : { [ k ] : string ; } ; /** Route parameters. */ params : { [ k ] : string ; } ; /** Location history action. */ action : HISTORY_ACTION ; } param { RawLocation } rawLocation return { Location } export function createLocation ( rawLocation ) { const location = { path : '' , hash : '' , query : { } , params : { } , action : HISTORY_ACTION . PUSH , } ; location . path = rawLocation . path || '' ; if ( location . path . length > 0 && hasPrefix ( location . path , '/' ) == false ) { location . path = '/' + location . path ; } if ( tc . not . isNullOrUndefined ( rawLocation . replace ) && rawLocation . replace === true ) { location . action = HISTORY_ACTION . REPLACE ; } if ( tc . not . isNullOrUndefined ( rawLocation . name ) && tc . isString ( rawLocation . name ) ) { location . name = rawLocation . name ; } if ( tc . not . isNullOrUndefined ( rawLocation . hash ) && tc . isString ( rawLocation . hash ) ) { location . hash = rawLocation . hash . replace ( '#' , '' ) ; } if ( tc . not . isNullOrUndefined ( rawLocation . params ) && tc . isObject ( rawLocation . params ) ) { for ( const key in rawLocation . params ) { if ( rawLocation . params . hasOwnProperty ( key ) ) { location . params [ key ] = rawLocation . params [ key ] ; } } } if ( tc . not . isNullOrUndefined ( rawLocation . query ) && tc . isObject ( rawLocation . query ) ) { for ( const key in rawLocation . query ) { if ( rawLocation . query . hasOwnProperty ( key ) ) { location . query [ key ] = rawLocation . query [ key ] ; } } } if ( location . path . length == 0 ) { return location ; } try { const parsedURL = parseURL ( location . path ) ; location . path = parsedURL . base ; location . query = { ... location . query , ... parsedURL . query } ; if ( parsedURL . hash . length > 0 ) { location . hash = parsedURL . hash ; } } catch ( e ) { throw new Error ( `invalid URL, ${ e . toString ( ) }` ) ; } return location ; } ;