mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [strutil] handle non-ASCII letters in case conversion (#413)
* fix(package): [strutil] handle non-ASCII letters in case conversion * test(package): [strutil] cover non-ASCII letters in case conversion
This commit is contained in:
1 parent
4c9c628a31
commit
ed237c18b3
2 files changed
+22
-16
No files matched your search
+10
-16
@@ -60,25 +60,22 @@ func isDigit(r rune) bool {
|
||||
return r >= '0' && r <= '9'
|
||||
}
|
||||
|
||||
// isLower checks if a character is lower case ('a' to 'z')
|
||||
// isLower checks if a character is a lower case letter
|
||||
func isLower(r rune) bool {
|
||||
return r >= 'a' && r <= 'z'
|
||||
return unicode.IsLower(r)
|
||||
}
|
||||
|
||||
// isUpper checks if a character is upper case ('A' to 'Z')
|
||||
// isUpper checks if a character is an upper case letter
|
||||
func isUpper(r rune) bool {
|
||||
return r >= 'A' && r <= 'Z'
|
||||
return unicode.IsUpper(r)
|
||||
}
|
||||
|
||||
// toLower converts a character 'A' to 'Z' to its lower case
|
||||
// toLower converts a character to its lower case
|
||||
func toLower(r rune) rune {
|
||||
if r >= 'A' && r <= 'Z' {
|
||||
return r + 32
|
||||
}
|
||||
return r
|
||||
return unicode.ToLower(r)
|
||||
}
|
||||
|
||||
// toLowerAll converts a character 'A' to 'Z' to its lower case
|
||||
// toLowerAll converts characters to their lower case
|
||||
func toLowerAll(rs []rune) []rune {
|
||||
for i := range rs {
|
||||
rs[i] = toLower(rs[i])
|
||||
@@ -86,15 +83,12 @@ func toLowerAll(rs []rune) []rune {
|
||||
return rs
|
||||
}
|
||||
|
||||
// toUpper converts a character 'a' to 'z' to its upper case
|
||||
// toUpper converts a character to its upper case
|
||||
func toUpper(r rune) rune {
|
||||
if r >= 'a' && r <= 'z' {
|
||||
return r - 32
|
||||
}
|
||||
return r
|
||||
return unicode.ToUpper(r)
|
||||
}
|
||||
|
||||
// toUpperAll converts a character 'a' to 'z' to its upper case
|
||||
// toUpperAll converts characters to their upper case
|
||||
func toUpperAll(rs []rune) []rune {
|
||||
for i := range rs {
|
||||
rs[i] = toUpper(rs[i])
|
||||
|
||||
@@ -23,6 +23,8 @@ func TestCamelCase(t *testing.T) {
|
||||
{"$foo%", "foo"},
|
||||
{" $#$Foo 22 bar ", "foo22Bar"},
|
||||
{"Foo-#1😄$_%^&*(1bar", "foo11Bar"},
|
||||
{"Привет Мир", "приветМир"},
|
||||
{"Köln Straße", "kölnStraße"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -47,6 +49,8 @@ func TestUpperCamelCase(t *testing.T) {
|
||||
{"$foo%", "Foo"},
|
||||
{" $#$Foo 22 bar ", "Foo22Bar"},
|
||||
{"Foo-#1😄$_%^&*(1bar", "Foo11Bar"},
|
||||
{"Привет Мир", "ПриветМир"},
|
||||
{"Köln Straße", "KölnStraße"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -99,6 +103,8 @@ func TestKebabCase(t *testing.T) {
|
||||
{"$foo@Bar", "foo-bar"},
|
||||
{" $#$Foo 22 bar ", "foo-22-bar"},
|
||||
{"Foo-#1😄$_%^&*(1bar", "foo-1-1-bar"},
|
||||
{"Привет Мир", "привет-мир"},
|
||||
{"Köln Straße", "köln-straße"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -127,6 +133,8 @@ func TestUpperKebabCase(t *testing.T) {
|
||||
{"$foo@Bar", "FOO-BAR"},
|
||||
{" $#$Foo 22 bar ", "FOO-22-BAR"},
|
||||
{"Foo-#1😄$_%^&*(1bar", "FOO-1-1-BAR"},
|
||||
{"Привет Мир", "ПРИВЕТ-МИР"},
|
||||
{"Köln Straße", "KÖLN-STRAßE"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -155,6 +163,8 @@ func TestSnakeCase(t *testing.T) {
|
||||
{"$foo@Bar", "foo_bar"},
|
||||
{" $#$Foo 22 bar ", "foo_22_bar"},
|
||||
{"Foo-#1😄$_%^&*(1bar", "foo_1_1_bar"},
|
||||
{"Привет Мир", "привет_мир"},
|
||||
{"Köln Straße", "köln_straße"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -183,6 +193,8 @@ func TestUpperSnakeCase(t *testing.T) {
|
||||
{"$foo@Bar", "FOO_BAR"},
|
||||
{" $#$Foo 22 bar ", "FOO_22_BAR"},
|
||||
{"Foo-#1😄$_%^&*(1bar", "FOO_1_1_BAR"},
|
||||
{"Привет Мир", "ПРИВЕТ_МИР"},
|
||||
{"Köln Straße", "KÖLN_STRAßE"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Reference in new issue
Block a user