2016-07-13 12:44:24 -08:00
|
|
|
import * as regex from './url-regex';
|
|
|
|
|
|
2016-07-28 11:12:21 -08:00
|
|
|
export const domainRegex = /\b((?=[a-z0-9-]{1,63}\.)(xn--)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}\b|^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*:)*?:?0*1$/;
|
2016-07-14 15:40:15 -08:00
|
|
|
|
2016-09-21 06:27:11 -08:00
|
|
|
export default function isUrlCommand(shell, data) {
|
|
|
|
|
const matcher = regex[shell]; // eslint-disable-line import/namespace
|
|
|
|
|
if (undefined === matcher || !data) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-07-14 15:40:15 -08:00
|
|
|
|
2016-07-20 09:08:12 -08:00
|
|
|
const match = data.match(matcher);
|
2016-09-21 06:27:11 -08:00
|
|
|
if (!match) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-07-20 09:08:12 -08:00
|
|
|
const protocol = match[1];
|
|
|
|
|
const path = match[2];
|
2016-07-14 15:40:15 -08:00
|
|
|
|
2016-07-20 09:08:12 -08:00
|
|
|
if (path) {
|
|
|
|
|
if (protocol) {
|
|
|
|
|
return `${protocol}${path}`;
|
|
|
|
|
}
|
|
|
|
|
// extract the domain portion from the url
|
|
|
|
|
const domain = path.split('/')[0];
|
|
|
|
|
if (domainRegex.test(domain)) {
|
|
|
|
|
return `http://${path}`;
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-14 15:40:15 -08:00
|
|
|
return null;
|
2016-07-13 12:44:24 -08:00
|
|
|
}
|