tldjs: 1.7.0 -> 2.3.2 (#29461)

This commit is contained in:
Ruben Maher
2018-10-09 21:27:02 -07:00
committed by Andy
parent c3f722d914
commit 91fccb7a82
2 changed files with 73 additions and 44 deletions
+34 -34
View File
@@ -1,48 +1,48 @@
// Type definitions for tldjs v1.7
// Type definitions for tldjs v2.3.1
// Project: https://github.com/oncletom/tld.js
// Definitions by: Joshua DeVinney <https://github.com/geoffreak>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
/**
* 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}
* @deprecated "isValid" is deprecated, please use "isValidHostname" instead.
*/
export declare function isValid(host: string): string | null;
export declare function isValid(host: string): boolean;
export declare function isValidHostname(host: string): boolean;
export declare function extractHostname(host: string): string | null;
export declare function parse(host: string): {
hostname: ReturnType<typeof extractHostname>,
isValid: ReturnType<typeof isValid>,
isIp: boolean,
tldExists: ReturnType<typeof tldExists>,
publicSuffix: ReturnType<typeof getPublicSuffix>,
domain: ReturnType<typeof getDomain>,
subdomain: ReturnType<typeof getSubdomain>,
};
export declare function fromUserSettings(options: {
rules?: any,
validHosts?: string[],
extractHostname?: ReturnType<typeof extractHostname>,
}): {
extractHostname: typeof extractHostname,
isValidHostname: typeof isValidHostname,
isValid: typeof isValid,
parse: typeof parse,
tldExists: typeof tldExists,
getPublicSuffix: typeof getPublicSuffix,
getDomain: typeof getDomain,
getSubdomain: typeof getSubdomain,
fromUserSettings: typeof fromUserSettings,
};
+39 -10
View File
@@ -31,19 +31,48 @@ 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`
tld.isValidHostname('google.com'); // returns `true`
tld.isValidHostname('.google.com'); // returns `false`
tld.isValidHostname('my.fake.domain'); // returns `true`
tld.isValidHostname('localhost'); // returns `false`
tld.isValidHostname('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');
tld.extractHostname('https://www.npmjs.com/package/tldjs') // returns 'www.npmjs.com'
tld2.getDomain('localhost'); // returns null
tld2.getSubdomain('vhost.localhost'); // returns null
tld.parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv');
// { hostname: 'spark-public.s3.amazonaws.com',
// isValid: true,
// isIp: false,
// tldExists: true,
// publicSuffix: 's3.amazonaws.com',
// domain: 'spark-public.s3.amazonaws.com',
// subdomain: ''
// }
tld2.validHosts = ['localhost'];
tld.parse('gopher://domain.unknown/');
// { hostname: 'domain.unknown',
// isValid: true,
// isIp: false,
// tldExists: false,
// publicSuffix: 'unknown',
// domain: 'domain.unknown',
// subdomain: ''
// }
tld.parse('https://192.168.0.0')
// { hostname: '192.168.0.0',
// isValid: true,
// isIp: true,
// tldExists: false,
// publicSuffix: null,
// domain: null,
// subdomain: null
// }
tld.getDomain('localhost'); // returns null
tld.getSubdomain('vhost.localhost'); // returns null
const tld2 = tld.fromUserSettings({ validHosts: ['localhost'] });
tld2.getDomain('localhost'); // returns 'localhost'
tld2.getSubdomain('vhost.localhost'); // returns 'vhost'