From 0127cca7682659015d5cdc43f2ee6d8b375fc537 Mon Sep 17 00:00:00 2001 From: Roshan Ranabhat Date: Fri, 20 Jan 2023 11:02:29 +0545 Subject: [PATCH 1/2] feat: acronym function added --- example/main.go | 5 +++++ stringy.go | 15 +++++++++++++++ stringy_test.go | 8 ++++++++ 3 files changed, 28 insertions(+) diff --git a/example/main.go b/example/main.go index f7bcd14..7b8a192 100644 --- a/example/main.go +++ b/example/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/gobeam/stringy" ) @@ -79,4 +80,8 @@ func main() { pun := stringy.New("this really is a cliff") fmt.Println(pun.Suffix("hanger")) // this really is a cliffhanger + + acronym := stringy.New("Laugh Out Loud") + // fmt.Println(acronym.Acronym().Get()) // LOL + fmt.Println(acronym.Acronym().ToLower()) // lol } diff --git a/stringy.go b/stringy.go index 49040c9..943ef89 100644 --- a/stringy.go +++ b/stringy.go @@ -18,6 +18,7 @@ type input struct { // StringManipulation is an interface that holds all abstract methods to manipulate strings type StringManipulation interface { + Acronym() StringManipulation Between(start, end string) StringManipulation Boolean() bool CamelCase(rule ...string) string @@ -50,6 +51,20 @@ func New(val string) StringManipulation { return &input{Input: val} } +// Acronym func returns acronym of input string. +// You can chain to upper which with make result all upercase or ToLower +// which will make result all lower case or Get which will return result as it is +func (i *input) Acronym() StringManipulation { + input := getInput(*i) + words := strings.Fields(input) + var acronym string + for _, word := range words { + acronym += string(word[0]) + } + i.Result = acronym + return i +} + // Between takes two string params start and end which and returns // value which is in middle of start and end part of input. You can // chain to upper which with make result all upercase or ToLower which diff --git a/stringy_test.go b/stringy_test.go index 3ba9652..82a51fb 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -6,6 +6,14 @@ import ( var sm StringManipulation = New("This is example.") +func TestInput_Acronym(t *testing.T) { + acronym := New("Laugh Out Loud") + val := acronym.Acronym().Get() + if val != "LOL" { + t.Errorf("Expected: %s but got: %s", "IS", val) + } +} + func TestInput_Between(t *testing.T) { val := sm.Between("This", "example").ToUpper() if val != "IS" { From 103d950d1d755a9727acefcdaa7a5a7ee23de944 Mon Sep 17 00:00:00 2001 From: Roshan Ranabhat Date: Fri, 20 Jan 2023 11:05:43 +0545 Subject: [PATCH 2/2] chore: README.md update for Acronym --- README.md | 12 +++++++++++- example/main.go | 1 - 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9c3ae80..9fa31f6 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Convert string to camel case, snake case, kebab case / slugify, custom delimiter Suffix - + Acronym @@ -357,6 +357,16 @@ Suffix makes sure string has been suffixed with a given string and avoids adding ``` +#### Acronym() string + +Acronym func returns acronym of input string. You can chain ToUpper() which with make result all upercase or ToLower() which will make result all lower case or Get which will return result as it is + +```go + acronym := stringy.New("Laugh Out Loud") + fmt.Println(acronym.Acronym().ToLower()) // lol +``` + + ## Running the tests ``` bash diff --git a/example/main.go b/example/main.go index 7b8a192..f6b8339 100644 --- a/example/main.go +++ b/example/main.go @@ -82,6 +82,5 @@ func main() { fmt.Println(pun.Suffix("hanger")) // this really is a cliffhanger acronym := stringy.New("Laugh Out Loud") - // fmt.Println(acronym.Acronym().Get()) // LOL fmt.Println(acronym.Acronym().ToLower()) // lol }