hd-utils
Version:
A handy utils for modern JS developers
15 lines (14 loc) • 638 B
JavaScript
import { httpURLRegex } from "../../regex/index";
/**
* @description If the url doesn't contain http, add it to the url, otherwise return the url.
* @param {string} url - string - the url to check
* @param {boolean} secured - https instead of http
* @example addHttpToURL("www.google.com") => "http://www.google.com"
* @example addHttpToURL("www.google.com", true) => "https://www.google.com"
* @returns {string}
*/
export default function addHttpToURL(url, secured = false) {
return url.search(httpURLRegex) === -1
? `${secured ? 'https' : 'http'}://${url.trim()}`
: url.trim(); //check if url contains http
}