tldjs

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

npm install tldjs
24 downloads in the last day
205 downloads in the last week
715 downloads in the last month

tld.js Build Status

browser support

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

Node.js

var tld = require('tldjs');

tld.getDomain('mail.google.co.uk');
// -> 'google.co.uk'

Browser

<script src="bower_components/tld/dist/tld.min.js">
<script>
tld.getDomain('mail.google.co.uk');
// -> 'google.co.uk'
</script>

Browser with AMD / Require.js

<script>
require(['tld'], function(tld){
  tld.getDomain('mail.google.co.uk');
  // -> 'google.co.uk'
});
</script>

API

getDomain()

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`

tldExists()

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)

getSubdomain()

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`

isValid()

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`

Troubleshouting

Updating the TLDs List

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-script build

A fresh copy will be located in src/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.

npm loves you