From b092f52a1b8e73bf8a5d531a2561047f72bede9e Mon Sep 17 00:00:00 2001 From: Aksel Meola Date: Fri, 14 May 2021 13:41:41 +0300 Subject: [PATCH] Add methods for prefixing and suffixing Closes #5 Add methods for prefixing and suffixing Closes #5 --- README.md | 20 ++++++++++++++++++++ example/main.go | 5 +++++ stringy.go | 22 ++++++++++++++++++++++ stringy_test.go | 27 +++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) diff --git a/README.md b/README.md index e3ce688..1dae6f6 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,26 @@ LcFirst simply returns result by lower casing first letter of string and it can ``` +#### Prefix(string) string + +Prefix makes sure string has been prefixed with a given string and avoids adding it again if it has. + +```go +ufo := stringy.New("known flying object") +fmt.Println(ufo.Prefix("un")) // unknown flying object +``` + + +#### Suffix(string) string + +Suffix makes sure string has been suffixed with a given string and avoids adding it again if it has. + +```go +pun := stringy.New("this really is a cliff") +fmt.Println(pun.Suffix("hanger")) // this really is a cliffhanger +``` + + ## Running the tests ``` bash diff --git a/example/main.go b/example/main.go index 9510def..f7bcd14 100644 --- a/example/main.go +++ b/example/main.go @@ -74,4 +74,9 @@ func main() { last := lcn.Last(4) fmt.Println(last) // 1348 + ufo := stringy.New("known flying object") + fmt.Println(ufo.Prefix("un")) // unknown flying object + + pun := stringy.New("this really is a cliff") + fmt.Println(pun.Suffix("hanger")) // this really is a cliffhanger } diff --git a/stringy.go b/stringy.go index da70928..cfcac33 100644 --- a/stringy.go +++ b/stringy.go @@ -41,6 +41,8 @@ type StringManipulation interface { ToLower() string ToUpper() string UcFirst() string + Prefix(with string) string + Suffix(with string) string } // New func returns pointer to input struct @@ -329,3 +331,23 @@ func (i *input) UcFirst() string { input := getInput(*i) return ucfirst(input) } + +// Prefix makes sure that string is prefixed with a given string +func (i *input) Prefix(with string) string { + input := getInput(*i) + if strings.HasPrefix(input, with) { + return input + } + + return with + input +} + +// Suffix makes sure that string is suffixed with a given string +func (i *input) Suffix(with string) string { + input := getInput(*i) + if strings.HasSuffix(input, with) { + return input + } + + return input + with +} diff --git a/stringy_test.go b/stringy_test.go index 11a5d09..bac1018 100644 --- a/stringy_test.go +++ b/stringy_test.go @@ -1,6 +1,7 @@ package stringy import ( + "fmt" "testing" ) @@ -310,3 +311,29 @@ func TestInput_Last(t *testing.T) { t.Errorf("Expected: to be %s but got: %s", against, last) } } + +func ExampleStringManipulation_Prefix() { + fmt.Println(New("foobar").Prefix("foo")) + fmt.Println(New("foobar").Prefix("foofoo")) + fmt.Println(New("foobar").Prefix("/")) + fmt.Println(New("").Prefix("_")) + // Output: + // foobar + // foofoofoobar + // /foobar + // _ +} + +func ExampleStringManipulation_Suffix() { + fmt.Println(New("foobar").Suffix("bar")) + fmt.Println(New("foobar").Suffix("barbar")) + fmt.Println(New("foobar").Suffix("/")) + fmt.Println(New("foobar/").Suffix("/")) + fmt.Println(New("").Suffix("_")) + // Output: + // foobar + // foobarbarbar + // foobar/ + // foobar/ + // _ +}