2016-02-20 03:39:51 +01:00
|
|
|
import url from 'url';
|
|
|
|
import validator from 'validator';
|
|
|
|
|
|
|
|
function normalizeUrl(testUrl) {
|
|
|
|
// add protocol if protocol not found
|
|
|
|
let normalized = testUrl;
|
|
|
|
const parsed = url.parse(normalized);
|
|
|
|
if (!parsed.protocol) {
|
|
|
|
normalized = 'http://' + normalized;
|
|
|
|
}
|
2017-04-18 23:30:00 +02:00
|
|
|
|
|
|
|
const validatorOptions = {
|
|
|
|
require_protocol: true,
|
|
|
|
require_tld: false,
|
|
|
|
allow_trailing_dot: true // mDNS addresses, https://github.com/jiahaog/nativefier/issues/308
|
|
|
|
};
|
|
|
|
if (!validator.isURL(normalized, validatorOptions)) {
|
2016-02-20 03:39:51 +01:00
|
|
|
throw `Your Url: "${normalized}" is invalid!`;
|
|
|
|
}
|
|
|
|
return normalized;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default normalizeUrl;
|