Files
crawlab/core/utils/string.go
Marvin Zhang c937e0f45f refactor: enhance Spider model and string utility functions
- Updated the Spider model to introduce a new SpiderTemplateParams struct for improved template handling.
- Refactored string utility functions in utils/string.go to include a new replaceChars function, streamlining character replacement across multiple functions.
- Enhanced ToSnakeCase and ToKebabCase functions to utilize the new replaceChars function for better maintainability and readability.
- Added splitStringWithQuotes function to facilitate string manipulation with quotes, improving overall utility in string processing.
2025-01-07 13:21:16 +08:00

76 lines
1.7 KiB
Go

package utils
import (
"golang.org/x/text/cases"
"golang.org/x/text/language"
"strings"
)
// replaceChars replaces characters in a string
// Parameters:
// - s: the string to replace characters in
// - o: the characters to replace
// - r: the replacement character
//
// Example:
//
// replaceChars("a-b-c", []string{"-"}, "_") => "a_b_c"
//
// Returns:
// - the string with characters replaced
func replaceChars(s string, o []string, r string) string {
for _, c := range o {
s = strings.ReplaceAll(s, c, r)
}
return s
}
func ToSnakeCase(s string) string {
s = strings.TrimSpace(s)
s = strings.ToLower(s)
return replaceChars(s, []string{" ", "-", "."}, "_")
}
func ToPascalCase(s string) string {
s = strings.TrimSpace(s)
s = strings.ReplaceAll(s, "_", " ")
s = cases.Title(language.English).String(s)
s = strings.ReplaceAll(s, " ", "")
return s
}
func ToKebabCase(s string) string {
s = strings.TrimSpace(s)
s = strings.ToLower(s)
return replaceChars(s, []string{" ", "_", "."}, "-")
}
// splitStringWithQuotes splits a string with quotes
// Parameters:
// - s: the string to split
// - q: the quote character
// - d: the delimiter
// - r: the replacement
//
// Example:
//
// splitStringWithQuotes("a,b,c", "'", ",", ", ") => "'a', 'b', 'c'"
//
// Returns:
// - the split string
func splitStringWithQuotes(s, q, d, r string) string {
s = strings.TrimSpace(s)
s = strings.ReplaceAll(s, " ", "")
s = strings.ReplaceAll(s, d, q+r+q)
s = q + s + q
return s
}
func SplitStringWithSingleQuotes(s string) string {
return splitStringWithQuotes(s, "'", ",", ", ")
}
func SplitStringWithDoubleQuotes(s string) string {
return splitStringWithQuotes(s, "\"", ",", "\", \"")
}