improve function addWordBoundariesToNumbers for package gstr

This commit is contained in:
John
2020-10-10 13:38:28 +08:00
parent 0509e41198
commit 651aa895f8
2 changed files with 17 additions and 5 deletions

View File

@ -24,8 +24,7 @@ import (
)
var (
numberSequence = regexp.MustCompile(`([a-zA-Z])(\d+)([a-zA-Z]?)`)
numberReplacement = []byte(`$1 $2 $3`)
numberSequence = regexp.MustCompile(`([a-zA-Z]{0,1})(\d+)([a-zA-Z]{0,1})`)
firstCamelCaseStart = regexp.MustCompile(`([A-Z]+)([A-Z]?[_a-z\d]+)|$`)
firstCamelCaseEnd = regexp.MustCompile(`([\w\W]*?)([_]?[A-Z]+)$`)
)
@ -139,9 +138,21 @@ func DelimitedScreamingCase(s string, del uint8, screaming bool) string {
}
func addWordBoundariesToNumbers(s string) string {
b := []byte(s)
b = numberSequence.ReplaceAll(b, numberReplacement)
return string(b)
r := numberSequence.ReplaceAllFunc([]byte(s), func(bytes []byte) []byte {
var result []byte
match := numberSequence.FindSubmatch(bytes)
if len(match[1]) > 0 {
result = append(result, match[1]...)
result = append(result, []byte(" ")...)
}
result = append(result, match[2]...)
if len(match[3]) > 0 {
result = append(result, []byte(" ")...)
result = append(result, match[3]...)
}
return result
})
return string(r)
}
// Converts a string to CamelCase

View File

@ -130,6 +130,7 @@ func Test_SnakeScreamingCase(t *testing.T) {
func Test_KebabCase(t *testing.T) {
cases := [][]string{
{"testCase", "test-case"},
{"optimization1.0.0", "optimization-1-0-0"},
}
for _, i := range cases {
in := i[0]