From 91fccb7a82c068ed58d2447bb9f762ed841d8053 Mon Sep 17 00:00:00 2001 From: Ruben Maher Date: Wed, 10 Oct 2018 04:27:02 +0000 Subject: [PATCH] tldjs: 1.7.0 -> 2.3.2 (#29461) --- types/tldjs/index.d.ts | 68 +++++++++++++++++++------------------- types/tldjs/tldjs-tests.ts | 49 +++++++++++++++++++++------ 2 files changed, 73 insertions(+), 44 deletions(-) diff --git a/types/tldjs/index.d.ts b/types/tldjs/index.d.ts index 6917c8f067..3f63ca0994 100644 --- a/types/tldjs/index.d.ts +++ b/types/tldjs/index.d.ts @@ -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 // 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, + isValid: ReturnType, + isIp: boolean, + tldExists: ReturnType, + publicSuffix: ReturnType, + domain: ReturnType, + subdomain: ReturnType, +}; + +export declare function fromUserSettings(options: { + rules?: any, + validHosts?: string[], + extractHostname?: ReturnType, +}): { + 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, +}; diff --git a/types/tldjs/tldjs-tests.ts b/types/tldjs/tldjs-tests.ts index d405563853..e6eea6e0d6 100644 --- a/types/tldjs/tldjs-tests.ts +++ b/types/tldjs/tldjs-tests.ts @@ -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'