2020-04-05 16:10:08 +03:00
|
|
|
package stringy
|
2020-04-05 12:11:56 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
2020-04-05 14:02:47 +03:00
|
|
|
func caseHelper(input string, isCamel bool, rule ...string) []string {
|
2020-04-05 12:11:56 +03:00
|
|
|
if !isCamel {
|
2020-04-05 14:02:47 +03:00
|
|
|
re := regexp.MustCompile(SelectCapital)
|
|
|
|
input = re.ReplaceAllString(input, ReplaceCapital)
|
2020-04-05 12:11:56 +03:00
|
|
|
}
|
|
|
|
input = strings.Join(strings.Fields(strings.TrimSpace(input)), " ")
|
2020-04-22 14:51:08 +03:00
|
|
|
if len(rule) > 0 && len(rule)%2 != 0 {
|
2020-04-05 14:02:47 +03:00
|
|
|
panic(errors.New(OddError))
|
2020-04-05 12:11:56 +03:00
|
|
|
}
|
|
|
|
rule = append(rule, ".", " ", "_", " ", "-", " ")
|
|
|
|
|
2020-04-05 14:02:47 +03:00
|
|
|
replacer := strings.NewReplacer(rule...)
|
2020-04-05 12:11:56 +03:00
|
|
|
input = replacer.Replace(input)
|
|
|
|
words := strings.Fields(input)
|
|
|
|
return words
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(slice []string, item string) bool {
|
|
|
|
set := make(map[string]struct{}, len(slice))
|
|
|
|
for _, s := range slice {
|
|
|
|
set[s] = struct{}{}
|
|
|
|
}
|
|
|
|
_, ok := set[item]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInput(i input) (input string) {
|
|
|
|
if i.Result != "" {
|
|
|
|
input = i.Result
|
|
|
|
} else {
|
|
|
|
input = i.Input
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func replaceStr(input, search, replace, types string) string {
|
|
|
|
lcInput := strings.ToLower(input)
|
|
|
|
lcSearch := strings.ToLower(search)
|
|
|
|
if input == "" || !strings.Contains(lcInput, lcSearch) {
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
var start int
|
|
|
|
if types == "last" {
|
|
|
|
start = strings.LastIndex(lcInput, lcSearch)
|
|
|
|
} else {
|
|
|
|
start = strings.Index(lcInput, lcSearch)
|
|
|
|
}
|
|
|
|
end := start + len(search)
|
|
|
|
return input[:start] + replace + input[end:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func ucfirst(val string) string {
|
|
|
|
for i, v := range val {
|
|
|
|
return string(unicode.ToUpper(v)) + val[i+1:]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|