Skip to content

Commit 9dd1f5c

Browse files
authored
Merge pull request #25 from angelcervera/pascal-code-and-consitency
API consistency and PascalCase
2 parents 21f56c6 + 1770a50 commit 9dd1f5c

4 files changed

Lines changed: 76 additions & 16 deletions

File tree

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ Convert string to camel case, snake case, kebab case / slugify, custom delimiter
5757
<td><a href="#acronym-string">Acronym</a></td>
5858
<td><a href="#title-string">Title</a></td>
5959
</tr>
60+
<tr>
61+
<td><a href="#pascalcaserule-string-string">PascalCase</a></td>
62+
<td></td>
63+
<td></td>
64+
</tr>
6065
</table>
6166

6267

@@ -124,13 +129,13 @@ CamelCase is variadic function which takes one Param rule i.e slice of strings a
124129

125130
```go
126131
camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
127-
fmt.Println(camelCase.CamelCase("?", "", "#", "")) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt
132+
fmt.Println(camelCase.CamelCase("?", "", "#", "")) // thisIsOneMessedUpStringCanWeReallyCamelCaseIt
128133
```
129134
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.
130135

131136
```go
132137
camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
133-
fmt.Println(camelCase.CamelCase()) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt?##
138+
fmt.Println(camelCase.CamelCase()) // thisIsOneMessedUpStringCanWeReallyCamelCaseIt?##
134139
```
135140

