tldjs

JavaScript API to work against complex domain names, subdomains and URIs.

tld.js

tld.js is JavaScript API to work against complex domain names, subdomains and URIs.

It answers with accuracy to questions like what is the domain/subdomain of mail.google.com and a.b.ide.kyoto.jp?

tld.js is fully tested, works in Node.js and in the browser, with or without AMD. Its database keeps up to date thanks to Mozilla's public suffix list to have and keep up to date with domain names.

Thanks Mozilla!

Install

npm bower component
npm install --save tldjs bower install --save tld component install tld

Using It

var tld = require('tldjs');
 
tld.getDomain('mail.google.co.uk');
// -> 'google.co.uk' 

A browser version is made available thanks to Browserify CDN.

<script src="http://wzrd.in/standalone/tldjs">
<script>
tldjs.getDomain('mail.google.co.uk');
// -> 'google.co.uk'
</script> 

You can build your own by using browserify:

npm install --save tldjs
browserify -s tld -r node_modules/tldjs/index.js -o tld.js

An UMD module will be created as of tld.js.

API

Checks if the TLD is valid for a given host.

tld.tldExists('google.com'); // returns `true` 
tld.tldExists('google.google'); // returns `false` (not an explicit registered TLD) 
tld.tldExists('com'); // returns `true` 
tld.tldExists('uk'); // returns `true` 
tld.tldExists('co.uk'); // returns `true` (because `uk` is a valid TLD) 
tld.tldExists('amazon.fancy.uk'); // returns `true` (still because `uk` is a valid TLD) 
tld.tldExists('amazon.co.uk'); // returns `true` (still because `uk` is a valid TLD) 
tld.tldExists('https://user:password@example.co.uk:8080/some/path?and&query#hash'); // returns `true` 

Returns the fully qualified domain from a host string.

tld.getDomain('google.com'); // returns `google.com` 
tld.getDomain('fr.google.com'); // returns `google.com` 
tld.getDomain('fr.google.google'); // returns `google.google` 
tld.getDomain('foo.google.co.uk'); // returns `google.co.uk` 
tld.getDomain('t.co'); // returns `t.co` 
tld.getDomain('fr.t.co'); // returns `t.co` 
tld.getDomain('https://user:password@example.co.uk:8080/some/path?and&query#hash'); // returns `example.co.uk` 

Returns the complete subdomain for a given host.

tld.getSubdomain('google.com'); // returns `` 
tld.getSubdomain('fr.google.com'); // returns `fr` 
tld.getSubdomain('google.co.uk'); // returns `` 
tld.getSubdomain('foo.google.co.uk'); // returns `foo` 
tld.getSubdomain('moar.foo.google.co.uk'); // returns `moar.foo` 
tld.getSubdomain('t.co'); // returns `` 
tld.getSubdomain('fr.t.co'); // returns `fr` 
tld.getSubdomain('https://user:password@example.co.uk:8080/some/path?and&query#hash'); // returns `` 

Returns the public suffix for a given host.

tld.getPublicSuffix('google.com'); // returns `com` 
tld.getPublicSuffix('fr.google.com'); // returns `com` 
tld.getPublicSuffix('google.co.uk'); // returns `co.uk` 
tld.getPublicSuffix('s3.amazonaws.com'); // returns `s3.amazonaws.com` 

Checks if the host string is valid. It does not check if the tld exists.

tld.isValid('google.com'); // returns `true` 
tld.isValid('.google.com'); // returns `false` 
tld.isValid('my.fake.domain'); // returns `true` 
tld.isValid('localhost'); // returns `false` 
tld.isValid('https://user:password@example.co.uk:8080/some/path?and&query#hash'); // returns `true` 

Troubleshouting

Many libraries offer a list of TLDs. But, are they up-to-date? And how to update them?

Hopefully for you, even if I'm flying over the world, if I've lost my Internet connection or even if you do manage your own list, you can update it by yourself, painlessly.

How? By typing this in your console

npm run build

A fresh copy will be made available as ./rules.json.

Open an issue to request an update in all package systems (or do a PR with a bugfix version bump).

Contributing

Provide a pull request (with tested code) to include your work in this main project. Issues may be awaiting for help so feel free to give a hand, with code or ideas.