### Install tld.js Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Installs the tld.js module using npm. An optional flag can be used to update the list of well-known TLDs during installation. ```bash npm install --save tldjs # To update rules during install: npm install --save tldjs --tldjs-update-rules ``` -------------------------------- ### Updating tld.js TLD List Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Explains how to update the bundled list of Top-Level Domains (TLDs) for tld.js by using an npm install flag. This ensures the library uses the most current TLD information. ```bash # anytime you reinstall your project npm install --tldjs-update-rules # or if you add the dependency to your project npm install --save tldjs --tldjs-update-rules ``` -------------------------------- ### Get Subdomain Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Extracts the complete subdomain from a given string. It handles various formats including URLs with credentials and ports. Returns an empty string if no subdomain is found. ```javascript const { getSubdomain } = tldjs; getSubdomain('google.com'); // returns `` getSubdomain('fr.google.com'); // returns `fr` getSubdomain('google.co.uk'); // returns `` getSubdomain('foo.google.co.uk'); // returns `foo` getSubdomain('moar.foo.google.co.uk'); // returns `moar.foo` getSubdomain('t.co'); // returns `` getSubdomain('fr.t.co'); // returns `fr` getSubdomain('https://user:password@secure.example.co.uk:443/some/path?and&query#hash'); // returns `secure` ``` -------------------------------- ### Get Fully Qualified Domain Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Extracts the fully qualified domain name from a given string, which can be a URL or a hostname. It correctly handles subdomains and complex TLDs. ```javascript const { getDomain } = require('tldjs'); console.log(getDomain('google.com')); // returns `google.com` console.log(getDomain('fr.google.com')); // returns `google.com` console.log(getDomain('fr.google.google')); // returns `google.google` console.log(getDomain('foo.google.co.uk')); // returns `google.co.uk` console.log(getDomain('t.co')); // returns `t.co` console.log(getDomain('fr.t.co')); // returns `t.co` console.log(getDomain('https://user:password@example.co.uk:8080/some/path?and&query#hash')); // returns `example.co.uk` ``` -------------------------------- ### Get Public Suffix Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Retrieves the public suffix for a given string. This is useful for identifying the registrable domain. It can handle multi-level public suffixes like 'co.uk'. ```javascript const { getPublicSuffix } = tldjs; getPublicSuffix('google.com'); // returns `com` getPublicSuffix('fr.google.com'); // returns `com` getPublicSuffix('google.co.uk'); // returns `co.uk` getPublicSuffix('s3.amazonaws.com'); // returns `s3.amazonaws.com` getPublicSuffix('tld.is.unknown'); // returns `unknown` ``` -------------------------------- ### Benchmarking tld.js Performance Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Provides instructions on how to measure the performance of tld.js on your own hardware using the provided benchmark script. It also suggests providing a custom extractHostname function for performance optimization. ```bash npm run benchmark ``` -------------------------------- ### Customizing tld.js with Valid Hosts Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Demonstrates how to instantiate tld.js with custom valid hosts, such as 'localhost', to enable parsing of local or custom domain names. ```javascript const tldjs = require('tldjs'); tldjs.getDomain('localhost'); // returns null tldjs.getSubdomain('vhost.localhost'); // returns null const myTldjs = tldjs.fromUserSettings({ validHosts: ['localhost'] }); myTldjs.getDomain('localhost'); // returns 'localhost' myTldjs.getSubdomain('vhost.localhost'); // returns 'vhost' ``` -------------------------------- ### getDomain() Method Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Retrieves the main domain name from a given URL or hostname, excluding subdomains and including the public suffix. ```APIDOC getDomain(input: String) Returns the fully qualified domain from a given string. Parameters: input (String): The URL or hostname to process. Returns: String: The fully qualified domain name. Example: getDomain('sub.example.co.uk') // 'example.co.uk' ``` -------------------------------- ### Check TLD Existence Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Checks if the Top-Level Domain (TLD) for a given hostname is recognized or well-known. It can process full URLs or just hostnames. ```javascript const { tldExists } = require('tldjs'); console.log(tldExists('google.com')); // returns `true` console.log(tldExists('google.local')); // returns `false` console.log(tldExists('com')); // returns `true` console.log(tldExists('uk')); // returns `true` console.log(tldExists('co.uk')); // returns `true` console.log(tldExists('amazon.fancy.uk')); // returns `true` console.log(tldExists('amazon.co.uk')); // returns `true` console.log(tldExists('https://user:password@example.co.uk:8080/some/path?and&query#hash')); // returns `true` ``` -------------------------------- ### tldjs.parse() Properties Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Details the properties returned by the `tldjs.parse()` method, which provides structured information about a URL or hostname. ```APIDOC tldjs.parse(input: String) Parses a URL or hostname to extract domain-related information. Returns: Object: An object containing the following properties: - hostname (String): The extracted hostname. - isValid (Boolean): Indicates if the hostname is valid according to RFC standards. - isIp (Boolean): Indicates if the hostname is an IP address. - tldExists (Boolean): Indicates if the TLD is well-known. - publicSuffix (String): The public suffix of the hostname. - domain (String): The domain name (e.g., 'example.com'). - subdomain (String): The subdomain part (e.g., 'www' in 'www.example.com'). Example: tldjs.parse('https://www.bbc.co.uk/news') // Returns: { hostname: 'www.bbc.co.uk', isValid: true, isIp: false, tldExists: true, publicSuffix: 'bbc.co.uk', domain: 'bbc.co.uk', subdomain: 'www' } ``` -------------------------------- ### tldExists() Method Source: https://github.com/thom4parisot/tld.js/blob/master/README.md A utility method to determine if a Top-Level Domain (TLD) is recognized within the Public Suffix List. ```APIDOC tldExists(input: String) Checks if the TLD of a given hostname is well-known. Parameters: input (String): The URL or hostname to check. Returns: Boolean: `true` if the TLD exists, `false` otherwise. Example: tldExists('example.com') // true tldExists('example.local') // false ``` -------------------------------- ### Parse URL or Hostname with tldjs Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Parses a given URL or hostname to extract properties like hostname, validity, TLD existence, public suffix, domain, and subdomain. It handles various URL formats and IP addresses. ```javascript const { parse } = require('tldjs'); // Example 1: Standard URL console.log(parse('https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv')); // Expected output: { hostname: 'spark-public.s3.amazonaws.com', isValid: true, isIp: false, tldExists: true, publicSuffix: 's3.amazonaws.com', domain: 'spark-public.s3.amazonaws.com', subdomain: '' } // Example 2: URL with unknown TLD console.log(parse('gopher://domain.unknown/')); // Expected output: { hostname: 'domain.unknown', isValid: true, isIp: false, tldExists: false, publicSuffix: 'unknown', domain: 'domain.unknown', subdomain: '' } // Example 3: IP Address console.log(parse('https://192.168.0.0')) // Expected output: { hostname: '192.168.0.0', isValid: true, isIp: true, tldExists: false, publicSuffix: null, domain: null, subdomain: null } ``` -------------------------------- ### Validate Hostname Source: https://github.com/thom4parisot/tld.js/blob/master/README.md Checks if a given string is a valid hostname according to RFC 1035. It does not verify if the TLD is well-known. IP addresses are considered valid hostnames. ```javascript const { isValidHostname } = tldjs; isValidHostname('google.com'); // returns `true` isValidHostname('.google.com'); // returns `false` isValidHostname('my.fake.domain'); // returns `true` isValidHostname('localhost'); // returns `false` isValidHostname('https://user:password@example.co.uk:8080/some/path?and&query#hash'); // returns `false` isValidHostname('192.168.0.0') // returns `true` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.