mirror of
https://github.com/Xevion/scla-unsubscribe.git
synced 2025-12-08 08:08:30 -06:00
NormalizeTitle helper
This commit is contained in:
30
helpers.go
30
helpers.go
@@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"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-Language", "en-US,en;q=0.5")
|
||||||
req.Header.Set("Accept-Encoding", "gzip, deflate")
|
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