README.md 2.15 KB

parse-domain

Splits an url into sub-domain, domain and top-level-domain.

Since domains are handled differently across different countries and organizations, splitting an url into sub-domain, domain and top-level-domain is not a simple regexp. parse-domain uses a large list of known tlds (borrowed from http://publicsuffix.org) to recognize different parts of the domain.

var parseDomain = require("parse-domain");

expect(parseDomain("some.subdomain.example.co.uk")).to.eql({
    subdomain: "some.subdomain",
    domain: "example",
    tld: "co.uk"
});

expect(parseDomain("https://user:password@example.co.uk:8080/some/path?and&query#hash")).to.eql({
    subdomain: "",
    domain: "example",
    tld: "co.uk"
});

expect(parseDomain("unknown.tld.kk")).to.equal(null);
expect(parseDomain("invalid url")).to.equal(null);
expect(parseDomain({})).to.equal(null);


Setup

npm status

Build Status Dependency Status Coverage Status

browser support


API

parseDomain(url: String): ParsedDomain|null

Returns null if url has an unknown tld or if it's not a valid url.

ParsedDomain

{
    tld: String,
    domain: String,
    subdomain: String
}


License

Unlicense