export function isUrl ( url ) { try { return typeof new URL ( url ) . hostname === 'string' ; } catch ( e ) { if ( ! ( e instanceof TypeError ) ) { console . log ( `URL is not valid: ${ e }` ) ; } } return false ; } export function resolveUrl ( baseUrl , url ) { let parsedUrl = undefined ; try { if ( url ) { parsedUrl = new URL ( url ) ; } } catch ( e ) { if ( ! ( e instanceof TypeError ) ) { console . error ( e ) ; } } if ( ! parsedUrl ) { try { if ( baseUrl && url ) { parsedUrl = new URL ( url , baseUrl ) ; } } catch ( e ) { console . error ( e ) ; } } if ( ! parsedUrl && baseUrl ) { return new URL ( baseUrl ) ; } return parsedUrl ; } export function validateUrl ( url , base ? ) { try { new URL ( url , base ) ; return null ; } catch ( urlError ) { return `${ urlError }` ; } } export function cleanUrl ( url ) { let cleanedUrl ; if ( url && ! url . startsWith ( 'http' ) && ! url . startsWith ( 'https' ) ) { cleanedUrl = 'https://' + url ; } if ( cleanedUrl ) { const test = isValidURL ( cleanedUrl ) ; if ( test === false && ! url . toLowerCase ( ) . startsWith ( 'http://' ) ) { throw new Error ( 'This error means that you may have a bad https cert or the url may not be correct' ) ; } return cleanedUrl ; } return url ; } export function isValidURL ( str ) { var pattern = new RegExp ( '^((https?:)?\\/\\/)?' + '(?:\\S+(?::\\S*)?@)?(([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}' + '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + '(\\?[;&a-z\\d%_.~+=-]*)?' + '(\\\\#[-a-z\\\\d_]*)?' , 'i' ) ; return ! ! pattern . test ( str ) ; }