import pathToRegexp = require ( 'path-to-regexp' ) ; import { Key } from 'path-to-regexp' ; import { FunctionUtility } from './function.utility' ; import { ObjectUtility } from './object.utility' ; import { DataUtility } from './data.utility' ; import { IKeyValuePair } from '../types' ; export abstract class RouteUtility { static getNamedSegmentKeyValues ( originalUri , requestedUri ) { let keys = [ ] ; let route = pathToRegexp ( originalUri , keys ) ; let values = route . exec ( requestedUri ) ; if ( ! DataUtility . isArray ( values ) ) { return { } ; } let i = 1 ; let obj = { } ; for ( const key of keys ) { obj [ key . name ] = values [ i ] ; i ++ ; } return obj ; } static mapSegmentsAndQueryToActionArguments ( originalUri , requestedUri , queryString , params ) { let arr = [ ] ; if ( params . length === 0 ) return [ ] ; let val = RouteUtility . getNamedSegmentKeyValues ( originalUri , requestedUri ) ; let values = Object . assign ( val , queryString ) ; ObjectUtility . keys ( values ) . forEach ( key => { let index = params . findIndex ( e => e === key ) ; arr [ index ] = values [ key ] ; } ) ; return params . map ( ( val , index ) => { return { key : val , value : arr [ index ] } ; } ) ; } }