Library to work against complex domain names, subdomains and URIs
82
Build a URL normalizer that handles edge cases in hostnames and domains, providing clean, normalized output even when inputs are malformed or unusual.
Your solution should implement a function that processes URLs or hostnames and normalizes them, handling various edge cases including:
Malformed hostname handling: Process hostnames with leading dots (.example.com), trailing dots (example.com.), or multiple consecutive dots (example..com)
Unknown TLD handling: Handle hostnames with TLDs that are not in the public suffix list, treating the last label as the suffix
Single-label hostname handling: Process single-label hostnames (like localhost or intranet) appropriately
Case normalization: Ensure all hostnames are normalized to lowercase
Output format: Return a structured result containing:
The normalizer should be fault-tolerant and provide meaningful output even for malformed input, rather than failing.
Processing .example.com (leading dot) returns normalized hostname example.com with domain example.com and indicates normalization occurred @test
Processing example.com. (trailing dot) returns normalized hostname example.com with domain example.com and indicates normalization occurred @test
Processing localhost (single-label) returns hostname localhost, null domain, and appropriate flags @test
Processing example.xyzunknown (unknown TLD) returns hostname, extracts example.xyzunknown as domain, uses xyzunknown as suffix, and flags TLD as unrecognized @test
@generates
/**
* Normalizes URLs or hostnames, handling edge cases gracefully
*
* @param {string} input - URL or hostname to normalize
* @returns {Object} Normalized result
* @returns {string|null} result.hostname - Normalized hostname
* @returns {string|null} result.domain - Extracted domain
* @returns {string|null} result.publicSuffix - Public suffix/TLD
* @returns {boolean} result.wasNormalized - True if input required normalization
* @returns {boolean} result.hasKnownTld - True if TLD is in public suffix list
*/
function normalizeUrl(input) {
// Implementation here
}
module.exports = { normalizeUrl };Provides URL and hostname parsing with public suffix list support.
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