Created
May 17, 2017 13:19
-
-
Save stewartcelani/8642cc07a69fbcddf955311450be22db to your computer and use it in GitHub Desktop.
Extract Root Domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function extractDomain(url) { | |
return new Promise(function (resolve, reject) { | |
var domain; | |
//find & remove protocol (http, ftp, etc.) and get domain | |
if (url.indexOf("://") > -1) { | |
domain = url.split('/')[2]; | |
} | |
else { | |
domain = url.split('/')[0]; | |
} | |
domain = domain.split(':')[0]; | |
return resolve(domain); | |
}) | |
} | |
function extractDomainRoot(domain){ | |
// Works back from end segment and returns first segment > 4 characters | |
// Dumb implementation as does not work for domains <= 4 characters but does the job asfar as augmenting | |
// ElasticSearch hits | |
let parts = domain.split('.') | |
for (let i = parts.length; i--; i >= 0) { | |
if (parts[i].length > 4) { | |
return parts[i] | |
} | |
} | |
}; | |
const test = 'http://molstaging.elasticdesign.com.au' | |
extractDomain(test).then((domain) => { | |
console.log(extractDomainRoot(domain)) | |
}).catch((err) => { | |
console.error(err) | |
}) | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function extractDomain(url) { | |
return new Promise(function (resolve, reject) { | |
var domain; | |
//find & remove protocol (http, ftp, etc.) and get domain | |
if (url.indexOf("://") > -1) { | |
domain = url.split('/')[2]; | |
} | |
else { | |
domain = url.split('/')[0]; | |
} | |
domain = domain.split(':')[0]; | |
return resolve(domain); | |
}) | |
} | |
function extractDomainRoot(domain){ | |
// Works back from end segment and returns first segment > 4 characters | |
// Dumb implementation as does not work for domains <= 4 characters but does the job asfar as augmenting | |
// ElasticSearch hits | |
let parts = domain.split('.') | |
for (let i = parts.length; i--; i >= 0) { | |
if (parts[i].length > 4) { | |
return parts[i] | |
} | |
} | |
}; | |
const test = 'http://molstaging.elasticdesign.com.au' | |
extractDomain(test).then((domain) => { | |
console.log(extractDomainRoot(domain)) | |
}).catch((err) => { | |
console.error(err) | |
})</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function extractDomain(url) { | |
return new Promise(function (resolve, reject) { | |
var domain; | |
//find & remove protocol (http, ftp, etc.) and get domain | |
if (url.indexOf("://") > -1) { | |
domain = url.split('/')[2]; | |
} | |
else { | |
domain = url.split('/')[0]; | |
} | |
domain = domain.split(':')[0]; | |
return resolve(domain); | |
}) | |
} | |
function extractDomainRoot(domain){ | |
// Works back from end segment and returns first segment > 4 characters | |
// Dumb implementation as does not work for domains <= 4 characters but does the job asfar as augmenting | |
// ElasticSearch hits | |
let parts = domain.split('.') | |
for (let i = parts.length; i--; i >= 0) { | |
if (parts[i].length > 4) { | |
return parts[i] | |
} | |
} | |
}; | |
const test = 'http://molstaging.elasticdesign.com.au' | |
extractDomain(test).then((domain) => { | |
console.log(extractDomainRoot(domain)) | |
}).catch((err) => { | |
console.error(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment