Doc comment added

This commit is contained in:
Roshan Ranabhat 2020-04-05 16:47:47 +05:45
parent a9a3cca5f9
commit 0fb167e55c
5 changed files with 236 additions and 27 deletions

View File

@ -8,10 +8,10 @@ import (
func main() { func main() {
strBetween := New("HelloMyName") 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") 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.") replaceFirst := New("Hello My name is Roshan and his name is Alis.")
fmt.Println(replaceFirst.ReplaceFirst("name", "nombre")) fmt.Println(replaceFirst.ReplaceFirst("name", "nombre"))
@ -20,16 +20,16 @@ func main() {
fmt.Println(replaceLast.ReplaceLast("name", "nombre")) fmt.Println(replaceLast.ReplaceLast("name", "nombre"))
snakeCase := New("ThisIsOne___messed up string. Can we Really Snake Case It?") 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 ?##") 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?") delimiterString := New("ThisIsOne___messed up string. Can we Really delimeter-case It?")
fmt.Println(delimiterString.Delimited("?").Get()) fmt.Println(delimiterString.Delimited("?").Get())
contains := New("hello mam how are you??") contains := New("hello mam how are you??")
fmt.Println(contains.ContainsAll("mams","?")) fmt.Println(contains.ContainsAll("mams", "?"))
lines := New("fòô\r\nbàř\nyolo123") lines := New("fòô\r\nbàř\nyolo123")
fmt.Println(lines.Lines()) fmt.Println(lines.Lines())
@ -38,9 +38,9 @@ func main() {
fmt.Println(reverse.Reverse()) fmt.Println(reverse.Reverse())
pad := New("Roshan") pad := New("Roshan")
fmt.Println(pad.Pad(0, "0","both")) fmt.Println(pad.Pad(0, "0", "both"))
fmt.Println(pad.Pad(0, "0","left")) fmt.Println(pad.Pad(0, "0", "left"))
fmt.Println(pad.Pad(0, "0","right")) fmt.Println(pad.Pad(0, "0", "right"))
shuffleString := New("roshan") shuffleString := New("roshan")
fmt.Println(shuffleString.Shuffle()) fmt.Println(shuffleString.Shuffle())

View File

@ -7,21 +7,21 @@ import (
"unicode" "unicode"
) )
func caseHelper(input string,isCamel bool, rule ...string) []string { func caseHelper(input string, isCamel bool, rule ...string) []string {
if !isCamel { if !isCamel {
re := regexp.MustCompile("([a-z])([A-Z])") re := regexp.MustCompile(SelectCapital)
input = re.ReplaceAllString(input, "$1 $2") input = re.ReplaceAllString(input, ReplaceCapital)
} }
input = strings.Join(strings.Fields(strings.TrimSpace(input)), " ") input = strings.Join(strings.Fields(strings.TrimSpace(input)), " ")
if len(rule) <= 1 { if len(rule) <= 1 {
rule = []string{".", " ", "_", " ", "-", " "} rule = []string{".", " ", "_", " ", "-", " "}
} }
if len(rule) > 1 && len(rule)%2 != 0 { 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, ".", " ", "_", " ", "-", " ") rule = append(rule, ".", " ", "_", " ", "-", " ")
replacer := strings.NewReplacer(rule ...) replacer := strings.NewReplacer(rule...)
input = replacer.Replace(input) input = replacer.Replace(input)
words := strings.Fields(input) words := strings.Fields(input)
return words return words

View File

@ -1,11 +1,15 @@
package string_manipulation package string_manipulation
const ( const (
First = "first" First = "first"
Last = "last" Last = "last"
Left = "left" Left = "left"
Right = "right" Right = "right"
Both = "both" 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"}

View File

@ -66,11 +66,11 @@ func (i *input) Between(start, end string) StringManipulation {
} else if len(input) > 0 { } else if len(input) > 0 {
endIndex = len(input) endIndex = len(input)
} }
i.Result = i.Input[startIndex:endIndex] i.Result = strings.TrimSpace(i.Input[startIndex:endIndex])
return i 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 // returns boolean value of string input
func (i *input) Boolean() bool { func (i *input) Boolean() bool {
input := getInput(*i) input := getInput(*i)
@ -86,13 +86,14 @@ func (i *input) Boolean() bool {
panic(errors.New("invalid string value to test boolean value")) 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 // Example input: hello user
// Result : HelloUser // Result : HelloUser
func (i *input) CamelCase(rule ...string) string { func (i *input) CamelCase(rule ...string) string {
input := getInput(*i) input := getInput(*i)
// removing excess space // removing excess space
wordArray := caseHelper(input, true, rule ...) wordArray := caseHelper(input, true, rule...)
for i, word := range wordArray { for i, word := range wordArray {
wordArray[i] = ucfirst(word) wordArray[i] = ucfirst(word)
} }
@ -117,22 +118,30 @@ func (i *input) Delimited(delimiter string, rule ...string) StringManipulation {
if strings.TrimSpace(delimiter) == "" { if strings.TrimSpace(delimiter) == "" {
delimiter = "." delimiter = "."
} }
wordArray := caseHelper(input, false, rule ...) wordArray := caseHelper(input, false, rule...)
i.Result = strings.Join(wordArray, delimiter) i.Result = strings.Join(wordArray, delimiter)
return i return i
} }
// Get simply returns result and can be chained on function which
// returns StringManipulation interface
func (i *input) Get() string { func (i *input) Get() string {
return getInput(*i) 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 { func (i *input) KebabCase(rule ...string) StringManipulation {
input := getInput(*i) input := getInput(*i)
wordArray := caseHelper(input, false, rule ...) wordArray := caseHelper(input, false, rule...)
i.Result = strings.Join(wordArray, "-") i.Result = strings.Join(wordArray, "-")
return i 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 { func (i *input) LcFirst() string {
input := getInput(*i) input := getInput(*i)
for i, v := range input { for i, v := range input {
@ -141,6 +150,7 @@ func (i *input) LcFirst() string {
return input return input
} }
// Lines returns slice of strings by removing white space characters
func (i *input) Lines() []string { func (i *input) Lines() []string {
input := getInput(*i) input := getInput(*i)
matchWord := regexp.MustCompile(`[\s]*[\W]\pN`) matchWord := regexp.MustCompile(`[\s]*[\W]\pN`)
@ -148,6 +158,10 @@ func (i *input) Lines() []string {
return strings.Fields(strings.TrimSpace(result)) 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 { func (i *input) Pad(length int, with, padType string) string {
input := getInput(*i) input := getInput(*i)
inputLength := len(input) 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 { func (i *input) RemoveSpecialCharacter() string {
input := getInput(*i) input := getInput(*i)
var result strings.Builder var result strings.Builder
@ -188,15 +204,26 @@ func (i *input) RemoveSpecialCharacter() string {
return result.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 { func (i *input) ReplaceFirst(search, replace string) string {
input := getInput(*i) input := getInput(*i)
return replaceStr(input, search, replace, First) 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 { func (i *input) ReplaceLast(search, replace string) string {
input := getInput(*i) input := getInput(*i)
return replaceStr(input, search, replace, Last) 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 { func (i *input) Reverse() string {
input := getInput(*i) input := getInput(*i)
r := []rune(input) r := []rune(input)
@ -206,6 +233,8 @@ func (i *input) Reverse() string {
return string(r) return string(r)
} }
// Shuffle shuffles the given string randomly
// it can be chained on function which return StringManipulation interface
func (i *input) Shuffle() string { func (i *input) Shuffle() string {
input := getInput(*i) input := getInput(*i)
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
@ -217,18 +246,28 @@ func (i *input) Shuffle() string {
return string(inRune) 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 { func (i *input) SnakeCase(rule ...string) StringManipulation {
input := getInput(*i) input := getInput(*i)
wordArray := caseHelper(input, false, rule ...) wordArray := caseHelper(input, false, rule...)
i.Result = strings.Join(wordArray, "_") i.Result = strings.Join(wordArray, "_")
return i 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 { func (i *input) Surround(with string) string {
input := getInput(*i) input := getInput(*i)
return with + input + with 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 { func (i *input) Tease(length int, indicator string) string {
input := getInput(*i) input := getInput(*i)
if input == "" || len(input) < length { if input == "" || len(input) < length {
@ -237,16 +276,22 @@ func (i *input) Tease(length int, indicator string) string {
return input[:length] + indicator 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) { func (i *input) ToLower() (result string) {
input := getInput(*i) input := getInput(*i)
return strings.ToLower(input) 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 { func (i *input) ToUpper() string {
input := getInput(*i) input := getInput(*i)
return strings.ToUpper(input) 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 { func (i *input) UcFirst() string {
input := getInput(*i) input := getInput(*i)
return ucfirst(input) return ucfirst(input)

160
stringy_test.go Normal file
View File

@ -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)
}
}