From 1770a505531548ad8e1eeed7ebe189fc6bbdcca1 Mon Sep 17 00:00:00 2001 From: Angel Cervera Claudio <293444+angelcervera@users.noreply.github.com> Date: Sat, 16 Mar 2024 10:19:07 +0000 Subject: [PATCH] API consistency and PascalCase --- README.md | 24 ++++++++++++++++++++++-- example/main.go | 4 ++-- stringy.go | 30 +++++++++++++++++++++++++----- stringy_test.go | 34 +++++++++++++++++++++++++++------- 4 files changed, 76 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5b88722..5d749d1 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,11 @@ Convert string to camel case, snake case, kebab case / slugify, custom delimiter Acronym Title + + PascalCase + + + @@ -124,13 +129,13 @@ CamelCase is variadic function which takes one Param rule i.e slice of strings a ```go camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##") - fmt.Println(camelCase.CamelCase("?", "", "#", "")) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt + fmt.Println(camelCase.CamelCase("?", "", "#", "")) // thisIsOneMessedUpStringCanWeReallyCamelCaseIt ``` look how it omitted ?## from string. If you dont want to omit anything and since it returns plain strings and you cant actually cap all or lower case all camelcase string its not required. ```go camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##") - fmt.Println(camelCase.CamelCase()) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt?## + fmt.Println(camelCase.CamelCase()) // thisIsOneMessedUpStringCanWeReallyCamelCaseIt?## ``` #### ContainsAll(check ...string) bool @@ -375,6 +380,21 @@ Acronym func returns acronym of input string. You can chain ToUpper() which with fmt.Println(acronym.Acronym().ToLower()) // lol ``` +#### PascalCase(rule ...string) string + +PascalCase is variadic function which takes one Param rule i.e slice of strings and it returns input type string in pascal case form and rule helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you don't have to worry about it. + +```go + pascalCase := stringy.New("ThisIsOne___messed up string. Can we Really pascal-case It ?##") + fmt.Println(pascalCase.PascalCase("?", "", "#", "")) // ThisIsOneMessedUpStringCanWeReallyPascalCaseIt +``` +look how it omitted ?## from string. If you dont want to omit anything and since it returns plain strings and you cant actually cap all or lower case all camelcase string it's not required. + +```go + pascalCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##") + fmt.Println(pascalCase.PascalCase()) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt?## +``` + ## Running the tests diff --git a/example/main.go b/example/main.go index 4a0e28f..5955d8e 100644 --- a/example/main.go +++ b/example/main.go @@ -26,7 +26,7 @@ func main() { fmt.Println(snakeCase.SnakeCase("?", "").ToLower()) camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##") - fmt.Println(camelCase.CamelCase("?", "", "#", "")) + fmt.Println(camelCase.CamelCase("?", "", "#", "").Get()) delimiterString := stringy.New("ThisIsOne___messed up string. Can we Really delimeter-case It?") fmt.Println(delimiterString.Delimited("?").Get()) @@ -58,7 +58,7 @@ func main() { fmt.Println(surroundStr.Surround("-")) str := stringy.New("hello__man how-Are you??") - result := str.CamelCase("?", "") + result := str.CamelCase("?", "").Get() fmt.Println(result) // HelloManHowAreYou snakeStr := str.SnakeCase("?", "") diff --git a/stringy.go b/stringy.go index 7d6ef5f..d8a0161 100644 --- a/stringy.go +++ b/stringy.go @@ -21,7 +21,8 @@ type StringManipulation interface { Acronym() StringManipulation Between(start, end string) StringManipulation Boolean() bool - CamelCase(rule ...string) string + PascalCase(rule ...string) StringManipulation + CamelCase(rule ...string) StringManipulation ContainsAll(check ...string) bool Delimited(delimiter string, rule ...string) StringManipulation First(length int) string @@ -112,10 +113,11 @@ func (i *input) Boolean() bool { // CamelCase is variadic function which takes one Param rule i.e slice of strings and it returns // input type string in camel case form and rule helps to omit character you want to omit from string. // By default special characters like "_", "-","."," " are l\treated like word separator and treated -// accordingly by default and you dont have to worry about it +// accordingly by default and you don't have to worry about it +// First letter will be lowercase. // Example input: hello user -// Result : HelloUser -func (i *input) CamelCase(rule ...string) string { +// Result : helloUser +func (i *input) CamelCase(rule ...string) StringManipulation { input := getInput(*i) // removing excess space wordArray := caseHelper(input, true, rule...) @@ -126,7 +128,25 @@ func (i *input) CamelCase(rule ...string) string { wordArray[i] = ucfirst(word) } } - return strings.Join(wordArray, "") + i.Result = strings.Join(wordArray, "") + return i +} + +// PascalCase is variadic function which takes one Param rule i.e slice of strings and it returns +// input type string in camel case form and rule helps to omit character you want to omit from string. +// By default special characters like "_", "-","."," " are l\treated like word separator and treated +// accordingly by default and you don't have to worry about it +// Example input: hello user +// Result : HelloUser +func (i *input) PascalCase(rule ...string) StringManipulation { + input := getInput(*i) + // removing excess space + wordArray := caseHelper(input, true, rule...) + for i, word := range wordArray { + wordArray[i] = ucfirst(word) + } + i.Result = strings.Join(wordArray, "") + return i } // ContainsAll is variadic function which takes slice of strings as param and checks if they are present diff --git a/stringy_test.go b/stringy_test.go index bc536b0..2aaa6df 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -77,16 +77,16 @@ func TestInput_BooleanError(t *testing.T) { func TestInput_CamelCase(t *testing.T) { str := New("Camel case this_complicated__string%%") - val := str.CamelCase("%", "") - if val != "camelCaseThisComplicatedString" { + against := "camelCaseThisComplicatedString" + if val := str.CamelCase("%", "").Get(); val != against { t.Errorf("Expected: to be %s but got: %s", "camelCaseThisComplicatedString", val) } } func TestInput_CamelCaseNoRule(t *testing.T) { str := New("Camel case this_complicated__string%%") - val := str.CamelCase() - if val != "camelCaseThisComplicatedString%%" { + against := "camelCaseThisComplicatedString%%" + if val := str.CamelCase().Get(); val != against { t.Errorf("Expected: to be %s but got: %s", "camelCaseThisComplicatedString", val) } } @@ -98,13 +98,33 @@ func TestInput_CamelCaseOddRuleError(t *testing.T) { } }() str := New("Camel case this_complicated__string%%") - val := str.CamelCase("%") - - if val != "camelCaseThisComplicatedString%%" { + against := "camelCaseThisComplicatedString%%" + if val := str.CamelCase("%").Get(); val != against { t.Errorf("Expected: to be %s but got: %s", "camelCaseThisComplicatedString", val) } } +func TestInput_PascalCaseNoRule(t *testing.T) { + str := New("pascal case this_complicated__string%%") + against := "PascalCaseThisComplicatedString%%" + if val := str.PascalCase().Get(); val != against { + t.Errorf("Expected: to be %s but got: %s", "PascalCaseThisComplicatedString", val) + } +} + +func TestInput_PascalCaseOddRuleError(t *testing.T) { + defer func() { + if err := recover(); err == nil { + t.Errorf("Error expected") + } + }() + str := New("pascal case this_complicated__string%%") + against := "PascalCaseThisComplicatedString%%" + if val := str.PascalCase("%").Get(); val != against { + t.Errorf("Expected: to be %s but got: %s", "PascalCaseThisComplicatedString", val) + } +} + func TestInput_ContainsAll(t *testing.T) { contains := New("hello mam how are you??") if val := contains.ContainsAll("mam", "?"); !val {