Skip to content

Go library to sanitize, normalize, and parse domain names (Unicode → ASCII IDNA/punycode, public suffix / eTLD+1)

License

Notifications You must be signed in to change notification settings

pixel365/domain-normalizer

Repository files navigation

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
}

About

Go library to sanitize, normalize, and parse domain names (Unicode → ASCII IDNA/punycode, public suffix / eTLD+1)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published