import string from '../string/index' ; import array from '../array/index' ; import fn from '../fn/index' ; obj desc method method const net = { parseParams : function ( locationSearch ) : object { let first = ( arr ) => arr [ 0 ] ; let last = ( arr ) => arr [ arr . length - 1 ] ; let hasQuestionMark = ( locationSearch ) => locationSearch . includes ( '?' ) ? locationSearch : `?${ locationSearch }` ; let separateOutParams = fn . compose < string > ( last , string . split ( '?' ) , hasQuestionMark ) ; let groupingParams = fn . compose < string [ ] [ ] > ( array . map ( string . split ( '=' ) ) , string . split ( '&' ) ) ; let getParams = ( paramsArr ) => { let params = { } ; paramsArr . forEach ( ( param ) => { if ( first ( param ) ) { params [ first ( param ) ] = last ( param ) ; } } ) ; return params ; } ; let paramsStr = separateOutParams ( locationSearch ) ; let paramsArr = groupingParams ( paramsStr ) ; let params = getParams ( paramsArr ) ; return params ; } , parseUrl : function ( url ) : UrlInfoObject { const HOST_PORT_REGEXP = /^(http:\/\/|https:\/\/)[0-9a-zA-Z.:]+/ ; const HOST_REGEXP = /^(http:\/\/|https:\/\/)[0-9a-zA-Z.]+/ ; let first = ( arr ) => arr [ 0 ] ; let last = ( arr ) => arr [ arr . length - 1 ] ; let checkPort = ( port ) => port ? port : 80 ; let checkPath = ( path ) => path ? path : '/' ; let checkParams = ( url ) => url . includes ( '?' ) ? url : '' ; let separateOutHref = fn . compose < string > ( first , string . split ( '?' ) ) ; let separateOutParams = fn . compose < string > ( last , string . split ( '?' ) , checkParams ) ; let matchHost = fn . compose < string > ( first , string . match ( HOST_PORT_REGEXP ) ) ; let groupingParams = fn . compose < string [ ] [ ] > ( array . map < string > ( string . split ( '=' ) ) , string . split ( '&' ) ) ; let getParams = ( paramsArr ) => { let params = { } ; paramsArr . forEach ( ( param ) => { if ( first ( param ) ) { params [ first ( param ) ] = last ( param ) ; } } ) ; return params ; } ; let getHost = fn . compose < string > ( first , string . match ( HOST_REGEXP ) ) ; let getPort = ( host ) => fn . compose < number > ( checkPort , last , string . split ( ':' ) , last , string . split ( host ) ) ; let href = separateOutHref ( url ) ; let paramsStr = separateOutParams ( url ) ; let hostWithPort = matchHost ( href ) ; let host = getHost ( hostWithPort ) ; let port = getPort ( host ) ( hostWithPort ) ; let path = fn . compose < string > ( checkPath , last , string . split ( hostWithPort ) ) ( href ) ; let paramsArr = groupingParams ( paramsStr ) ; let params = getParams ( paramsArr ) ; const urlMsg = { url , host , port , path , params } ; return urlMsg ; } } ; export default net ; export const parseParams = net . parseParams ; export const parseUrl = net . parseUrl ;