feat: add string conversion functions (#466)

* feat: add string conversion functions

* fix: fix `Capitalize`, update tests

* fix: fix `Capitalize`, update tests

* update README.md

* update tests

* update `Capitalize`

* style: unify coding style
This commit is contained in:
eiixy
2024-06-27 18:56:08 +08:00
committed by GitHub
parent a66bf34300
commit 266436bb40
5 changed files with 520 additions and 0 deletions

View File

@@ -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.

1
go.mod
View File

@@ -11,6 +11,7 @@ require (
github.com/thoas/go-funk v0.9.1
go.uber.org/goleak v1.2.1
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17
golang.org/x/text v0.16.0
)
require (

2
go.sum
View File

@@ -22,6 +22,8 @@ go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@@ -1,8 +1,12 @@
package lo
import (
"golang.org/x/text/cases"
"golang.org/x/text/language"
"math/rand"
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
@@ -14,6 +18,9 @@ var (
AlphanumericCharset = append(LettersCharset, NumbersCharset...)
SpecialCharset = []rune("!@#$%^&*()_+-=[]{}|;':\",./<>?")
AllCharset = append(AlphanumericCharset, SpecialCharset...)
splitWordReg = regexp.MustCompile(`([a-z])([A-Z0-9])|([a-zA-Z])([0-9])|([0-9])([a-zA-Z])|([A-Z])([A-Z])([a-z])`)
splitNumberLetterReg = regexp.MustCompile(`([0-9])([a-zA-Z])`)
)
// RandomString return a random string.
@@ -94,3 +101,64 @@ 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(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 {
items := Words(str)
for i, item := range items {
items[i] = strings.ToLower(item)
}
return strings.Join(items, "-")
}
// SnakeCase converts string to snake case.
func SnakeCase(str string) string {
items := Words(str)
for i, item := range items {
items[i] = strings.ToLower(item)
}
return strings.Join(items, "_")
}
// Words splits string into an array of its words.
func Words(str string) []string {
str = splitWordReg.ReplaceAllString(str, `$1$3$5$7 $2$4$6$8$9`)
// example: Int8Value => Int 8Value => Int 8 Value
str = splitNumberLetterReg.ReplaceAllString(str, "$1 $2")
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 {
return cases.Title(language.English).String(str)
}

View File

@@ -100,3 +100,386 @@ func TestRuneLength(t *testing.T) {
is.Equal(5, RuneLength("hellô"))
is.Equal(6, len("hellô"))
}
func TestAllCase(t *testing.T) {
type output struct {
PascalCase string
CamelCase string
KebabCase string
SnakeCase string
}
name := ""
tests := []struct {
name string
input string
output output
}{
{name: name, output: output{}},
{name: name, input: ".", output: output{}},
{name: name, input: "Hello world!", output: output{
PascalCase: "HelloWorld",
CamelCase: "helloWorld",
KebabCase: "hello-world",
SnakeCase: "hello_world",
}},
{name: name, input: "A", output: output{
PascalCase: "A",
CamelCase: "a",
KebabCase: "a",
SnakeCase: "a",
}},
{name: name, input: "a", output: output{
PascalCase: "A",
CamelCase: "a",
KebabCase: "a",
SnakeCase: "a",
}},
{name: name, input: "foo", output: output{
PascalCase: "Foo",
CamelCase: "foo",
KebabCase: "foo",
SnakeCase: "foo",
}},
{name: name, input: "snake_case", output: output{
PascalCase: "SnakeCase",
CamelCase: "snakeCase",
KebabCase: "snake-case",
SnakeCase: "snake_case",
}},
{name: name, input: "SNAKE_CASE", output: output{
PascalCase: "SnakeCase",
CamelCase: "snakeCase",
KebabCase: "snake-case",
SnakeCase: "snake_case",
}},
{name: name, input: "kebab-case", output: output{
PascalCase: "KebabCase",
CamelCase: "kebabCase",
KebabCase: "kebab-case",
SnakeCase: "kebab_case",
}},
{name: name, input: "PascalCase", output: output{
PascalCase: "PascalCase",
CamelCase: "pascalCase",
KebabCase: "pascal-case",
SnakeCase: "pascal_case",
}},
{name: name, input: "camelCase", output: output{
PascalCase: "CamelCase",
CamelCase: "camelCase",
KebabCase: `camel-case`,
SnakeCase: "camel_case",
}},
{name: name, input: "Title Case", output: output{
PascalCase: "TitleCase",
CamelCase: "titleCase",
KebabCase: "title-case",
SnakeCase: "title_case",
}},
{name: name, input: "point.case", output: output{
PascalCase: "PointCase",
CamelCase: "pointCase",
KebabCase: "point-case",
SnakeCase: "point_case",
}},
{name: name, input: "snake_case_with_more_words", output: output{
PascalCase: "SnakeCaseWithMoreWords",
CamelCase: "snakeCaseWithMoreWords",
KebabCase: "snake-case-with-more-words",
SnakeCase: "snake_case_with_more_words",
}},
{name: name, input: "SNAKE_CASE_WITH_MORE_WORDS", output: output{
PascalCase: "SnakeCaseWithMoreWords",
CamelCase: "snakeCaseWithMoreWords",
KebabCase: "snake-case-with-more-words",
SnakeCase: "snake_case_with_more_words",
}},
{name: name, input: "kebab-case-with-more-words", output: output{
PascalCase: "KebabCaseWithMoreWords",
CamelCase: "kebabCaseWithMoreWords",
KebabCase: "kebab-case-with-more-words",
SnakeCase: "kebab_case_with_more_words",
}},
{name: name, input: "PascalCaseWithMoreWords", output: output{
PascalCase: "PascalCaseWithMoreWords",
CamelCase: "pascalCaseWithMoreWords",
KebabCase: "pascal-case-with-more-words",
SnakeCase: "pascal_case_with_more_words",
}},
{name: name, input: "camelCaseWithMoreWords", output: output{
PascalCase: "CamelCaseWithMoreWords",
CamelCase: "camelCaseWithMoreWords",
KebabCase: "camel-case-with-more-words",
SnakeCase: "camel_case_with_more_words",
}},
{name: name, input: "Title Case With More Words", output: output{
PascalCase: "TitleCaseWithMoreWords",
CamelCase: "titleCaseWithMoreWords",
KebabCase: "title-case-with-more-words",
SnakeCase: "title_case_with_more_words",
}},
{name: name, input: "point.case.with.more.words", output: output{
PascalCase: "PointCaseWithMoreWords",
CamelCase: "pointCaseWithMoreWords",
KebabCase: "point-case-with-more-words",
SnakeCase: "point_case_with_more_words",
}},
{name: name, input: "snake_case__with___multiple____delimiters", output: output{
PascalCase: "SnakeCaseWithMultipleDelimiters",
CamelCase: "snakeCaseWithMultipleDelimiters",
KebabCase: "snake-case-with-multiple-delimiters",
SnakeCase: "snake_case_with_multiple_delimiters",
}},
{name: name, input: "SNAKE_CASE__WITH___multiple____DELIMITERS", output: output{
PascalCase: "SnakeCaseWithMultipleDelimiters",
CamelCase: "snakeCaseWithMultipleDelimiters",
KebabCase: "snake-case-with-multiple-delimiters",
SnakeCase: "snake_case_with_multiple_delimiters",
}},
{name: name, input: "kebab-case--with---multiple----delimiters", output: output{
PascalCase: "KebabCaseWithMultipleDelimiters",
CamelCase: "kebabCaseWithMultipleDelimiters",
KebabCase: "kebab-case-with-multiple-delimiters",
SnakeCase: "kebab_case_with_multiple_delimiters",
}},
{name: name, input: "Title Case With Multiple Delimiters", output: output{
PascalCase: "TitleCaseWithMultipleDelimiters",
CamelCase: "titleCaseWithMultipleDelimiters",
KebabCase: "title-case-with-multiple-delimiters",
SnakeCase: "title_case_with_multiple_delimiters",
}},
{name: name, input: "point.case..with...multiple....delimiters", output: output{
PascalCase: "PointCaseWithMultipleDelimiters",
CamelCase: "pointCaseWithMultipleDelimiters",
KebabCase: "point-case-with-multiple-delimiters",
SnakeCase: "point_case_with_multiple_delimiters",
}},
{name: name, input: " leading space", output: output{
PascalCase: "LeadingSpace",
CamelCase: "leadingSpace",
KebabCase: "leading-space",
SnakeCase: "leading_space",
}},
{name: name, input: " leading spaces", output: output{
PascalCase: "LeadingSpaces",
CamelCase: "leadingSpaces",
KebabCase: "leading-spaces",
SnakeCase: "leading_spaces",
}},
{name: name, input: "\t\t\r\n leading whitespaces", output: output{
PascalCase: "LeadingWhitespaces",
CamelCase: "leadingWhitespaces",
KebabCase: "leading-whitespaces",
SnakeCase: "leading_whitespaces",
}},
{name: name, input: "trailing space ", output: output{
PascalCase: "TrailingSpace",
CamelCase: "trailingSpace",
KebabCase: "trailing-space",
SnakeCase: "trailing_space",
}},
{name: name, input: "trailing spaces ", output: output{
PascalCase: "TrailingSpaces",
CamelCase: "trailingSpaces",
KebabCase: "trailing-spaces",
SnakeCase: "trailing_spaces",
}},
{name: name, input: "trailing whitespaces\t\t\r\n", output: output{
PascalCase: "TrailingWhitespaces",
CamelCase: "trailingWhitespaces",
KebabCase: "trailing-whitespaces",
SnakeCase: "trailing_whitespaces",
}},
{name: name, input: " on both sides ", output: output{
PascalCase: "OnBothSides",
CamelCase: "onBothSides",
KebabCase: "on-both-sides",
SnakeCase: "on_both_sides",
}},
{name: name, input: " many on both sides ", output: output{
PascalCase: "ManyOnBothSides",
CamelCase: "manyOnBothSides",
KebabCase: "many-on-both-sides",
SnakeCase: "many_on_both_sides",
}},
{name: name, input: "\r whitespaces on both sides\t\t\r\n", output: output{
PascalCase: "WhitespacesOnBothSides",
CamelCase: "whitespacesOnBothSides",
KebabCase: "whitespaces-on-both-sides",
SnakeCase: "whitespaces_on_both_sides",
}},
{name: name, input: " extraSpaces in_This TestCase Of MIXED_CASES\t", output: output{
PascalCase: "ExtraSpacesInThisTestCaseOfMixedCases",
CamelCase: "extraSpacesInThisTestCaseOfMixedCases",
KebabCase: "extra-spaces-in-this-test-case-of-mixed-cases",
SnakeCase: "extra_spaces_in_this_test_case_of_mixed_cases",
}},
{name: name, input: "CASEBreak", output: output{
PascalCase: "CaseBreak",
CamelCase: "caseBreak",
KebabCase: "case-break",
SnakeCase: "case_break",
}},
{name: name, input: "ID", output: output{
PascalCase: "Id",
CamelCase: "id",
KebabCase: "id",
SnakeCase: "id",
}},
{name: name, input: "userID", output: output{
PascalCase: "UserId",
CamelCase: "userId",
KebabCase: "user-id",
SnakeCase: "user_id",
}},
{name: name, input: "JSON_blob", output: output{
PascalCase: "JsonBlob",
CamelCase: "jsonBlob",
KebabCase: "json-blob",
SnakeCase: "json_blob",
}},
{name: name, input: "HTTPStatusCode", output: output{
PascalCase: "HttpStatusCode",
CamelCase: "httpStatusCode",
KebabCase: "http-status-code",
SnakeCase: "http_status_code",
}},
{name: name, input: "FreeBSD and SSLError are not golang initialisms", output: output{
PascalCase: "FreeBsdAndSslErrorAreNotGolangInitialisms",
CamelCase: "freeBsdAndSslErrorAreNotGolangInitialisms",
KebabCase: "free-bsd-and-ssl-error-are-not-golang-initialisms",
SnakeCase: "free_bsd_and_ssl_error_are_not_golang_initialisms",
}},
{name: name, input: "David's Computer", output: output{
PascalCase: "DavidSComputer",
CamelCase: "davidSComputer",
KebabCase: "david-s-computer",
SnakeCase: "david_s_computer",
}},
{name: name, input: "http200", output: output{
PascalCase: "Http200",
CamelCase: "http200",
KebabCase: "http-200",
SnakeCase: "http_200",
}},
{name: name, input: "NumberSplittingVersion1.0r3", output: output{
PascalCase: "NumberSplittingVersion10R3",
CamelCase: "numberSplittingVersion10R3",
KebabCase: "number-splitting-version-1-0-r3",
SnakeCase: "number_splitting_version_1_0_r3",
}},
{name: name, input: "When you have a comma, odd results", output: output{
PascalCase: "WhenYouHaveACommaOddResults",
CamelCase: "whenYouHaveACommaOddResults",
KebabCase: "when-you-have-a-comma-odd-results",
SnakeCase: "when_you_have_a_comma_odd_results",
}},
{name: name, input: "Ordinal numbers work: 1st 2nd and 3rd place", output: output{
PascalCase: "OrdinalNumbersWork1St2NdAnd3RdPlace",
CamelCase: "ordinalNumbersWork1St2NdAnd3RdPlace",
KebabCase: "ordinal-numbers-work-1-st-2-nd-and-3-rd-place",
SnakeCase: "ordinal_numbers_work_1_st_2_nd_and_3_rd_place",
}},
{name: name, input: "BadUTF8\xe2\xe2\xa1", output: output{
PascalCase: "BadUtf8",
CamelCase: "badUtf8",
KebabCase: "bad-utf-8",
SnakeCase: "bad_utf_8",
}},
{name: name, input: "IDENT3", output: output{
PascalCase: "Ident3",
CamelCase: "ident3",
KebabCase: "ident-3",
SnakeCase: "ident_3",
}},
{name: name, input: "LogRouterS3BucketName", output: output{
PascalCase: "LogRouterS3BucketName",
CamelCase: "logRouterS3BucketName",
KebabCase: "log-router-s3-bucket-name",
SnakeCase: "log_router_s3_bucket_name",
}},
{name: name, input: "PINEAPPLE", output: output{
PascalCase: "Pineapple",
CamelCase: "pineapple",
KebabCase: "pineapple",
SnakeCase: "pineapple",
}},
{name: name, input: "Int8Value", output: output{
PascalCase: "Int8Value",
CamelCase: "int8Value",
KebabCase: "int-8-value",
SnakeCase: "int_8_value",
}},
{name: name, input: "first.last", output: output{
PascalCase: "FirstLast",
CamelCase: "firstLast",
KebabCase: "first-last",
SnakeCase: "first_last",
}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
pascal := PascalCase(test.input)
if pascal != test.output.PascalCase {
t.Errorf("PascalCase(%q) = %q; expected %q", test.input, pascal, test.output.PascalCase)
}
camel := CamelCase(test.input)
if camel != test.output.CamelCase {
t.Errorf("CamelCase(%q) = %q; expected %q", test.input, camel, test.output.CamelCase)
}
kebab := KebabCase(test.input)
if kebab != test.output.KebabCase {
t.Errorf("KebabCase(%q) = %q; expected %q", test.input, kebab, test.output.KebabCase)
}
snake := SnakeCase(test.input)
if snake != test.output.SnakeCase {
t.Errorf("SnakeCase(%q) = %q; expected %q", test.input, snake, test.output.SnakeCase)
}
})
}
}
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{"UPPERCASE"}, []string{"UPPERCASE"}},
{"", args{"HTTPCode"}, []string{"HTTP", "Code"}},
{"", args{"Int8Value"}, []string{"Int", "8", "Value"}},
}
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)
})
}
}