diff --git a/tldjs/index.d.ts b/tldjs/index.d.ts new file mode 100644 index 0000000000..6917c8f067 --- /dev/null +++ b/tldjs/index.d.ts @@ -0,0 +1,48 @@ +// Type definitions for tldjs v1.7 +// Project: https://github.com/oncletom/tld.js +// Definitions by: Joshua DeVinney +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * 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. + * Optionally use validHosts to add it. `tld.validHosts = ['localhost'];` + */ +export declare var validHosts: string[]; + +/** + * Checks if the TLD is valid for a given host. + * @param host The TLD/host/url to check + * @return {boolean} + */ +export declare function tldExists(host: string): boolean; + +/** + * Returns the fully qualified domain from a host string. + * @param host The host/url to check + * @return {string|null} a domain string if any, otherwise null + */ +export declare function getDomain(host: string): string | null; + +/** + * Returns the complete subdomain for a given host. + * @param host The host/url to check + * @return {string|null} a subdomain string if any, blank string if subdomain is empty, otherwise null + */ +export declare function getSubdomain(host: string): string | null; + +/** + * Returns the public suffix (including exact matches) + * @param host The host/url to check + * @return {string|null} a public suffix string if any, otherwise null + */ +export declare function getPublicSuffix(host: string): string | null; + +/** + * Checking if a host string is valid + * It's usually a preliminary check before trying to use getDomain or anything else + * Beware: it does not check if the TLD exists. + * @param host The host/url to check + * @return {boolean} + */ +export declare function isValid(host: string): string | null; diff --git a/tldjs/tldjs-tests.ts b/tldjs/tldjs-tests.ts new file mode 100644 index 0000000000..d405563853 --- /dev/null +++ b/tldjs/tldjs-tests.ts @@ -0,0 +1,49 @@ +import * as tld from 'tldjs'; + +tld.tldExists('google.com'); // returns `true` +tld.tldExists('google.local'); // 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` + +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` + +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 `` + +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` + +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` + +// You'll need to use this import syntax to set valid hosts +import tld2 = require('tldjs'); + +tld2.getDomain('localhost'); // returns null +tld2.getSubdomain('vhost.localhost'); // returns null + +tld2.validHosts = ['localhost']; + +tld2.getDomain('localhost'); // returns 'localhost' +tld2.getSubdomain('vhost.localhost'); // returns 'vhost' diff --git a/tldjs/tsconfig.json b/tldjs/tsconfig.json new file mode 100644 index 0000000000..97866b9ac4 --- /dev/null +++ b/tldjs/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "tldjs-tests.ts" + ] +} \ No newline at end of file