Git Product home page Git Product logo

tld.js's Introduction

tld.js Build Status

tld.js is JavaScript API to work against complex domain names, subdomains and well-known TLDs.

It answers with accuracy to questions like what is mail.google.com domain?, what is a.b.ide.kyoto.jp subdomain? and is https://big.data TLD a well-known one?.

tld.js runs fast, is fully tested and works both in Node.js and in the browser. Because it relies on Mozilla's public suffix list, now is a good time to say thank you Mozilla!

Install

# With bundled Top Level Domains list
npm install --save tldjs

# You can get an up-to-date Top Level Domains list during the install
npm install --save tldjs --tldjs-update-rules

The latter is useful if this package has not been published for a while on npm.

Using It

Node.js

const { getDomain } = require('tldjs');

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

Browser

A browser version is made available thanks to browserify CDN.

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

You can build your own browser bundle with browserify:

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

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

API

tldjs can be use either as a whole, or using destructuring.

// ES2015 modules syntax
import tldjs from 'tldjs';
import { getDomain } from 'tldjs';

// Node/CommonJS modules syntax
const tldjs = require('tldjs');
const { getDomain } = require('tldjs');

tldExists()

Checks if the TLD is well-known for a given string — parseable with require('url').parse.

const { tldExists } = tldjs;

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

getDomain()

Returns the fully qualified domain from a given string — parseable with require('url').parse.

const { getDomain } = tldjs;

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

getSubdomain()

Returns the complete subdomain for a given string — parseable with require('url').parse.

const { getSubdomain } = tldjs;

getSubdomain('google.com');             // returns ``
getSubdomain('fr.google.com');          // returns `fr`
getSubdomain('google.co.uk');           // returns ``
getSubdomain('foo.google.co.uk');       // returns `foo`
getSubdomain('moar.foo.google.co.uk');  // returns `moar.foo`
getSubdomain('t.co');                   // returns ``
getSubdomain('fr.t.co');                // returns `fr`
getSubdomain('https://user:[email protected]:443/some/path?and&query#hash'); // returns `secure`

getPublicSuffix()

Returns the public suffix for a given string — parseable with require('url').parse.

const { getPublicSuffix } = tldjs;

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

isValid()

Checks the validity of a given string — parseable with require('url').parse. It does not check if the TLD is well-known.

const { isValid } = tldjs;

isValid('google.com');      // returns `true`
isValid('.google.com');     // returns `false`
isValid('my.fake.domain');  // returns `true`
isValid('localhost');       // returns `false`
isValid('https://user:[email protected]:8080/some/path?and&query#hash'); // returns `true`

Troubleshooting

Retrieving subdomain of localhost and custom hostnames

tld.js methods getDomain and getSubdomain are designed to work only with known and valid TLDs. This way, you can trust what a domain is.

localhost is a valid hostname but not a TLD. Although you can instanciate your own flavour of tld.js with additional valid hosts:

const tldjs = require('tldjs');

tldjs.getDomain('localhost');           // returns null
tldjs.getSubdomain('vhost.localhost');  // returns null

const myTldjs = tldjs.fromUserSettings({
  validHosts: ['localhost']
});

myTldjs.getDomain('localhost');           // returns 'localhost'
myTldjs.getSubdomain('vhost.localhost');  // returns 'vhost'

Updating the TLDs List

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

tld.js bundles a list of known TLDs but this list can become outdated. This is especially true if the package have not been updated on npm for a while.

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 passing the --tldjs-update-rules to your npm install command:

# anytime you reinstall your project
npm install --tldjs-update-rules

# or if you add the dependency to your project
npm install --save tldjs --tldjs-update-rules

Open an issue to request an update of the bundled TLDs.

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.

Performances

While interpreting the results, keep in mind that each "op" reported by the benchmark is processing 24 domains
tldjs#isValid x 1,952,688 ops/sec ±1.27% (86 runs sampled)
tldjs#cleanHost x 11,901 ops/sec ±1.53% (84 runs sampled)
tldjs#tldExists x 10,030 ops/sec ±2.20% (83 runs sampled)
tldjs#getDomain x 3,705 ops/sec ±4.86% (71 runs sampled)
tldjs#getSubdomain x 3,035 ops/sec ±1.58% (81 runs sampled)
tldjs#getPublicSuffix x 7,038 ops/sec ±1.88% (83 runs sampled)

You can measure the performance of tld.js on your hardware by running the following command:

npx tldjs -c './bin/benchmark.js'

License

MIT License.

tld.js's People

Contributors

thom4parisot avatar remusao avatar olivoil avatar jhnns avatar ghostwords avatar jdesboeufs avatar kellycampbell avatar krinkle avatar yehezkielbs avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.