136141
#### ContainsAll(check ...string) bool
@@ -375,6 +380,21 @@ Acronym func returns acronym of input string. You can chain ToUpper() which with
375380
fmt.Println(acronym.Acronym().ToLower()) // lol
376381
```
377382

383+
#### PascalCase(rule ...string) string
384+
385+
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.
386+
387+
```go
388+
pascalCase := stringy.New("ThisIsOne___messed up string. Can we Really pascal-case It ?##")
389+
fmt.Println(pascalCase.PascalCase("?", "", "#", "")) // ThisIsOneMessedUpStringCanWeReallyPascalCaseIt
390+
```
391+
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.
392+
393+
```go
394+
pascalCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
395+
fmt.Println(pascalCase.PascalCase()) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt?##
396+
```
397+
378398

379399
## Running the tests
380400

example/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func main() {
2626
fmt.Println(snakeCase.SnakeCase("?", "").ToLower())
2727

2828
camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
29-
fmt.Println(camelCase.CamelCase("?", "", "#", ""))
29+
fmt.Println(camelCase.CamelCase("?", "", "#", "").Get())
3030

3131
delimiterString := stringy.New("ThisIsOne___messed up string. Can we Really delimeter-case It?")
3232
fmt.Println(delimiterString.Delimited("?").Get())
@@ -58,7 +58,7 @@ func main() {
5858
fmt.Println(surroundStr.Surround("-"))
5959

6060
str := stringy.New("hello__man how-Are you??")
61-
result := str.CamelCase("?", "")
61+
result := str.CamelCase("?", "").Get()
6262
fmt.Println(result) // HelloManHowAreYou
6363

6464
snakeStr := str.SnakeCase("?", "")

stringy.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ type StringManipulation interface {
2121
Acronym() StringManipulation
2222
Between(start, end string) StringManipulation
2323
Boolean() bool
24-
CamelCase(rule ...string) string
24+
PascalCase(rule ...string) StringManipulation
25+
CamelCase(rule ...string) StringManipulation
2526
ContainsAll(check ...string) bool
2627
Delimited(delimiter string, rule ...string) StringManipulation
2728
First(length int) string
@@ -112,10 +113,11 @@ func (i *input) Boolean() bool {
112113
// CamelCase is variadic function which takes one Param rule i.e slice of strings and it returns
113114
// input type string in camel case form and rule helps to omit character you want to omit from string.
114115
// By default special characters like "_", "-","."," " are l\treated like word separator and treated
115-
// accordingly by default and you dont have to worry about it
116+
// accordingly by default and you don't have to worry about it
117+
// First letter will be lowercase.
116118
// Example input: hello user
117-
// Result : HelloUser
118-
func (i *input) CamelCase(rule ...string) string {
119+
// Result : helloUser
120+
func (i *input) CamelCase(rule ...string) StringManipulation {
119121
input := getInput(*i)
120122
// removing excess space
121123
wordArray := caseHelper(input, true, rule...)
@@ -126,7 +128,25 @@ func (i *input) CamelCase(rule ...string) string {
126128
wordArray[i] = ucfirst(word)
127129
}
128130
}
129-
return strings.Join(wordArray, "")
131+
i.Result = strings.Join(wordArray, "")
132+
return i
133+
}
134+
135+
// PascalCase is variadic function which takes one Param rule i.e slice of strings and it returns
136+
// input type string in camel case form and rule helps to omit character you want to omit from string.
137+
// By default special characters like "_", "-","."," " are l\treated like word separator and treated
138+
// accordingly by default and you don't have to worry about it
139+
// Example input: hello user
140+
// Result : HelloUser
141+
func (i *input) PascalCase(rule ...string) StringManipulation {
142+
input := getInput(*i)
143+
// removing excess space
144+
wordArray := caseHelper(input, true, rule...)
145+
for i, word := range wordArray {
146+
wordArray[i] = ucfirst(word)
147+
}
148+
i.Result = strings.Join(wordArray, "")
149+
return i
130150
}
131151

132152
// ContainsAll is variadic function which takes slice of strings as param and checks if they are present

stringy_test.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ func TestInput_BooleanError(t *testing.T) {
7777

7878
func TestInput_CamelCase(t *testing.T) {
7979
str := New("Camel case this_complicated__string%%")
80-
val := str.CamelCase("%", "")
81-
if val != "camelCaseThisComplicatedString" {
80+
against := "camelCaseThisComplicatedString"
81+
if val := str.CamelCase("%", "").Get(); val != against {
8282
t.Errorf("Expected: to be %s but got: %s", "camelCaseThisComplicatedString", val)
8383
}
8484
}
8585

8686
func TestInput_CamelCaseNoRule(t *testing.T) {
8787
str := New("Camel case this_complicated__string%%")
88-
val := str.CamelCase()
89-
if val != "camelCaseThisComplicatedString%%" {
88+
against := "camelCaseThisComplicatedString%%"
89+
if val := str.CamelCase().Get(); val != against {
9090
t.Errorf("Expected: to be %s but got: %s", "camelCaseThisComplicatedString", val)
9191
}
9292
}
@@ -98,13 +98,33 @@ func TestInput_CamelCaseOddRuleError(t *testing.T) {
9898
}
9999
}()
100100
str := New("Camel case this_complicated__string%%")
101-
val := str.CamelCase("%")
102-
103-
if val != "camelCaseThisComplicatedString%%" {
101+
against := "camelCaseThisComplicatedString%%"
102+
if val := str.CamelCase("%").Get(); val != against {
104103
t.Errorf("Expected: to be %s but got: %s", "camelCaseThisComplicatedString", val)
105104
}
106105
}
107106

107+
func TestInput_PascalCaseNoRule(t *testing.T) {
108+
str := New("pascal case this_complicated__string%%")
109+
against := "PascalCaseThisComplicatedString%%"
110+
if val := str.PascalCase().Get(); val != against {
111+
t.Errorf("Expected: to be %s but got: %s", "PascalCaseThisComplicatedString", val)
112+
}
113+
}
114+
115+
func TestInput_PascalCaseOddRuleError(t *testing.T) {
116+
defer func() {
117+
if err := recover(); err == nil {
118+
t.Errorf("Error expected")
119+
}
120+
}()
121+
str := New("pascal case this_complicated__string%%")
122+
against := "PascalCaseThisComplicatedString%%"
123+
if val := str.PascalCase("%").Get(); val != against {
124+
t.Errorf("Expected: to be %s but got: %s", "PascalCaseThisComplicatedString", val)
125+
}
126+
}
127+
108128
func TestInput_ContainsAll(t *testing.T) {
109129
contains := New("hello mam how are you??")
110130
if val := contains.ContainsAll("mam", "?"); !val {

0 commit comments

Comments
 (0)