tldjs typing [Types-2.0] (#11714)

* tldjs v1.7 typing

* Making things top level for tldjs
This commit is contained in:
Joshua DeVinney 2016-10-05 22:31:42 -05:00 committed by Mohamed Hegazy
parent a2f3639e48
commit b46144de01
3 changed files with 116 additions and 0 deletions

48
tldjs/index.d.ts vendored Normal file
View File

@ -0,0 +1,48 @@
// Type definitions for tldjs v1.7
// Project: https://github.com/oncletom/tld.js
// Definitions by: Joshua DeVinney <https://github.com/geoffreak>
// 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;

49
tldjs/tldjs-tests.ts Normal file
View File

@ -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'

19
tldjs/tsconfig.json Normal file
View File

@ -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"
]
}