-
-
Notifications
You must be signed in to change notification settings - Fork 823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add string conversion functions #466
Changes from 4 commits
68495d1
a786b13
9656a1b
dbb1fe2
754471a
6d178cd
5f102d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ package lo | |
|
||
import ( | ||
"math/rand" | ||
"regexp" | ||
"strings" | ||
"unicode" | ||
"unicode/utf8" | ||
) | ||
|
||
|
@@ -94,3 +96,70 @@ func ChunkString[T ~string](str T, size int) []T { | |
func RuneLength(str string) int { | ||
return utf8.RuneCountInString(str) | ||
} | ||
|
||
// PascalCase converts string to pascal case. | ||
func PascalCase(str string) string { | ||
items := Words(str) | ||
for i, item := range items { | ||
items[i] = Capitalize(strings.ToLower(item)) | ||
} | ||
return strings.Join(items, "") | ||
} | ||
|
||
// CamelCase converts string to camel case. | ||
func CamelCase(str string) string { | ||
items := Words(str) | ||
for i, item := range items { | ||
item = strings.ToLower(item) | ||
if i > 0 { | ||
item = Capitalize(item) | ||
} | ||
items[i] = item | ||
} | ||
return strings.Join(items, "") | ||
} | ||
|
||
// KebabCase converts string to kebab case. | ||
func KebabCase(str string) string { | ||
return strings.Join(Map(Words(str), func(item string, index int) string { | ||
return strings.ToLower(item) | ||
}), "-") | ||
} | ||
|
||
// SnakeCase converts string to snake case. | ||
func SnakeCase(str string) string { | ||
return strings.Join(Map(Words(str), func(item string, index int) string { | ||
return strings.ToLower(item) | ||
}), "_") | ||
} | ||
|
||
// Words splits string into an array of its words. | ||
func Words(str string) []string { | ||
reg := regexp.MustCompile(`([a-z])([A-Z])|([a-zA-Z])([0-9])|([0-9])([a-zA-Z])`) | ||
str = reg.ReplaceAllString(str, `$1$3$5 $2$4$6`) | ||
var result strings.Builder | ||
for _, r := range str { | ||
if unicode.IsLetter(r) || unicode.IsDigit(r) { | ||
result.WriteRune(r) | ||
} else { | ||
result.WriteRune(' ') | ||
} | ||
} | ||
return strings.Fields(result.String()) | ||
} | ||
|
||
// Capitalize converts the first character of string to upper case and the remaining to lower case. | ||
func Capitalize(str string) string { | ||
if len(str) == 0 { | ||
return str | ||
} | ||
runes := []rune(str) | ||
for i, r := range runes { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say, keep it simple for i, r := range runes {
r = unicode.ToLower(r)
if i == 0 {
r = unicode.ToUpper(r)
}
runes[i] = r
} |
||
if i == 0 { | ||
runes[i] = unicode.ToUpper(r) | ||
} else { | ||
runes[i] = unicode.ToLower(r) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code still looks strange to me There was a function now deprecated that did this https://pkg.go.dev/strings#Title It had been replaced by https://pkg.go.dev/golang.org/x/text/cases#Title There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your suggested changes. |
||
} | ||
} | ||
return string(runes) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,3 +100,150 @@ func TestRuneLength(t *testing.T) { | |
is.Equal(5, RuneLength("hellô")) | ||
is.Equal(6, len("hellô")) | ||
} | ||
|
||
func TestPascalCase(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{"", "hello_world", "HelloWorld"}, | ||
{"", "helloWorld", "HelloWorld"}, | ||
{"", "__hello_world-example string--", "HelloWorldExampleString"}, | ||
{"", "WITH UPPERCASE LETTERS", "WithUppercaseLetters"}, | ||
{"", "test123_string", "Test123String"}, | ||
{"", "test123string", "Test123String"}, | ||
{"", "", ""}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
actual := PascalCase(test.input) | ||
if actual != test.expected { | ||
t.Errorf("PascalCase(%q) = %q; expected %q", test.input, actual, test.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCamelCase(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{"", "hello_world", "helloWorld"}, | ||
{"", "helloWorld", "helloWorld"}, | ||
{"", "__hello_world-example string--", "helloWorldExampleString"}, | ||
{"", "WITH UPPERCASE LETTERS", "withUppercaseLetters"}, | ||
{"", "test123_string", "test123String"}, | ||
{"", "test123string", "test123String"}, | ||
{"", "", ""}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
result := CamelCase(test.input) | ||
if result != test.expected { | ||
t.Errorf("CamelCase(%q) = %q; want %q", test.input, result, test.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestKebabCase(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{"", "hello world", "hello-world"}, | ||
{"", "HelloWorld", "hello-world"}, | ||
{"", "KebabCase", "kebab-case"}, | ||
{"", "already-kebab-case", "already-kebab-case"}, | ||
{"", "Already-Kebab-Case", "already-kebab-case"}, | ||
{"", "multiple spaces", "multiple-spaces"}, | ||
{"", "", ""}, | ||
{"", "Single", "single"}, | ||
{"", "123_abs", "123-abs"}, | ||
{"", "SINGLE", "single"}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
result := KebabCase(test.input) | ||
if result != test.expected { | ||
t.Errorf("KebabCase(%q) = %q; want %q", test.input, result, test.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestSnakeCase(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{"", "CamelCase", "camel_case"}, | ||
{"", "snakeCase", "snake_case"}, | ||
{"", "snake-case", "snake_case"}, | ||
{"", "SnakeCaseTest", "snake_case_test"}, | ||
{"", "Snake_Case_With_Underscores", "snake_case_with_underscores"}, | ||
{"", "lowercase", "lowercase"}, | ||
{"", "UPPERCASE", "uppercase"}, | ||
{"", "", ""}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.input, func(t *testing.T) { | ||
got := SnakeCase(test.input) | ||
if got != test.expected { | ||
t.Errorf("SnakeCase(%q) = %q; want %q", test.input, got, test.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestWords(t *testing.T) { | ||
type args struct { | ||
str string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
}{ | ||
{"", args{"PascalCase"}, []string{"Pascal", "Case"}}, | ||
{"", args{"camelCase"}, []string{"camel", "Case"}}, | ||
{"", args{"snake_case"}, []string{"snake", "case"}}, | ||
{"", args{"kebab_case"}, []string{"kebab", "case"}}, | ||
{"", args{"_test text_"}, []string{"test", "text"}}, | ||
{"", args{"test123string"}, []string{"test", "123", "string"}}, | ||
{"", args{"UPPERCASE"}, []string{"UPPERCASE"}}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test should provide real life examples, and include things about Go initialisms Maybe you could have to look at these repositories: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated the test cases |
||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, Words(tt.args.str), "words(%v)", tt.args.str) | ||
}) | ||
} | ||
} | ||
|
||
func TestCapitalize(t *testing.T) { | ||
type args struct { | ||
word string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{"", args{"hello"}, "Hello"}, | ||
{"", args{"heLLO"}, "Hello"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equalf(t, tt.want, Capitalize(tt.args.word), "Capitalize(%v)", tt.args.word) | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regexp should/could be moved at package level. Computing the regexp each time you use the method seems a bad thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your point. I have optimized the regex as suggested. Please review the latest commit.