mirror of https://github.com/gobeam/stringy.git
feat: acronym function added
This commit is contained in:
parent
39a0bb1486
commit
0127cca768
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/gobeam/stringy"
|
"github.com/gobeam/stringy"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -79,4 +80,8 @@ func main() {
|
||||||
|
|
||||||
pun := stringy.New("this really is a cliff")
|
pun := stringy.New("this really is a cliff")
|
||||||
fmt.Println(pun.Suffix("hanger")) // this really is a cliffhanger
|
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
|
||||||
}
|
}
|
||||||
|
|
15
stringy.go
15
stringy.go
|
@ -18,6 +18,7 @@ type input struct {
|
||||||
|
|
||||||
// StringManipulation is an interface that holds all abstract methods to manipulate strings
|
// StringManipulation is an interface that holds all abstract methods to manipulate strings
|
||||||
type StringManipulation interface {
|
type StringManipulation interface {
|
||||||
|
Acronym() StringManipulation
|
||||||
Between(start, end string) StringManipulation
|
Between(start, end string) StringManipulation
|
||||||
Boolean() bool
|
Boolean() bool
|
||||||
CamelCase(rule ...string) string
|
CamelCase(rule ...string) string
|
||||||
|
@ -50,6 +51,20 @@ func New(val string) StringManipulation {
|
||||||
return &input{Input: val}
|
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
|
// 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
|
// 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
|
// chain to upper which with make result all upercase or ToLower which
|
||||||
|
|
|
@ -6,6 +6,14 @@ import (
|
||||||
|
|
||||||
var sm StringManipulation = New("This is example.")
|
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) {
|
func TestInput_Between(t *testing.T) {
|
||||||
val := sm.Between("This", "example").ToUpper()
|
val := sm.Between("This", "example").ToUpper()
|
||||||
if val != "IS" {
|
if val != "IS" {
|
||||||
|
|
Loading…
Reference in New Issue