Skip to content

Instantly share code, notes, and snippets.

@ondrej-kvasnovsky
Created April 17, 2018 18:27
Show Gist options
  • Save ondrej-kvasnovsky/a5eb335d405b2a3348346cdb3efd0f6a to your computer and use it in GitHub Desktop.
Save ondrej-kvasnovsky/a5eb335d405b2a3348346cdb3efd0f6a to your computer and use it in GitHub Desktop.
snowflake udf to extract domain from json or url
const data1 = {
attributes: {
domain: 'from.attributes.com'
}
}
const data2 = {
attributes: {
unknownAttributes: {
domain: 'from.unknownAttributes.com'
}
}
}
const data3 = {
attributes: {
url:
'https://www.fromurl.com/Zinus-SmartBase-Foundation-Replacement-Noise-Free/dp/B01GHHIWFO/ref=sr_1_1/146-9491966-1844353?ie=UTF8&qid=1521436930&sr=8-1&keywords=841550091137'
}
}
function getDomain(attributes) {
if (!attributes) {
return null
}
if (attributes) {
if (attributes.domain) {
return attributes.domain
}
const unknownAttributes = attributes.unknownAttributes
if (unknownAttributes) {
if (unknownAttributes.domain) {
return unknownAttributes.domain
}
}
const url = attributes.url
if (url) {
const match = url
.replace(/^(https?:)/i, '')
.replace(/(www[0-9]?\.)/i, '')
.split('/')
return match[2]
}
}
}
console.log(getDomain(null))
console.log(getDomain(undefined))
console.log(getDomain(data1.attributes))
console.log(getDomain(data2.attributes))
console.log(getDomain(data3.attributes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment