normalizer

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2025 License: MIT Imports: 5 Imported by: 5

README

domain-normalizer

Normalize and parse domain names from messy input. Cleans whitespace and stray symbols, preserves label boundaries, converts Unicode to ASCII (IDNA/punycode), and extracts eTLD+1 (e.g. co.uk), SLD, and subdomain.

Install

go get github.com/pixel365/domain-normalizer

Examples

Normalize a free-form string

package main

import (
	"fmt"

	"github.com/pixel365/domain-normalizer"
)

func main() {
	fmt.Println(normalizer.Normalize("  SomE...DOMa in..com!!!  "))
	// Output: some.doma-in.com
}

Parse (IDNA + public suffix)

package main

import (
	"fmt"

	"github.com/pixel365/domain-normalizer"
)

func main() {
	d, err := normalizer.Parse("a.bücher.de")
	if err != nil { panic(err) }

	fmt.Println(d.Normalized)   // a.bücher.de
	fmt.Println(d.ASCII)        // a.xn--bcher-kva.de
	fmt.Println(d.Tld)          // de
	fmt.Println(d.Registerable) // bücher.de
	fmt.Println(d.Sld)          // bücher
	fmt.Println(d.SubDomain)    // a
}

Multipart public suffix (co.uk)

package main

import (
	"fmt"

	"github.com/pixel365/domain-normalizer"
)

func main() {
	d, _ := normalizer.Parse("x.example.co.uk")
	fmt.Println(d.Tld)          // co.uk
	fmt.Println(d.Registerable) // example.co.uk
	fmt.Println(d.SubDomain)    // x
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyInput   = errors.New("empty input")
	ErrTooLong      = errors.New("domain too long")
	ErrInvalidLabel = errors.New("invalid label")
	ErrIDNA         = errors.New("idna conversion failed")
)

Functions

func Normalize

func Normalize(s string) string

Types

type Domain

type Domain struct {
	Raw          string
	Normalized   string
	ASCII        string
	SubDomain    string
	Sld          string
	Tld          string
	Registerable string
	Labels       []string
	ASCIILabels  []string
	IsIDN        bool
	HasPunycode  bool
	Icann        bool
}

func Parse

func Parse(s string) (Domain, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL