@wordpress/url
Version:
WordPress URL utilities.
32 lines (27 loc) • 611 B
JavaScript
/**
* Internal dependencies
*/
import { isEmail } from './is-email';
const USABLE_HREF_REGEXP = /^(?:[a-z]+:|#|\?|\.|\/)/i;
/**
* Prepends "http://" to a url, if it looks like something that is meant to be a TLD.
*
* @param {string} url The URL to test.
*
* @example
* ```js
* const actualURL = prependHTTP( 'wordpress.org' ); // http://wordpress.org
* ```
*
* @return {string} The updated URL.
*/
export function prependHTTP( url ) {
if ( ! url ) {
return url;
}
url = url.trim();
if ( ! USABLE_HREF_REGEXP.test( url ) && ! isEmail( url ) ) {
return 'http://' + url;
}
return url;
}