Coder Social home page Coder Social logo

olivierbeaulieu / tld.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thom4parisot/tld.js

0.0 2.0 0.0 529 KB

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

Home Page: https://npmjs.com/tldjs

License: MIT License

JavaScript 100.00%

tld.js's Introduction

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

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

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)
tld.tldExists('https://user:[email protected]:8080/some/path?and&query#hash'); // returns `true`

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`
tld.getDomain('https://user:[email protected]:8080/some/path?and&query#hash'); // returns `example.co.uk`

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`
tld.getSubdomain('https://user:[email protected]:8080/some/path?and&query#hash'); // returns ``

getPublicSuffix()

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`

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`
tld.isValid('https://user:[email protected]:8080/some/path?and&query#hash'); // returns `true`

Troubleshouting

Retrieving subdomain of localhost and custom hostnames

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

Unfortunately, localhost is a valid hostname but it is not a TLD. tld.js has a concept of validHosts you declare

var tld = require('tldjs');

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

tld.validHosts = ['localhost'];

tld.getDomain('localhost');           // returns 'localhost'
tld.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?

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

Alternatively, you can launch the updater through its API:

var updater = require('tldjs/lib/updater');
updater.run(function done(){
  // do something when update is performed
});

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.

tld.js's People

Contributors

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

Watchers

James Cloos avatar Olivier Beaulieu 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.