export class UrlHelper { param url public static isValidUrl ( url ) { return /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/ . test ( url ) ; } param { String } field param { String } url return { String } public static getQueryStringParam ( field , url ) { const href = url ? url : window . location . href ; const reg = new RegExp ( "[?&#]" + field + "=([^&#]*)" , "i" ) ; const qs = reg . exec ( href ) ; return qs ? qs [ 1 ] : null ; } param { String } field param { String } sourceURL return { String } public static removeQueryStringParam ( field , sourceURL ) { let rtn = sourceURL . split ( "?" ) [ 0 ] ; let param = null ; let paramsArr = [ ] ; const hash = window . location . hash ; const queryString = ( sourceURL . indexOf ( "?" ) !== - 1 ) ? sourceURL . split ( "?" ) [ 1 ] : "" ; if ( queryString !== "" ) { paramsArr = queryString . split ( "&" ) ; for ( let i = paramsArr . length - 1 ; i >= 0 ; i -= 1 ) { param = paramsArr [ i ] . split ( "=" ) [ 0 ] ; if ( param === field ) { paramsArr . splice ( i , 1 ) ; } } if ( paramsArr . length > 0 ) { rtn = rtn + "?" + paramsArr . join ( "&" ) . replace ( hash , '' ) + hash ; } } return rtn ; } param url param param param value public static addOrReplaceQueryStringParam ( url , param , value ) { const re = new RegExp ( "[\\?&]" + param + "=([^&#]*)" ) ; const match = re . exec ( url ) ; let delimiter ; let newString ; if ( match === null ) { const hash = window . location . hash && window . location . hash !== '' ? window . location . hash : '#' ; const hasQuestionMark = /\?/ . test ( url ) ; delimiter = hasQuestionMark ? "&" : "?" ; newString = url . replace ( hash , '' ) + delimiter + param + "=" + encodeURIComponent ( value ) + hash ; } else { delimiter = match [ 0 ] . charAt ( 0 ) ; newString = url . replace ( re , delimiter + param + "=" + encodeURIComponent ( value ) ) ; } return newString ; } returns public static getQueryStringParams ( ) { let queryStringParameters = { } ; const urlParams = new URLSearchParams ( window . location . search ) ; urlParams . forEach ( ( value , key ) => { queryStringParameters [ key ] = value ; } ) ; return queryStringParameters ; } param encodedStr public static decode ( encodedStr ) { const domParser = new DOMParser ( ) ; const htmlContent = domParser . parseFromString ( `<!doctype html><body>${ encodedStr }</body>` , 'text/html' ) ; return htmlContent . body . textContent ; } param item public static getGraphPreviewUrl ( url ) { if ( url ) { url = UrlHelper . createOdspPreviewUrl ( url ) ; } return url ; } public static createOdspPreviewUrl ( url ) { let previewUrl = url ; if ( url ) { const matches = url . match ( /^(http[s]?:\/\/[^\/]*)(.+)\/(.+)$/ ) ; if ( matches ) { const [ host , path , file ] = matches . slice ( 1 ) ; if ( host && path && file ) { previewUrl = `${ host }${ path }/?id=${ path }/${ file }&parent=${ path }` ; } } } return previewUrl ; } } export enum PageOpenBehavior { "Self" = "Self" , "NewTab" = "NewTab" } export enum QueryPathBehavior { "URLFragment" = "URLFragment" , "QueryParameter" = "QueryParameter" }