CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tldts

Library to work against complex domain names, subdomains and URIs

82

1.03x
Overview
Eval results
Files

task.mdevals/scenario-6/

URL Edge Case Normalizer

Build a URL normalizer that handles edge cases in hostnames and domains, providing clean, normalized output even when inputs are malformed or unusual.

Requirements

Your solution should implement a function that processes URLs or hostnames and normalizes them, handling various edge cases including:

  1. Malformed hostname handling: Process hostnames with leading dots (.example.com), trailing dots (example.com.), or multiple consecutive dots (example..com)

  2. Unknown TLD handling: Handle hostnames with TLDs that are not in the public suffix list, treating the last label as the suffix

  3. Single-label hostname handling: Process single-label hostnames (like localhost or intranet) appropriately

  4. Case normalization: Ensure all hostnames are normalized to lowercase

  5. Output format: Return a structured result containing:

    • The normalized hostname
    • The extracted domain (or null if not applicable)
    • The public suffix (or fallback for unknown TLDs)
    • A flag indicating if the hostname was valid or required normalization
    • A flag indicating if the TLD is recognized

The normalizer should be fault-tolerant and provide meaningful output even for malformed input, rather than failing.

Test Cases

  • 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

Implementation

@generates

API

/**
 * 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 };

Dependencies { .dependencies }

tldts { .dependency }

Provides URL and hostname parsing with public suffix list support.

Install with Tessl CLI

npx tessl i tessl/npm-tldts

tile.json