import { IJetURLChunk } from "./interfaces" ; export function parse ( url ) { if ( url [ 0 ] === "/" ) { url = url . substr ( 1 ) ; } const parts = url . split ( "/" ) ; const chunks = [ ] ; for ( let i = 0 ; i < parts . length ; i ++ ) { const test = parts [ i ] ; const result = { } ; let pos = test . indexOf ( ":" ) ; if ( pos === - 1 ) { pos = test . indexOf ( "?" ) ; } if ( pos !== - 1 ) { const params = test . substr ( pos + 1 ) . split ( /[\:\?\&]/g ) ; for ( const param of params ) { const dchunk = param . split ( "=" ) ; result [ dchunk [ 0 ] ] = decodeURIComponent ( dchunk [ 1 ] ) ; } } chunks [ i ] = { page : ( pos > - 1 ? test . substr ( 0 , pos ) : test ) , params : result , isNew : true } ; } return chunks ; } export function url2str ( stack ) { const url = [ ] ; for ( const chunk of stack ) { url . push ( "/" + chunk . page ) ; const params = obj2str ( chunk . params ) ; if ( params ) { url . push ( "?" + params ) ; } } return url . join ( "" ) ; } function obj2str ( obj ) { const str = [ ] ; for ( const key in obj ) { if ( str . length ) { str . push ( "&" ) ; } str . push ( key + "=" + encodeURIComponent ( obj [ key ] ) ) ; } return str . join ( "" ) ; }