Merge pull request #22 from gobeam/feat/title

Feat/title
This commit is contained in:
Roshan Ranabhat 2023-01-20 17:17:08 +05:45 committed by GitHub
commit 21f56c647b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 1 deletions

View File

@ -55,7 +55,7 @@ Convert string to camel case, snake case, kebab case / slugify, custom delimiter
<tr> <tr>
<td><a href="#suffixstring-string">Suffix</a></td> <td><a href="#suffixstring-string">Suffix</a></td>
<td><a href="#acronym-string">Acronym</a></td> <td><a href="#acronym-string">Acronym</a></td>
<td></td> <td><a href="#title-string">Title</a></td>
</tr> </tr>
</table> </table>
@ -306,6 +306,15 @@ Tease takes two params length and indicator and it shortens given string on pass
fmt.Println(teaseString.Tease(20, "...")) // Hello My name is Ros... fmt.Println(teaseString.Tease(20, "...")) // Hello My name is Ros...
``` ```
#### Title() string
Title returns string with first letter of each word in uppercase it can be chained on function which return StringManipulation interface.
```go
title := stringy.New("hello roshan")
fmt.Println(title.Title()) // Hello Roshan
```
#### ToLower() string #### ToLower() string

View File

@ -83,4 +83,7 @@ func main() {
acronym := stringy.New("Laugh Out Loud") acronym := stringy.New("Laugh Out Loud")
fmt.Println(acronym.Acronym().ToLower()) // lol fmt.Println(acronym.Acronym().ToLower()) // lol
title := stringy.New("this is just AN eXample")
fmt.Println(title.Title()) // This Is Just An Example
} }

View File

@ -39,6 +39,7 @@ type StringManipulation interface {
Surround(with string) string Surround(with string) string
SnakeCase(rule ...string) StringManipulation SnakeCase(rule ...string) StringManipulation
Tease(length int, indicator string) string Tease(length int, indicator string) string
Title() string
ToLower() string ToLower() string
ToUpper() string ToUpper() string
UcFirst() string UcFirst() string
@ -337,6 +338,17 @@ func (i *input) ToLower() (result string) {
return strings.ToLower(input) return strings.ToLower(input)
} }
// Title makes first letter of each word of user input to uppercase
// it can be chained on function which return StringManipulation interface
func (i *input) Title() (result string) {
input := getInput(*i)
wordArray := strings.Split(input, " ")
for i, word := range wordArray {
wordArray[i] = strings.ToUpper(string(word[0])) + strings.ToLower(word[1:])
}
return strings.Join(wordArray, " ")
}
// ToUpper makes all string of user input to uppercase // ToUpper makes all string of user input to uppercase
// it can be chained on function which return StringManipulation interface // it can be chained on function which return StringManipulation interface
func (i *input) ToUpper() string { func (i *input) ToUpper() string {

View File

@ -292,6 +292,14 @@ func TestInput_TeaseEmpty(t *testing.T) {
} }
} }
func TestInput_Title(t *testing.T) {
str := New("this is just AN eXample")
against := "This Is Just An Example"
if val := str.Title(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
}
func TestInput_UcFirst(t *testing.T) { func TestInput_UcFirst(t *testing.T) {
tests := []struct { tests := []struct {
name string name string