prepend-http
Version:
Prepend `https://` to humanized URLs like sindresorhus.com and localhost
14 lines (10 loc) • 354 B
JavaScript
export default function prependHttp(url, {https = true} = {}) {
if (typeof url !== 'string') {
throw new TypeError(`Expected \`url\` to be of type \`string\`, got \`${typeof url}\``);
}
url = url.trim();
if (/^\.*\/|^(?!localhost)\w+?:/.test(url)) {
return url;
}
return url.replace(/^(?!(?:\w+?:)?\/\/)/, https ? 'https://' : 'http://');
}