mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2026-02-25 09:30:54 -08:00
28 lines
662 B
JavaScript
28 lines
662 B
JavaScript
/* eslint-disable prefer-destructuring */
|
|
const extractHostname = (url) => {
|
|
try {
|
|
let hostname = url.trim();
|
|
|
|
// find & remove protocol (http, ftp, etc.) and get hostname
|
|
if (url.indexOf('://') > -1) {
|
|
hostname = url.split('/')[2];
|
|
} else {
|
|
hostname = url.split('/')[0];
|
|
}
|
|
|
|
// find & remove port number
|
|
hostname = hostname.split(':')[0];
|
|
// find & remove "?"
|
|
hostname = hostname.split('?')[0];
|
|
|
|
// find & remove "www"
|
|
// https://stackoverflow.com/a/9928725
|
|
hostname = hostname.replace(/^(www\.)/, '');
|
|
|
|
return hostname;
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
};
|
|
|
|
module.exports = extractHostname;
|