Library to work against complex domain names, subdomains and URIs
82
Build a URL normalization utility that extracts and normalizes hostnames from various URL formats. The utility should handle diverse input types and provide clean hostname outputs for comparison and grouping operations.
Implement a module that provides the following functionality:
Create a function normalizeHostname(url: string): string | null that:
Create a function normalizeHostnames(urls: string[]): Array<string | null> that:
Create a function groupByHostname(urls: string[]): Record<string, string[]> that:
Test file: test/normalizer.test.ts
"https://www.example.com/path", normalizeHostname returns "www.example.com" @test"http://api.github.com:8080/v1/users?id=123", normalizeHostname returns "api.github.com" @test"https://user:pass@secure.example.org/admin", normalizeHostname returns "secure.example.org" @test"example.com", normalizeHostname returns "example.com" @test"https://192.168.1.1/path", normalizeHostname returns null @test"http://[2001:db8::1]:8080/", normalizeHostname returns null @test["https://example.com/a", "http://test.org/b", "https://192.168.1.1/c"], normalizeHostnames returns ["example.com", "test.org", null] @test["https://example.com/page1", "https://example.com/page2", "http://test.org/home"], groupByHostname returns object with keys "example.com" and "test.org" containing their respective original URLs @test@generates
/**
* Normalizes a URL by extracting its hostname
* @param url - The URL string to normalize
* @returns The normalized hostname or null if invalid/IP address
*/
export function normalizeHostname(url: string): string | null;
/**
* Normalizes an array of URLs
* @param urls - Array of URL strings
* @returns Array of normalized hostnames in the same order
*/
export function normalizeHostnames(urls: string[]): Array<string | null>;
/**
* Groups URLs by their normalized hostname
* @param urls - Array of URL strings
* @returns Object mapping hostnames to arrays of original URLs
*/
export function groupByHostname(urls: string[]): Record<string, string[]>;Provides URL parsing and hostname extraction functionality.
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