wallet-core/node_modules/is-url/index.js

48 lines
856 B
JavaScript
Raw Normal View History

2017-05-28 00:38:50 +02:00
/**
* Expose `isUrl`.
*/
module.exports = isUrl;
/**
2018-09-20 02:56:13 +02:00
* RegExps.
* A URL must match #1 and then at least one of #2/#3.
* Use two levels of REs to avoid REDOS.
2017-05-28 00:38:50 +02:00
*/
2018-09-20 02:56:13 +02:00
var protocolAndDomainRE = /^(?:\w+:)?\/\/(\S+)$/;
var localhostDomainRE = /^localhost[\:?\d]*(?:[^\:?\d]\S*)?$/
var nonLocalhostDomainRE = /^[^\s\.]+\.\S{2,}$/;
2017-05-28 00:38:50 +02:00
/**
* Loosely validate a URL `string`.
*
* @param {String} string
* @return {Boolean}
*/
function isUrl(string){
2018-09-20 02:56:13 +02:00
if (typeof string !== 'string') {
return false;
}
var match = string.match(protocolAndDomainRE);
if (!match) {
return false;
}
var everythingAfterProtocol = match[1];
if (!everythingAfterProtocol) {
return false;
}
if (localhostDomainRE.test(everythingAfterProtocol) ||
nonLocalhostDomainRE.test(everythingAfterProtocol)) {
return true;
}
return false;
2017-05-28 00:38:50 +02:00
}