Skip to content

Commit

Permalink
Merge pull request #25 from angelcervera/pascal-code-and-consitency
Browse files Browse the repository at this point in the history
API consistency and PascalCase
  • Loading branch information
gobeam authored Mar 17, 2024
2 parents 21f56c6 + 1770a50 commit 9dd1f5c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 16 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ Convert string to camel case, snake case, kebab case / slugify, custom delimiter
<td><a href="#acronym-string">Acronym</a></td>
<td><a href="#title-string">Title</a></td>
</tr>
<tr>
<td><a href="#pascalcaserule-string-string">PascalCase</a></td>
<td></td>
<td></td>
</tr>
</table>


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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("?", "")
Expand Down
30 changes: 25 additions & 5 deletions stringy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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...)
Expand All @@ -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
Expand Down
34 changes: 27 additions & 7 deletions stringy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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 {
Expand Down

0 comments on commit 9dd1f5c

Please sign in to comment.