Last active
March 2, 2022 12:02
-
-
Save DungGramer/16a4571706e4eeee45c628220d3ed5a2 to your computer and use it in GitHub Desktop.
getDomainName
This file contains hidden or 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 getDomainName(url) { | |
if (!/^http|https|wss|ssh|ftp/i.test(url)) { | |
url = `http://${url}`; | |
} | |
let { hostname } = new URL(url); | |
hostname = hostname.split(".").filter(Boolean); | |
function getSuffixIndex(hostname) { | |
const Icann = ["com", "net", "org", "edu", "gov", "mil", "int"]; | |
const CountryTlds = ["ac", "ad", "ae", "af", "ag", "ai", "al", "am", "ao", "aq", "ar", "as", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo", "bq", "br", "bs", "bt", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cw", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sk", "sl", "sm", "sn", "so", "sr", "ss", "st", "su", "sv", "sx", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "za", "zm", "zw", "xn--lgbbat1ad8j", "xn--y9a3aq", "xn--mgbcpq6gpa1a", "xn--54b7fta0cc", "xn--90ais", "xn--90ae", "xn--fiqs8s", "xn--fiqz9s", "xn--wgbh1c", "xn--e1a4c", "xn--qxa6a", "xn--node", "xn--qxam", "xn--j6w193g", "xn--h2brj9c", "xn--mgbbh1a71e", "xn--fpcrj9c3d", "xn--gecrj9c", "xn--s9brj9c", "xn--xkc2dl3a5ee0h", "xn--45brj9c", "xn--2scrj9c", "xn--rvc1e0am3e", "xn--45br5cyl", "xn--3hcrj9c", "xn--mgbbh1a", "xn--h2breg3eve", "xn--h2brj9c8c", "xn--mgbgu82a", "xn--mgba3a4f16a", "xn--mgbtx2b", "xn--4dbrk0ce", "xn--mgbayh7gpa", "xn--80ao21a", "xn--q7ce6a", "xn--mix082f", "xn--mix891f", "xn--mgbx4cd0ab", "xn--mgbah1a3hjkrd", "xn--l1acc", "xn--mgbc0a9azcg", "xn--d1alf", "xn--mgb9awbf", "xn--mgbai9azgqp6j", "xn--ygbi2ammx", "xn--wgbl6a", "xn--p1ai", "xn--mgberp4a5d4ar", "xn--90a3ac", "xn--yfro4i67o", "xn--clchc0ea0b2g2a9gcd", "xn--3e0b707e", "xn--fzc2c9e2c", "xn--xkc2al3hye2a", "xn--mgbpl2fh", "xn--ogbpf8fl", "xn--kprw13d", "xn--kpry57d", "xn--o3cw4h", "xn--pgbs0dh", "xn--j1amh", "xn--mgbaam7a8h", "xn--mgb2ddes", "xn--11b4c3d", "xn--1ck2e1b", "xn--1qqw23a", "xn--30rr7y", "xn--3bst00m", "xn--3ds443g", "xn--3pxu8k", "xn--42c2d9a", "xn--45q11c", "xn--4gbrim", "xn--55qw42g", "xn--55qx5d", "xn--5su34j936bgsg", "xn--5tzm5g", "xn--6frz82g", "xn--6qq986b3xl", "xn--80adxhks", "xn--80aqecdr1a", "xn--80asehdb", "xn--80aswg", "xn--8y0a063a", "xn--9dbq2a", "xn--9et52u", "xn--9krt00a", "xn--b4w605ferd", "xn--bck1b9a5dre4c", "xn--c1avg", "xn--c2br7g", "xn--cck2b3b", "xn--cckwcxetd", "xn--cg4bki", "xn--czr694b", "xn--czrs0t", "xn--czru2d", "xn--d1acj3b", "xn--eckvdtc9d", "xn--efvy88h", "xn--fct429k", "xn--fhbei", "xn--fiq228c5hs", "xn--fiq64b", "xn--fjq720a", "xn--flw351e", "xn--fzys8d69uvgm", "xn--g2xx48c", "xn--gckr3f0f", "xn--gk3at1e", "xn--hxt814e", "xn--i1b6b1a6a2e", "xn--imr513n", "xn--io0a7i", "xn--j1aef", "xn--jlq480n2rg", "xn--jlq61u9w7b", "xn--jvr189m", "xn--kcrx77d1x4a", "xn--kput3i", "xn--mgba3a3ejt", "xn--mgba7c0bbn0a", "xn--mgbaakc7dvf", "xn--mgbab2bd", "xn--mgbca7dzdo", "xn--mgbi4ecexp", "xn--mgbt3dhd", "xn--mk1bu44c", "xn--mxtq1m", "xn--ngbc5azd", "xn--ngbe9e0a", "xn--ngbrx", "xn--nqv7f", "xn--nqv7fs00ema", "xn--nyqy26a", "xn--otu796d", "xn--p1acf", "xn--pssy2u", "xn--q9jyb4c", "xn--qcka1pmc", "xn--rhqv96g", "xn--rovu88b", "xn--ses554g", "xn--t60b56a", "xn--tckwe", "xn--tiq49xqyj", "xn--unup4y", "xn--vermgensberater-ctb", "xn--vermgensberatung-pwb", "xn--vhquv", "xn--vuq861b", "xn--w4r85el8fhu5dnra", "xn--w4rs40l", "xn--xhq521b", "xn--zfr164b"]; | |
const OtherTlds = ["arpa", "xxx", "xyz", "example", "invalid", "local", "localhost", "onion", "test", "ads", "ny", "io", "site", "vov", "msn", "msnbc"]; | |
const indexTlds = []; | |
for (const index in hostname) { | |
if (Icann.includes(hostname[index]) || CountryTlds.includes(hostname[index]) || OtherTlds.includes(hostname[index])) { | |
indexTlds.push(index); | |
} | |
} | |
return indexTlds.length ? Math.min(...indexTlds) : 0; | |
} | |
if (hostname.length >= 3) { | |
const suffixIndex = getSuffixIndex(hostname); | |
return suffixIndex ? hostname[suffixIndex - 1] : hostname[0]; | |
} | |
return hostname[hostname.length - 2]; | |
} |
This file contains hidden or 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
describe("Get domain name", () => { | |
test("test domain Google", () => { | |
expect(getDomainName("https://drive.google.com/drive/u/0/my-drive")).toBe("google"); | |
expect(getDomainName("http://google.com")).toBe("google"); | |
}); | |
test("test domain .*", () => { | |
expect(getDomainName("https://www.com.vn")).toBe("com"); | |
expect(getDomainName("https://abc.www.com")).toBe("www"); | |
expect(getDomainName("https://Abc.allstate.com")).toBe("allstate"); | |
expect(getDomainName("https://Abc.allstate.vn")).toBe("allstate"); | |
expect(getDomainName("https://Abc.czzx.zone.vn")).toBe("zone"); | |
expect(getDomainName("https://tatamotors.vn")).toBe("tatamotors"); | |
expect(getDomainName("https://www.set-edu.com/")).toBe("set-edu"); | |
expect(getDomainName("https://vnexpress.net/")).toBe("vnexpress"); | |
expect(getDomainName("https://ny.gov/")).toBe("ny"); | |
expect(getDomainName("https://dictionary.cambridge.org/")).toBe("cambridge"); | |
expect(getDomainName("https://Pelé@example.com")).toBe("example"); | |
expect(getDomainName("https://vhost.localhost")).toBe("vhost"); | |
expect(getDomainName("https://github.dev")).toBe("github"); | |
}); | |
test("test domain .*.*", () => { | |
expect(getDomainName("ftp://mapasamazonsa.com.ve/")).toBe("mapasamazonsa"); | |
expect(getDomainName("wss://119.92.223.221.prod.hosts.ooklaserver.net:8080/ws")).toBe("ooklaserver"); | |
expect(getDomainName("ssh://user:[email protected]/some/path?and&query#hash")).toBe("example"); | |
expect(getDomainName("http://ho.com.vn")).toBe("ho"); | |
expect(getDomainName("http://tatamotors.com.vn")).toBe("tatamotors"); | |
expect(getDomainName("http://hoc.vtc.vn/")).toBe("vtc"); | |
expect(getDomainName("http://A.b.c.d.com.vn.vn/")).toBe("d"); | |
expect(getDomainName("http://baoninhbinh.org.vn/")).toBe("baoninhbinh"); | |
expect(getDomainName("https://dantri.com.vn/")).toBe("dantri"); | |
expect(getDomainName("https://www.24h.com.vn/")).toBe("24h"); | |
expect(getDomainName("https://www.amherst.ny.us/")).toBe("amherst"); | |
expect(getDomainName("https://a.f.g.h.i.bar.baz.google.com/")).toBe("google"); | |
expect(getDomainName("http://llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch.co.uk/")).toBe("llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch"); | |
}); | |
test("test youtube", () => { | |
expect(getDomainName("https://www.youtube.com/watch?v=gDJJAs8GZGw")).toBe("youtube"); | |
}); | |
test("test domain notion", () => { | |
expect(getDomainName("https://nganpham.notion.site/")).toBe("notion"); | |
}); | |
test("test github page", () => { | |
expect(getDomainName("https://alixsep.github.io/yugilife/#/build")).toBe("github"); | |
}); | |
test("test deep web", () => { | |
expect(getDomainName("https://ulrn6sryqaifefld.onion")).toBe("ulrn6sryqaifefld"); | |
expect(getDomainName("https://auutwvpt2zktxwng.onion/?cat=8")).toBe("auutwvpt2zktxwng"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment