Library to work against complex domain names, subdomains and URIs
82
Build a utility that analyzes domain names to extract their public suffix and registrable domain, using domain parsing rules.
Domain names are structured hierarchically with public suffixes (like com, co.uk) that indicate where domains can be registered. Different domains have different suffix structures:
example.com has suffix comexample.co.uk has suffix co.ukCreate a utility that:
Takes a domain name (hostname) as input
Identifies the public suffix (the "TLD" part where domains can be registered)
Extracts the registrable domain (the suffix plus one additional label to the left)
Handles multi-level public suffixes correctly (like co.uk, gov.au)
Returns structured information about the domain including:
Your utility should handle these cases correctly:
| Input | Public Suffix | Registrable Domain | Subdomain |
|---|---|---|---|
example.com | com | example.com | (empty) |
www.example.com | com | example.com | www |
api.www.example.com | com | example.com | api.www |
example.co.uk | co.uk | example.co.uk | (empty) |
www.example.co.uk | co.uk | example.co.uk | www |
blog.example.gov.au | gov.au | example.gov.au | blog |
The utility should use a reliable domain parsing library that understands the Public Suffix List (which contains rules for all valid public suffixes including wildcards and exceptions).
@generates
Your implementation should correctly parse domains and extract the relevant components using proper domain parsing that respects the Public Suffix List rules (including wildcards and exceptions).
example.com) extracts correct suffix and registrable domain @testexample.co.uk) extracts correct suffix and registrable domain @test/**
* Result of parsing a domain name
*/
export interface DomainInfo {
hostname: string;
publicSuffix: string | null;
domain: string | null; // The registrable domain
subdomain: string | null;
}
/**
* Parses a domain name and extracts its components
* @param hostname - The domain name to parse
* @returns Domain information including suffix, registrable domain, and subdomain
*/
export function parseDomain(hostname: string): DomainInfo;Provides domain parsing and Public Suffix List utilities.
Install with Tessl CLI
npx tessl i tessl/npm-tldtsdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10