forked from mirror/stringy
Doc comment added
This commit is contained in:
parent
a9a3cca5f9
commit
0fb167e55c
|
@ -8,10 +8,10 @@ import (
|
|||
func main() {
|
||||
|
||||
strBetween := New("HelloMyName")
|
||||
fmt.Println(strBetween.Between("hello","name").ToUpper())
|
||||
fmt.Println(strBetween.Between("hello", "name").ToUpper())
|
||||
|
||||
teaseString := New("Hello My name is Roshan. I am full stack developer")
|
||||
fmt.Println(teaseString.Tease(100,"..."))
|
||||
fmt.Println(teaseString.Tease(100, "..."))
|
||||
|
||||
replaceFirst := New("Hello My name is Roshan and his name is Alis.")
|
||||
fmt.Println(replaceFirst.ReplaceFirst("name", "nombre"))
|
||||
|
@ -20,16 +20,16 @@ func main() {
|
|||
fmt.Println(replaceLast.ReplaceLast("name", "nombre"))
|
||||
|
||||
snakeCase := New("ThisIsOne___messed up string. Can we Really Snake Case It?")
|
||||
fmt.Println(snakeCase.SnakeCase("?","").Get())
|
||||
fmt.Println(snakeCase.SnakeCase("?", "").Get())
|
||||
|
||||
camelCase := New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
|
||||
fmt.Println(camelCase.CamelCase("?","","#",""))
|
||||
fmt.Println(camelCase.CamelCase("?", "", "#", ""))
|
||||
|
||||
delimiterString := New("ThisIsOne___messed up string. Can we Really delimeter-case It?")
|
||||
fmt.Println(delimiterString.Delimited("?").Get())
|
||||
|
||||
contains := New("hello mam how are you??")
|
||||
fmt.Println(contains.ContainsAll("mams","?"))
|
||||
fmt.Println(contains.ContainsAll("mams", "?"))
|
||||
|
||||
lines := New("fòô\r\nbàř\nyolo123")
|
||||
fmt.Println(lines.Lines())
|
||||
|
@ -38,9 +38,9 @@ func main() {
|
|||
fmt.Println(reverse.Reverse())
|
||||
|
||||
pad := New("Roshan")
|
||||
fmt.Println(pad.Pad(0, "0","both"))
|
||||
fmt.Println(pad.Pad(0, "0","left"))
|
||||
fmt.Println(pad.Pad(0, "0","right"))
|
||||
fmt.Println(pad.Pad(0, "0", "both"))
|
||||
fmt.Println(pad.Pad(0, "0", "left"))
|
||||
fmt.Println(pad.Pad(0, "0", "right"))
|
||||
|
||||
shuffleString := New("roshan")
|
||||
fmt.Println(shuffleString.Shuffle())
|
||||
|
|
10
helper.go
10
helper.go
|
@ -7,21 +7,21 @@ import (
|
|||
"unicode"
|
||||
)
|
||||
|
||||
func caseHelper(input string,isCamel bool, rule ...string) []string {
|
||||
func caseHelper(input string, isCamel bool, rule ...string) []string {
|
||||
if !isCamel {
|
||||
re := regexp.MustCompile("([a-z])([A-Z])")
|
||||
input = re.ReplaceAllString(input, "$1 $2")
|
||||
re := regexp.MustCompile(SelectCapital)
|
||||
input = re.ReplaceAllString(input, ReplaceCapital)
|
||||
}
|
||||
input = strings.Join(strings.Fields(strings.TrimSpace(input)), " ")
|
||||
if len(rule) <= 1 {
|
||||
rule = []string{".", " ", "_", " ", "-", " "}
|
||||
}
|
||||
if len(rule) > 1 && len(rule)%2 != 0 {
|
||||
panic(errors.New("odd number rule provided please provide in even count"))
|
||||
panic(errors.New(OddError))
|
||||
}
|
||||
rule = append(rule, ".", " ", "_", " ", "-", " ")
|
||||
|
||||
replacer := strings.NewReplacer(rule ...)
|
||||
replacer := strings.NewReplacer(rule...)
|
||||
input = replacer.Replace(input)
|
||||
words := strings.Fields(input)
|
||||
return words
|
||||
|
|
18
message.go
18
message.go
|
@ -1,11 +1,15 @@
|
|||
package string_manipulation
|
||||
|
||||
const (
|
||||
First = "first"
|
||||
Last = "last"
|
||||
Left = "left"
|
||||
Right = "right"
|
||||
Both = "both"
|
||||
First = "first"
|
||||
Last = "last"
|
||||
Left = "left"
|
||||
Right = "right"
|
||||
Both = "both"
|
||||
OddError = "odd number rule provided please provide in even count"
|
||||
SelectCapital = "([a-z])([A-Z])"
|
||||
ReplaceCapital = "$1 $2"
|
||||
)
|
||||
var False = []string{"off","no","0","false"}
|
||||
var True = []string{"on","yes","1","True"}
|
||||
|
||||
var False = []string{"off", "no", "0", "false"}
|
||||
var True = []string{"on", "yes", "1", "True"}
|
||||
|
|
59
stringy.go
59
stringy.go
|
@ -66,11 +66,11 @@ func (i *input) Between(start, end string) StringManipulation {
|
|||
} else if len(input) > 0 {
|
||||
endIndex = len(input)
|
||||
}
|
||||
i.Result = i.Input[startIndex:endIndex]
|
||||
i.Result = strings.TrimSpace(i.Input[startIndex:endIndex])
|
||||
return i
|
||||
}
|
||||
|
||||
// Boolean dunc return boolean value of string value like on, off, 0, 1, yes, no
|
||||
// Boolean func return boolean value of string value like on, off, 0, 1, yes, no
|
||||
// returns boolean value of string input
|
||||
func (i *input) Boolean() bool {
|
||||
input := getInput(*i)
|
||||
|
@ -86,13 +86,14 @@ func (i *input) Boolean() bool {
|
|||
panic(errors.New("invalid string value to test boolean value"))
|
||||
}
|
||||
|
||||
// CamelCase function returns camel case value of string
|
||||
// CamelCase takes one Param rule and it returns passed string in
|
||||
// camel case form and rule helps to omit character you want from string
|
||||
// Example input: hello user
|
||||
// Result : HelloUser
|
||||
func (i *input) CamelCase(rule ...string) string {
|
||||
input := getInput(*i)
|
||||
// removing excess space
|
||||
wordArray := caseHelper(input, true, rule ...)
|
||||
wordArray := caseHelper(input, true, rule...)
|
||||
for i, word := range wordArray {
|
||||
wordArray[i] = ucfirst(word)
|
||||
}
|
||||
|
@ -117,22 +118,30 @@ func (i *input) Delimited(delimiter string, rule ...string) StringManipulation {
|
|||
if strings.TrimSpace(delimiter) == "" {
|
||||
delimiter = "."
|
||||
}
|
||||
wordArray := caseHelper(input, false, rule ...)
|
||||
wordArray := caseHelper(input, false, rule...)
|
||||
i.Result = strings.Join(wordArray, delimiter)
|
||||
return i
|
||||
}
|
||||
|
||||
// Get simply returns result and can be chained on function which
|
||||
// returns StringManipulation interface
|
||||
func (i *input) Get() string {
|
||||
return getInput(*i)
|
||||
}
|
||||
|
||||
// KebabCase takes one Param rule and it returns passed string in
|
||||
// kebab case form and rule helps to omit character you want from string
|
||||
// Example input: hello user
|
||||
// Result : hello-user
|
||||
func (i *input) KebabCase(rule ...string) StringManipulation {
|
||||
input := getInput(*i)
|
||||
wordArray := caseHelper(input, false, rule ...)
|
||||
wordArray := caseHelper(input, false, rule...)
|
||||
i.Result = strings.Join(wordArray, "-")
|
||||
return i
|
||||
}
|
||||
|
||||
// LcFirst simply returns result by lowercasing first letter of string and
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) LcFirst() string {
|
||||
input := getInput(*i)
|
||||
for i, v := range input {
|
||||
|
@ -141,6 +150,7 @@ func (i *input) LcFirst() string {
|
|||
return input
|
||||
}
|
||||
|
||||
// Lines returns slice of strings by removing white space characters
|
||||
func (i *input) Lines() []string {
|
||||
input := getInput(*i)
|
||||
matchWord := regexp.MustCompile(`[\s]*[\W]\pN`)
|
||||
|
@ -148,6 +158,10 @@ func (i *input) Lines() []string {
|
|||
return strings.Fields(strings.TrimSpace(result))
|
||||
}
|
||||
|
||||
// Pad takes three param length i.e total length to be after padding,
|
||||
// with i.e what to pad with and pad type which can be (both or left or right)
|
||||
// it return string after padding upto length by with param and on padType type
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) Pad(length int, with, padType string) string {
|
||||
input := getInput(*i)
|
||||
inputLength := len(input)
|
||||
|
@ -173,6 +187,8 @@ func (i *input) Pad(length int, with, padType string) string {
|
|||
}
|
||||
}
|
||||
|
||||
// RemoveSpecialCharacter removes all special characters and returns the string
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) RemoveSpecialCharacter() string {
|
||||
input := getInput(*i)
|
||||
var result strings.Builder
|
||||
|
@ -188,15 +204,26 @@ func (i *input) RemoveSpecialCharacter() string {
|
|||
return result.String()
|
||||
}
|
||||
|
||||
// ReplaceFirst takes two param search and replace
|
||||
// it return string by searching search sub string and replacing it
|
||||
// with replace substring on first occurrence
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) ReplaceFirst(search, replace string) string {
|
||||
input := getInput(*i)
|
||||
return replaceStr(input, search, replace, First)
|
||||
}
|
||||
|
||||
// ReplaceLast takes two param search and replace
|
||||
// it return string by searching search sub string and replacing it
|
||||
// with replace substring on last occurrence
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) ReplaceLast(search, replace string) string {
|
||||
input := getInput(*i)
|
||||
return replaceStr(input, search, replace, Last)
|
||||
}
|
||||
|
||||
// Reverse reverses the passed strings
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) Reverse() string {
|
||||
input := getInput(*i)
|
||||
r := []rune(input)
|
||||
|
@ -206,6 +233,8 @@ func (i *input) Reverse() string {
|
|||
return string(r)
|
||||
}
|
||||
|
||||
// Shuffle shuffles the given string randomly
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) Shuffle() string {
|
||||
input := getInput(*i)
|
||||
rand.Seed(time.Now().Unix())
|
||||
|
@ -217,18 +246,28 @@ func (i *input) Shuffle() string {
|
|||
return string(inRune)
|
||||
}
|
||||
|
||||
// SnakeCase is variadic function that takes one param rule
|
||||
// it returns passed string in snake case form and rule helps to
|
||||
// omit character you want from string
|
||||
// Example input: hello user
|
||||
// Result : hello_user
|
||||
func (i *input) SnakeCase(rule ...string) StringManipulation {
|
||||
input := getInput(*i)
|
||||
wordArray := caseHelper(input, false, rule ...)
|
||||
wordArray := caseHelper(input, false, rule...)
|
||||
i.Result = strings.Join(wordArray, "_")
|
||||
return i
|
||||
}
|
||||
|
||||
// Surround takes one param with which is used to surround user input
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) Surround(with string) string {
|
||||
input := getInput(*i)
|
||||
return with + input + with
|
||||
}
|
||||
|
||||
// Tease takes two params length and indicator and it shortens given string
|
||||
// on passed length and adds indicator on end
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) Tease(length int, indicator string) string {
|
||||
input := getInput(*i)
|
||||
if input == "" || len(input) < length {
|
||||
|
@ -237,16 +276,22 @@ func (i *input) Tease(length int, indicator string) string {
|
|||
return input[:length] + indicator
|
||||
}
|
||||
|
||||
// ToLowerr makes all string of user input to lowercase
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) ToLower() (result string) {
|
||||
input := getInput(*i)
|
||||
return strings.ToLower(input)
|
||||
}
|
||||
|
||||
// ToUpper makes all string of user input to uppercase
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) ToUpper() string {
|
||||
input := getInput(*i)
|
||||
return strings.ToUpper(input)
|
||||
}
|
||||
|
||||
// UcFirst makes first word of user input to uppercase
|
||||
// it can be chained on function which return StringManipulation interface
|
||||
func (i *input) UcFirst() string {
|
||||
input := getInput(*i)
|
||||
return ucfirst(input)
|
||||
|
|
|
@ -0,0 +1,160 @@
|
|||
package string_manipulation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
var sm StringManipulation = New("This is example.")
|
||||
|
||||
func TestInput_Between(t *testing.T) {
|
||||
val := sm.Between("This", "example").ToUpper()
|
||||
if val != "IS" {
|
||||
t.Errorf("Expected: %s but got: %s", "IS", val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_Boolean(t *testing.T) {
|
||||
str := New("on")
|
||||
val := str.Boolean()
|
||||
if !val {
|
||||
t.Errorf("Expected: to be true but got: %v", val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_CamelCase(t *testing.T) {
|
||||
str := New("Camel case this_complicated__string%%")
|
||||
val := str.CamelCase("%", "")
|
||||
if val != "CamelCaseThisComplicatedString" {
|
||||
t.Errorf("Expected: to be %s but got: %s", "CamelCaseThisComplicatedString", val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_ContainsAll(t *testing.T) {
|
||||
contains := New("hello mam how are you??")
|
||||
if val := contains.ContainsAll("mam", "?"); !val {
|
||||
t.Errorf("Expected value to be true but got false")
|
||||
}
|
||||
if val := contains.ContainsAll("non existent"); val {
|
||||
t.Errorf("Expected value to be false but got true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_Delimited(t *testing.T) {
|
||||
str := New("Delimited case this_complicated__string@@")
|
||||
against := "delimited.case.this.complicated.string"
|
||||
if val := str.Delimited(".", "@", "").ToLower(); val != against {
|
||||
t.Errorf("Expected: to be %s but got: %s", against, val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_KebabCase(t *testing.T) {
|
||||
str := New("Kebab case this-complicated___string@@")
|
||||
against := "Kebab-case-this-complicated-string"
|
||||
if val := str.KebabCase("@", "").Get(); val != against {
|
||||
t.Errorf("Expected: to be %s but got: %s", against, val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_LcFirst(t *testing.T) {
|
||||
str := New("this is an all lower")
|
||||
against := "This is an all lower"
|
||||
if val := str.LcFirst(); val != against {
|
||||
t.Errorf("Expected: to be %s but got: %s", against, val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_Lines(t *testing.T) {
|
||||
lines := New("fòô\r\nbàř\nyolo")
|
||||
strSlic := lines.Lines()
|
||||
if len(strSlic) != 3 {
|
||||
t.Errorf("Length expected to be 3 but got: %d", len(strSlic))
|
||||
}
|
||||
if strSlic[0] != "fòô" {
|
||||
t.Errorf("Expected: %s but got: %s", "fòô", strSlic[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_Pad(t *testing.T) {
|
||||
pad := New("Roshan")
|
||||
if result := pad.Pad(10, "0", "both"); result != "00Roshan00" {
|
||||
t.Errorf("Expected: %s but got: %s", "00Roshan00", result)
|
||||
}
|
||||
if result := pad.Pad(10, "0", "left"); result != "0000Roshan" {
|
||||
t.Errorf("Expected: %s but got: %s", "0000Roshan", result)
|
||||
}
|
||||
if result := pad.Pad(10, "0", "right"); result != "Roshan0000" {
|
||||
t.Errorf("Expected: %s but got: %s", "Roshan0000", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_RemoveSpecialCharacter(t *testing.T) {
|
||||
cleanString := New("special@#remove%%%%")
|
||||
against := "specialremove"
|
||||
if result := cleanString.RemoveSpecialCharacter(); result != against {
|
||||
t.Errorf("Expected: %s but got: %s", against, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_ReplaceFirst(t *testing.T) {
|
||||
replaceFirst := New("Hello My name is Roshan and his name is Alis.")
|
||||
against := "Hello My nombre is Roshan and his name is Alis."
|
||||
if result := replaceFirst.ReplaceFirst("name", "nombre"); result != against {
|
||||
t.Errorf("Expected: %s but got: %s", against, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_ReplaceLast(t *testing.T) {
|
||||
replaceLast := New("Hello My name is Roshan and his name is Alis.")
|
||||
against := "Hello My name is Roshan and his nombre is Alis."
|
||||
if result := replaceLast.ReplaceLast("name", "nombre"); result != against {
|
||||
t.Errorf("Expected: %s but got: %s", against, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_Reverse(t *testing.T) {
|
||||
reverseString := New("roshan")
|
||||
against := "nahsor"
|
||||
if result := reverseString.Reverse(); result != against {
|
||||
t.Errorf("Expected: %s but got: %s", against, result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_Shuffle(t *testing.T) {
|
||||
check := "roshan"
|
||||
shuffleString := New(check)
|
||||
if result := shuffleString.Shuffle(); len(result) != len(check) && check == result {
|
||||
t.Errorf("Shuffle string gave wrong output")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_SnakeCase(t *testing.T) {
|
||||
str := New("SnakeCase this-complicated___string@@")
|
||||
against := "snake_case_this_complicated_string"
|
||||
if val := str.SnakeCase("@", "").ToLower(); val != against {
|
||||
t.Errorf("Expected: to be %s but got: %s", against, val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_Surround(t *testing.T) {
|
||||
str := New("this")
|
||||
against := "__this__"
|
||||
if val := str.Surround("__"); val != against {
|
||||
t.Errorf("Expected: to be %s but got: %s", against, val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_Tease(t *testing.T) {
|
||||
str := New("This is just simple paragraph on lorem ipsum.")
|
||||
against := "This is just..."
|
||||
if val := str.Tease(12, "..."); val != against {
|
||||
t.Errorf("Expected: to be %s but got: %s", against, val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInput_UcFirst(t *testing.T) {
|
||||
str := New("this is test")
|
||||
against := "This is test"
|
||||
if val := str.UcFirst(); val != against {
|
||||
t.Errorf("Expected: to be %s but got: %s", against, val)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue