UNPKG

wix-style-react

Version:
56 lines (45 loc) 1.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isUrl = isUrl; exports.prependHTTP = void 0; /** * RegExps. * A URL must match #1 and then at least one of #2/#3. * Use two levels of REs to avoid REDOS. */ var protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/; var protocolRE = /^https?:\/\/.+$/i; var localhostDomainRE = /^localhost[\:?\d]*(?:[^\:?\d]\S*)?$/; var nonLocalhostDomainRE = /^[^\s\.]+\.\S{2,}$/; /** * Loosely validate a URL `string`. * * @param {String} string * @return {Boolean} */ function isUrl(string) { if (typeof string !== 'string') { return false; } var match = string.match(protocolAndDomainRE); if (!match) { return false; } var everythingAfterProtocol = match[1]; if (!everythingAfterProtocol) { return false; } return localhostDomainRE.test(everythingAfterProtocol) || nonLocalhostDomainRE.test(everythingAfterProtocol); } /** * Prepend `http://` to relative url. * * @param {String} url * @return {String} */ var prependHTTP = function prependHTTP(url) { return url && (protocolRE.test(url) ? url : "http://".concat(url)); }; exports.prependHTTP = prependHTTP;