diff --git a/.gitignore b/.gitignore index 8dd807d..414e4d9 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ # Dependency directories vendor + +# IDE Files +.idea/ diff --git a/str/str.go b/str/str.go index fcdb32c..cc6fadc 100644 --- a/str/str.go +++ b/str/str.go @@ -1,6 +1,9 @@ package str -import "unicode" +import ( + "strings" + "unicode" +) func UpperFirst(input string) string { if len(input) == 0 { @@ -19,3 +22,232 @@ func InSlice(input interface{}, expects ...interface{}) bool { } return false } + +// Return the remainder of a string after the first occurrence of a given value. +func After(subject string, search string) string { + l := len(search) + if l == 0 { + return "" + } + + byteIndex := strings.Index(subject, search) + if byteIndex == -1 { + return subject + } + + byteSubject := []byte(subject) + byteSearch := []byte(search) + + result := string(byteSubject[byteIndex+ len(byteSearch):]) + return result +} + +// Return the remainder of a string after the last occurrence of a given value. +func AfterLast(subject string, search string) string { + l := len(search) + if l == 0 { + return "" + } + + byteIndex := strings.LastIndex(subject, search) + if byteIndex == -1 { + return subject + } + + byteSubject := []byte(subject) + byteSearch := []byte(search) + + result := string(byteSubject[byteIndex+ len(byteSearch):]) + return result +} + +// Get the portion of a string before the first occurrence of a given value. +func Before(subject string, search string) string { + l := len(search) + if l == 0 { + return subject + } + + byteIndex := strings.Index(subject, search) + if byteIndex == -1 { + return subject + } + + byteSubject := []byte(subject) + + result := string(byteSubject[:byteIndex]) + return result +} + +func BeforeLast(subject string, search string) string { + l := len(search) + if l == 0 { + return subject + } + + byteIndex := strings.LastIndex(subject, search) + if byteIndex == -1 { + return subject + } + + byteSubject := []byte(subject) + + result := string(byteSubject[:byteIndex]) + return result +} + +func Between(subject string, from string, to string) string { + if len(from) == 0 || len(to) == 0 { + return subject + } + return BeforeLast(After(subject, from), to) +} + +func Contains(haystack string, needle string) bool { + return strings.Index(haystack, needle) == -1 +} + +func ContainsFromSlice(haystack string, needle []string) bool { + // TODO + return false +} + +func ContainsAllFromSlice(haystack string, needle []string) bool { + // TODO + return false +} + +func EndsWith(haystack string, needle string) bool { + // TODO + return false +} + +func Finish(value string, cap string) string { + // TODO + return "" +} + +func Kebab(vale string) string { + // TODO + return "" +} + +func Length(value string) int { + // TODO + return 0 +} + +func LimitCharacters(value string, limit int, end string) string{ + // TODO + return "" +} + +func LimitWords(value string, limit int, end string) string{ + // TODO + return "" +} + +func Lower(value string) string { + // TODO + return "" +} + +func PadBoth(value string, length int, pad string) string { + // TODO + return "" +} + +func PadLeft(value string, length int, pad string) string { + // TODO + return "" +} + +func PadRight(value string, length int, pad string) string { + // TODO + return "" +} + +func ReplaceArray(search string, replace []string, subject string) string { + // TODO + return "" +} + +func ReplaceFirst(search string, replace string, subject string) string { + // TODO + return "" +} + +func ReplaceLast(search string, replace string, subject string) string { + // TODO + return "" +} + +func Start(value string, prefix string) string { + // TODO + return "" +} + +func Slug(value string) string { + // TODO + return "" +} +func SlugWithDelimiter(value string, delimiter string) string { + // TODO + return "" +} + +func Snake(value string) string { + // TODO + return "" +} + +func SnakeWithDelimiter(value string, delimiter string) string { + // TODO + return "" +} + +func StartsWith(haystack string, needle string) string { + // TODO + return "" +} + +func Studly(value string) string { + // TODO + return "" +} + +func UcFirst(value string) string { + // TODO + return "" +} + +func Upper(value string) string { + // TODO + return "" +} + +func Title(value string) string { + // TODO + return "" +} + + + + + + + + + + + + + + + + + + + + + diff --git a/str/str_test.go b/str/str_test.go new file mode 100644 index 0000000..c4226e3 --- /dev/null +++ b/str/str_test.go @@ -0,0 +1,74 @@ +package str + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestAfter(t *testing.T) { + // TODO: What if nothing is found? + assert.Equal(t, "", After("", "")) + assert.Equal(t, "", After("hannah", "")) + assert.Equal(t, "", After("", "han")) + assert.Equal(t, "nah", After("hannah", "han")) + assert.Equal(t, "nah", After("hannah", "n")) + assert.Equal(t, "nah", After("eee hannah", "han")) + assert.Equal(t, "nah", After("ééé hannah", "han")) + assert.Equal(t, "hannah", After("hannah", "xxxx")) + assert.Equal(t, "nah", After("han0nah", "0")) + assert.Equal(t, "nah", After("han2nah", "2")) +} + +func TestAfterLast(t *testing.T) { + // TODO: What if nothing is found? + assert.Equal(t, "", After("", "")) + assert.Equal(t, "", After("hannah", "")) + assert.Equal(t, "", After("", "han")) + assert.Equal(t,"tte", AfterLast("yvette", "yve")) + assert.Equal(t,"e", AfterLast("yvette", "t")) + assert.Equal(t,"e", AfterLast("ééé yvette", "t")) + assert.Equal(t,"", AfterLast("yvette", "tte")) + assert.Equal(t,"yvette", AfterLast("yvette", "xxxx")) + assert.Equal(t,"te", AfterLast("yv0et0te", "0")) + assert.Equal(t,"te", AfterLast("yv0et0te", "0")) + assert.Equal(t,"te", AfterLast("yv2et2te", "2")) + assert.Equal(t,"foo", AfterLast("----foo", "---")) +} + +func TestBefore(t *testing.T) { + assert.Equal(t, "han", Before("hannah", "nah")) + assert.Equal(t, "ha", Before("hannah", "n")) + assert.Equal(t, "ééé ", Before("ééé hannah", "han")) + assert.Equal(t, "hannah", Before("hannah", "xxxx")) + assert.Equal(t, "han", Before("han0nah", "0")) + assert.Equal(t, "han", Before("han0nah", "0")) + assert.Equal(t, "han", Before("han2nah", "2")) +} + +func TestBeforeLast(t *testing.T) { + assert.Equal(t,"yve", BeforeLast("yvette", "tte")) + assert.Equal(t,"yvet", BeforeLast("yvette", "t")) + assert.Equal(t,"ééé ", BeforeLast("ééé yvette", "yve")) + assert.Equal(t,"", BeforeLast("yvette", "yve")) + assert.Equal(t,"yvette", BeforeLast("yvette", "xxxx")) + assert.Equal(t,"yvette", BeforeLast("yvette", "")) + assert.Equal(t,"yv0et", BeforeLast("yv0et0te", "0")) + assert.Equal(t,"yv0et", BeforeLast("yv0et0te", "0")) + assert.Equal(t,"yv2et", BeforeLast("yv2et2te", "2")) +} + +func TestBetween(t *testing.T) { + assert.Equal(t,"abc", Between("abc", "", "c")) + assert.Equal(t,"abc", Between("abc", "a", "")) + assert.Equal(t,"abc", Between("abc", "", "")) + assert.Equal(t,"b", Between("abc", "a", "c")) + assert.Equal(t,"b", Between("dddabc", "a", "c")) + assert.Equal(t,"b", Between("abcddd", "a", "c")) + assert.Equal(t,"b", Between("dddabcddd", "a", "c")) + assert.Equal(t,"nn", Between("hannah", "ha", "ah")) + assert.Equal(t,"a]ab[b", Between("[a]ab[b]", "[", "]")) + assert.Equal(t,"foo", Between("foofoobar", "foo", "bar")) + assert.Equal(t,"bar", Between("foobarbar", "foo", "bar")) +} + +