mirror of
https://github.com/Xevion/scla-unsubscribe.git
synced 2025-12-06 15:16:23 -06:00
NormalizeTitle helper
This commit is contained in:
30
helpers.go
30
helpers.go
@@ -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, " ",
|
||||
),
|
||||
),
|
||||
),
|
||||
"-",
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user