UNPKG

wix-style-react

Version:
49 lines (40 loc) 1.01 kB
/** * RegExps. * A URL must match #1 and then at least one of #2/#3. * Use two levels of REs to avoid REDOS. */ const protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/; const protocolRE = /^https?:\/\/.+$/i; const localhostDomainRE = /^localhost[\:?\d]*(?:[^\:?\d]\S*)?$/; const nonLocalhostDomainRE = /^[^\s\.]+\.\S{2,}$/; /** * Loosely validate a URL `string`. * * @param {String} string * @return {Boolean} */ export function isUrl(string) { if (typeof string !== 'string') { return false; } const match = string.match(protocolAndDomainRE); if (!match) { return false; } const everythingAfterProtocol = match[1]; if (!everythingAfterProtocol) { return false; } return ( localhostDomainRE.test(everythingAfterProtocol) || nonLocalhostDomainRE.test(everythingAfterProtocol) ); } /** * Prepend `http://` to relative url. * * @param {String} url * @return {String} */ export const prependHTTP = url => url && (protocolRE.test(url) ? url : `http://${url}`);