@wordpress/url
Version:
WordPress URL utilities.
34 lines (29 loc) • 677 B
text/typescript
/**
* Internal dependencies
*/
import { prependHTTP } from './prepend-http';
/**
* Prepends "https://" to a url, if it looks like something that is meant to be a TLD.
*
* Note: this will not replace "http://" with "https://".
*
* @param url The URL to test.
*
* @example
* ```js
* const actualURL = prependHTTPS( 'wordpress.org' ); // https://wordpress.org
* ```
*
* @return The updated URL.
*/
export function prependHTTPS( url: string ): string {
if ( ! url ) {
return url;
}
// If url starts with http://, return it as is.
if ( url.startsWith( 'http://' ) ) {
return url;
}
url = prependHTTP( url );
return url.replace( /^http:/, 'https:' );
}