Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add string conversion functions #466

Merged
merged 7 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,72 @@ sub := len("hell么")

[[play](https://go.dev/play/p/tuhgW_lWY8l)]

### PascalCase

Converts string to pascal case.

```go
str := lo.PascalCase("hello_world")
// HelloWorld
```

[[play](https://go.dev/play/p/iZkdeLP9oiB)]

### CamelCase

Converts string to camel case.

```go
str := lo.CamelCase("hello_world")
// helloWorld
```

[[play](https://go.dev/play/p/dtyFB58MBRp)]

### KebabCase

Converts string to kebab case.

```go
str := lo.KebabCase("helloWorld")
// hello-world
```

[[play](https://go.dev/play/p/2YTuPafwECA)]

### SnakeCase

Converts string to snake case.

```go
str := lo.SnakeCase("HelloWorld")
// hello_world
```

[[play](https://go.dev/play/p/QVKJG9nOnDg)]

### Words

Splits string into an array of its words.

```go
str := lo.Words("helloWorld")
// []string{"hello", "world"}
```

[[play](https://go.dev/play/p/2P4zhqqq61g)]

### Capitalize

Converts the first character of string to upper case and the remaining to lower case.

```go
str := lo.PascalCase("heLLO")
// Hello
```

[[play](https://go.dev/play/p/jBIJ_OFtFYp)]

### T2 -> T9

Creates a tuple from a list of values.
Expand Down
69 changes: 69 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package lo

import (
"math/rand"
"regexp"
"strings"
"unicode"
"unicode/utf8"
)

Expand Down Expand Up @@ -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])`)
Copy link
Contributor

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

Copy link
Contributor Author

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.

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your suggested changes.

}
}
return string(runes)
}
147 changes: 147 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

https://github.com/client9/misspell

https://github.com/ettle/strcase

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
})
}
}