NormalizeTitle helper

This commit is contained in:
2023-12-30 10:07:21 -06:00
parent 756facf1b9
commit b0479070f5

View File

@@ -5,6 +5,7 @@ import (
"io"
"net/http"
"regexp"
"strings"
"time"
"github.com/rs/zerolog/log"
@@ -140,3 +141,32 @@ func ApplyUtsaHeaders(req *http.Request) {
req.Header.Set("Accept-Language", "en-US,en;q=0.5")
req.Header.Set("Accept-Encoding", "gzip, deflate")
}
var nonAlphaNumeric = regexp.MustCompile(`[^a-zA-Z0-9]+`)
var continuousWhitespace = regexp.MustCompile(`\s+`)
// NormalizeTitle creates a normalized title from a string, providing a consistent format regardless of whitespace or non-alphanumeric characters
//
// Non-alphanumeric characters are removed
// Capital letters are converted to lowercase
// Whitespace at the beginning or end of the string is removed
// Whitespace of any continuous length is replaced with a single dash
//
// Examples:
//
// "Mailing Address" => "mailing-address"
// "Mailing | Address" => "mailing-address"
// " Mailing Address " => "mailing-address"
// " Mailing | Address " => "mailing-address"
func NormalizeTitle(title string) string {
return continuousWhitespace.ReplaceAllString(
strings.TrimSpace(
strings.ToLower(
nonAlphaNumeric.ReplaceAllString(
title, " ",
),
),
),
"-",
)
}