From 174dd408c5c09c99e1b5dca2ceaca0866681688a Mon Sep 17 00:00:00 2001 From: wulfheart Date: Sat, 20 Feb 2021 11:28:09 +0100 Subject: [PATCH 01/22] Outline --- .idea/.gitignore | 8 +++ .idea/modules.xml | 8 +++ .idea/support.iml | 8 +++ .idea/vcs.xml | 6 ++ str/str.go | 170 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 200 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/modules.xml create mode 100644 .idea/support.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..15c91df --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/../../../../../../:\code\confetti\dev\support\.idea/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8efe1e3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/support.iml b/.idea/support.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/support.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/str/str.go b/str/str.go index fcdb32c..0a04634 100644 --- a/str/str.go +++ b/str/str.go @@ -19,3 +19,173 @@ func InSlice(input interface{}, expects ...interface{}) bool { } return false } + +func After(subject string, search string) string { + // TODO + return "" +} + +func AfterLast(subject string, search string) string { + // TODO + return "" +} + +func Before(subject string, search string) string { + // TODO + return "" +} + +func BeforeLast(subject string, search string) string { + // TODO + return "" +} + +func Between(subject string, from string, to string) string { + // TODO + return "" +} + +func Contains(haystack string, needle string) bool { + // TODO + return false +} + +func ContainsInSlice(haystack string, needle []string) bool { + // TODO + return false +} + +func ContainsAllInSlice(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 Upper(value string) string { + // TODO + return "" +} + +func Title(value string) string { + // TODO + return "" +} + + + + + + + + + + + + + + + + + + + + + From cf2a4ce28009adf49a6709381538748405432c6e Mon Sep 17 00:00:00 2001 From: wulfheart Date: Sat, 20 Feb 2021 12:02:23 +0100 Subject: [PATCH 02/22] After Issues --- .gitignore | 3 +++ str/str.go | 25 +++++++++++++++++++++++-- str/str_test.go | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 str/str_test.go diff --git a/.gitignore b/.gitignore index 8dd807d..5105a17 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 0a04634..70df62d 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 { @@ -20,9 +23,22 @@ 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 { // TODO - return "" + l := len(search) + if l == 0 { + return subject + } + + strs := strings.Index(subject, search) + if strs == -1 { + return subject + } + runes := []rune(subject) + + result := string(runes[strs + l:]) + return result } func AfterLast(subject string, search string) string { @@ -159,6 +175,11 @@ func Studly(value string) string { return "" } +func UcFirst(value string) string { + // TODO + return "" +} + func Upper(value string) string { // TODO return "" diff --git a/str/str_test.go b/str/str_test.go new file mode 100644 index 0000000..c7b75c0 --- /dev/null +++ b/str/str_test.go @@ -0,0 +1,18 @@ +package str + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestAfter(t *testing.T) { + + 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, "hannah", After("hannah", "")) + assert.Equal(t, "nah", After("han0nah", "0")) + assert.Equal(t, "nah", After("han2nah", "2")) +} From 813e797733322b82fab51f9d33c25469abdffce5 Mon Sep 17 00:00:00 2001 From: wulfheart Date: Sat, 20 Feb 2021 12:52:09 +0100 Subject: [PATCH 03/22] StringAfter Passing --- str/str.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/str/str.go b/str/str.go index 70df62d..a980761 100644 --- a/str/str.go +++ b/str/str.go @@ -31,13 +31,15 @@ func After(subject string, search string) string { return subject } - strs := strings.Index(subject, search) - if strs == -1 { + + byteIndex := strings.Index(subject, search) + if byteIndex == -1 { return subject } - runes := []rune(subject) + byteSubject := []byte(subject) + byteSearch := []byte(search) - result := string(runes[strs + l:]) + result := string(byteSubject[byteIndex+ len(byteSearch):]) return result } From c9ffa56aa2bb687286e9146297a9b84d9be7053b Mon Sep 17 00:00:00 2001 From: wulfheart Date: Sat, 20 Feb 2021 12:56:07 +0100 Subject: [PATCH 04/22] AfterLast Working --- str/str.go | 21 +++++++++++++++++---- str/str_test.go | 13 +++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/str/str.go b/str/str.go index a980761..fe27ef0 100644 --- a/str/str.go +++ b/str/str.go @@ -25,17 +25,16 @@ func InSlice(input interface{}, expects ...interface{}) bool { // Return the remainder of a string after the first occurrence of a given value. func After(subject string, search string) string { - // TODO l := len(search) if l == 0 { return subject } - byteIndex := strings.Index(subject, search) if byteIndex == -1 { return subject } + byteSubject := []byte(subject) byteSearch := []byte(search) @@ -43,9 +42,23 @@ func After(subject string, search string) string { return result } +// Return the remainder of a string after the last occurrence of a given value. func AfterLast(subject string, search string) string { - // TODO - return "" + l := len(search) + if l == 0 { + return subject + } + + byteIndex := strings.LastIndex(subject, search) + if byteIndex == -1 { + return subject + } + + byteSubject := []byte(subject) + byteSearch := []byte(search) + + result := string(byteSubject[byteIndex+ len(byteSearch):]) + return result } func Before(subject string, search string) string { diff --git a/str/str_test.go b/str/str_test.go index c7b75c0..7b23ed8 100644 --- a/str/str_test.go +++ b/str/str_test.go @@ -16,3 +16,16 @@ func TestAfter(t *testing.T) { assert.Equal(t, "nah", After("han0nah", "0")) assert.Equal(t, "nah", After("han2nah", "2")) } + +func TestAfterLast(t *testing.T) { + 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,"yvette", AfterLast("yvette", "")) + 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", "---")) +} From 682fdb173809af29db71824716bd54b31146e48f Mon Sep 17 00:00:00 2001 From: wulfheart Date: Sat, 20 Feb 2021 13:04:43 +0100 Subject: [PATCH 05/22] Between added --- str/str.go | 39 +++++++++++++++++++++++++++++++++------ str/str_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/str/str.go b/str/str.go index fe27ef0..d65b321 100644 --- a/str/str.go +++ b/str/str.go @@ -61,19 +61,46 @@ func AfterLast(subject string, search string) string { return result } +// Get the portion of a string before the first occurrence of a given value. func Before(subject string, search string) string { - // TODO - return "" + 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 { - // TODO - return "" + 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 { - // TODO - return "" + if len(from) == 0 || len(to) == 0 { + return subject + } + return BeforeLast(After(subject, from), to) } func Contains(haystack string, needle string) bool { diff --git a/str/str_test.go b/str/str_test.go index 7b23ed8..8ae8f2c 100644 --- a/str/str_test.go +++ b/str/str_test.go @@ -29,3 +29,40 @@ func TestAfterLast(t *testing.T) { 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, "hannah", Before("hannah", "")) + 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")) +} From b8c8bd7bd6b6234c3dae1e3135081950443d40ae Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 20 Feb 2021 13:05:19 +0100 Subject: [PATCH 06/22] Delete vcs.xml --- .idea/vcs.xml | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 6249fd2f7335cb45ba4bb713786804f1c75e15f0 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 20 Feb 2021 13:05:33 +0100 Subject: [PATCH 07/22] Delete .idea directory --- .idea/.gitignore | 8 -------- .idea/modules.xml | 8 -------- .idea/support.iml | 8 -------- 3 files changed, 24 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/modules.xml delete mode 100644 .idea/support.iml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 15c91df..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/../../../../../../:\code\confetti\dev\support\.idea/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 8efe1e3..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/support.iml b/.idea/support.iml deleted file mode 100644 index c956989..0000000 --- a/.idea/support.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file From 5d7008707722afcf8c2a9cb8c288774634878cc0 Mon Sep 17 00:00:00 2001 From: wulfheart Date: Sat, 20 Feb 2021 13:07:42 +0100 Subject: [PATCH 08/22] Gitignore update --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5105a17..414e4d9 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,4 @@ vendor # IDE Files -.idea +.idea/ From 01573e5656e67b16fd310572cca4e61efdb12968 Mon Sep 17 00:00:00 2001 From: wulfheart Date: Sat, 13 Mar 2021 21:01:01 +0100 Subject: [PATCH 09/22] Still some things to do --- str/str.go | 11 +++++------ str/str_test.go | 14 ++++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/str/str.go b/str/str.go index d65b321..cc6fadc 100644 --- a/str/str.go +++ b/str/str.go @@ -27,7 +27,7 @@ func InSlice(input interface{}, expects ...interface{}) bool { func After(subject string, search string) string { l := len(search) if l == 0 { - return subject + return "" } byteIndex := strings.Index(subject, search) @@ -46,7 +46,7 @@ func After(subject string, search string) string { func AfterLast(subject string, search string) string { l := len(search) if l == 0 { - return subject + return "" } byteIndex := strings.LastIndex(subject, search) @@ -104,16 +104,15 @@ func Between(subject string, from string, to string) string { } func Contains(haystack string, needle string) bool { - // TODO - return false + return strings.Index(haystack, needle) == -1 } -func ContainsInSlice(haystack string, needle []string) bool { +func ContainsFromSlice(haystack string, needle []string) bool { // TODO return false } -func ContainsAllInSlice(haystack string, needle []string) bool { +func ContainsAllFromSlice(haystack string, needle []string) bool { // TODO return false } diff --git a/str/str_test.go b/str/str_test.go index 8ae8f2c..c4226e3 100644 --- a/str/str_test.go +++ b/str/str_test.go @@ -6,24 +6,29 @@ import ( ) 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, "hannah", After("hannah", "")) 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,"yvette", AfterLast("yvette", "")) assert.Equal(t,"te", AfterLast("yv0et0te", "0")) assert.Equal(t,"te", AfterLast("yv0et0te", "0")) assert.Equal(t,"te", AfterLast("yv2et2te", "2")) @@ -35,7 +40,6 @@ func TestBefore(t *testing.T) { assert.Equal(t, "ha", Before("hannah", "n")) assert.Equal(t, "ééé ", Before("ééé hannah", "han")) assert.Equal(t, "hannah", Before("hannah", "xxxx")) - assert.Equal(t, "hannah", Before("hannah", "")) assert.Equal(t, "han", Before("han0nah", "0")) assert.Equal(t, "han", Before("han0nah", "0")) assert.Equal(t, "han", Before("han2nah", "2")) @@ -66,3 +70,5 @@ func TestBetween(t *testing.T) { assert.Equal(t,"foo", Between("foofoobar", "foo", "bar")) assert.Equal(t,"bar", Between("foobarbar", "foo", "bar")) } + + From 23dbbb93cc26685f4955f7f7392655a2ff381da4 Mon Sep 17 00:00:00 2001 From: Reindert Vetter Date: Sun, 11 Apr 2021 23:31:22 +0200 Subject: [PATCH 10/22] #5 Make it clear for test coverage which functions are not yet ready. --- str/str.go | 247 +++++++++++++++++++++++++++-------------------------- 1 file changed, 124 insertions(+), 123 deletions(-) diff --git a/str/str.go b/str/str.go index cc6fadc..aa121ad 100644 --- a/str/str.go +++ b/str/str.go @@ -104,132 +104,133 @@ func Between(subject string, from string, to string) string { } func Contains(haystack string, needle string) bool { + // TODO create tests 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 "" -} +// 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 "" +// } From 3e4410a08914f4572a140d79c6969f92b17dcdf1 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 01:04:40 +0300 Subject: [PATCH 11/22] move tests to test/str_test.go --- str/str_test.go | 74 ------------------------------------------------ test/str_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 74 deletions(-) delete mode 100644 str/str_test.go diff --git a/str/str_test.go b/str/str_test.go deleted file mode 100644 index c4226e3..0000000 --- a/str/str_test.go +++ /dev/null @@ -1,74 +0,0 @@ -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")) -} - - diff --git a/test/str_test.go b/test/str_test.go index c5719c3..033aafd 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -35,3 +35,69 @@ func Test_in_slice_with_multiple_one_matched_parameters(t *testing.T) { func Test_in_slice_with_integer(t *testing.T) { require.True(t, str.InSlice(1, 0, 1)) } + +func Test_After(t *testing.T) { + // TODO: What if nothing is found? + require.Equal(t, "", str.After("", "")) + require.Equal(t, "", str.After("hannah", "")) + require.Equal(t, "", str.After("", "han")) + require.Equal(t, "nah", str.After("hannah", "han")) + require.Equal(t, "nah", str.After("hannah", "n")) + require.Equal(t, "nah", str.After("eee hannah", "han")) + require.Equal(t, "nah", str.After("ééé hannah", "han")) + require.Equal(t, "hannah", str.After("hannah", "xxxx")) + require.Equal(t, "nah", str.After("han0nah", "0")) + require.Equal(t, "nah", str.After("han2nah", "2")) +} + +func Test_AfterLast(t *testing.T) { + // TODO: What if nothing is found? + require.Equal(t, "", str.After("", "")) + require.Equal(t, "", str.After("hannah", "")) + require.Equal(t, "", str.After("", "han")) + require.Equal(t,"tte", str.AfterLast("yvette", "yve")) + require.Equal(t,"e", str.AfterLast("yvette", "t")) + require.Equal(t,"e", str.AfterLast("ééé yvette", "t")) + require.Equal(t,"", str.AfterLast("yvette", "tte")) + require.Equal(t,"yvette", str.AfterLast("yvette", "xxxx")) + require.Equal(t,"te", str.AfterLast("yv0et0te", "0")) + require.Equal(t,"te", str.AfterLast("yv0et0te", "0")) + require.Equal(t,"te", str.AfterLast("yv2et2te", "2")) + require.Equal(t,"foo", str.AfterLast("----foo", "---")) +} + +func Test_Before(t *testing.T) { + require.Equal(t, "han", str.Before("hannah", "nah")) + require.Equal(t, "ha", str.Before("hannah", "n")) + require.Equal(t, "ééé ", str.Before("ééé hannah", "han")) + require.Equal(t, "hannah", str.Before("hannah", "xxxx")) + require.Equal(t, "han", str.Before("han0nah", "0")) + require.Equal(t, "han", str.Before("han0nah", "0")) + require.Equal(t, "han", str.Before("han2nah", "2")) +} + +func Test_BeforeLast(t *testing.T) { + require.Equal(t,"yve", str.BeforeLast("yvette", "tte")) + require.Equal(t,"yvet", str.BeforeLast("yvette", "t")) + require.Equal(t,"ééé ", str.BeforeLast("ééé yvette", "yve")) + require.Equal(t,"", str.BeforeLast("yvette", "yve")) + require.Equal(t,"yvette", str.BeforeLast("yvette", "xxxx")) + require.Equal(t,"yvette", str.BeforeLast("yvette", "")) + require.Equal(t,"yv0et", str.BeforeLast("yv0et0te", "0")) + require.Equal(t,"yv0et", str.BeforeLast("yv0et0te", "0")) + require.Equal(t,"yv2et", str.BeforeLast("yv2et2te", "2")) +} + +func Test_Between(t *testing.T) { + require.Equal(t,"abc", str.Between("abc", "", "c")) + require.Equal(t,"abc", str.Between("abc", "a", "")) + require.Equal(t,"abc", str.Between("abc", "", "")) + require.Equal(t,"b", str.Between("abc", "a", "c")) + require.Equal(t,"b", str.Between("dddabc", "a", "c")) + require.Equal(t,"b", str.Between("abcddd", "a", "c")) + require.Equal(t,"b", str.Between("dddabcddd", "a", "c")) + require.Equal(t,"nn", str.Between("hannah", "ha", "ah")) + require.Equal(t,"a]ab[b", str.Between("[a]ab[b]", "[", "]")) + require.Equal(t,"foo", str.Between("foofoobar", "foo", "bar")) + require.Equal(t,"bar", str.Between("foobarbar", "foo", "bar")) +} From efb921946f9e4e9694c165a6c23b71ab2127c8a9 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 01:56:41 +0300 Subject: [PATCH 12/22] reduced code & fix tests --- str/str.go | 56 +++++++++++++++--------------------------------- test/str_test.go | 4 ++-- 2 files changed, 19 insertions(+), 41 deletions(-) diff --git a/str/str.go b/str/str.go index aa121ad..1c1766b 100644 --- a/str/str.go +++ b/str/str.go @@ -25,81 +25,59 @@ func InSlice(input interface{}, expects ...interface{}) bool { // 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 { + if len(search) == 0 { return subject } - - byteSubject := []byte(subject) - byteSearch := []byte(search) - - result := string(byteSubject[byteIndex+ len(byteSearch):]) - return result + results := strings.SplitN(subject, search, 2) + return results[len(results)-1] } // 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 "" + if len(search) == 0 { + return subject } + position := strings.LastIndex(subject, search) - byteIndex := strings.LastIndex(subject, search) - if byteIndex == -1 { + if position == -1 { return subject } - byteSubject := []byte(subject) - byteSearch := []byte(search) - - result := string(byteSubject[byteIndex+ len(byteSearch):]) - return result + return subject[position+len(search):] } // 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 { + if len(search) == 0 { return subject } + position := strings.Index(subject, search) - byteIndex := strings.Index(subject, search) - if byteIndex == -1 { + if position == -1 { return subject } - byteSubject := []byte(subject) - - result := string(byteSubject[:byteIndex]) - return result + return subject[:position] } func BeforeLast(subject string, search string) string { - l := len(search) - if l == 0 { + if len(search) == 0 { return subject } + position := strings.LastIndex(subject, search) - byteIndex := strings.LastIndex(subject, search) - if byteIndex == -1 { + if position == -1 { return subject } - byteSubject := []byte(subject) - - result := string(byteSubject[:byteIndex]) - return result + return subject[:position] } func Between(subject string, from string, to string) string { if len(from) == 0 || len(to) == 0 { return subject } + return BeforeLast(After(subject, from), to) } diff --git a/test/str_test.go b/test/str_test.go index 033aafd..2c78112 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -39,7 +39,7 @@ func Test_in_slice_with_integer(t *testing.T) { func Test_After(t *testing.T) { // TODO: What if nothing is found? require.Equal(t, "", str.After("", "")) - require.Equal(t, "", str.After("hannah", "")) + require.Equal(t, "hannah", str.After("hannah", "")) require.Equal(t, "", str.After("", "han")) require.Equal(t, "nah", str.After("hannah", "han")) require.Equal(t, "nah", str.After("hannah", "n")) @@ -53,7 +53,7 @@ func Test_After(t *testing.T) { func Test_AfterLast(t *testing.T) { // TODO: What if nothing is found? require.Equal(t, "", str.After("", "")) - require.Equal(t, "", str.After("hannah", "")) + require.Equal(t, "hannah", str.After("hannah", "")) require.Equal(t, "", str.After("", "han")) require.Equal(t,"tte", str.AfterLast("yvette", "yve")) require.Equal(t,"e", str.AfterLast("yvette", "t")) From 56edce45914c8198d0f622832a509fb5f3d62e6b Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 01:58:24 +0300 Subject: [PATCH 13/22] fix tests --- test/str_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/str_test.go b/test/str_test.go index 2c78112..d261293 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -39,8 +39,8 @@ func Test_in_slice_with_integer(t *testing.T) { func Test_After(t *testing.T) { // TODO: What if nothing is found? require.Equal(t, "", str.After("", "")) - require.Equal(t, "hannah", str.After("hannah", "")) require.Equal(t, "", str.After("", "han")) + require.Equal(t, "hannah", str.After("hannah", "")) require.Equal(t, "nah", str.After("hannah", "han")) require.Equal(t, "nah", str.After("hannah", "n")) require.Equal(t, "nah", str.After("eee hannah", "han")) @@ -53,8 +53,8 @@ func Test_After(t *testing.T) { func Test_AfterLast(t *testing.T) { // TODO: What if nothing is found? require.Equal(t, "", str.After("", "")) - require.Equal(t, "hannah", str.After("hannah", "")) require.Equal(t, "", str.After("", "han")) + require.Equal(t, "hannah", str.After("hannah", "")) require.Equal(t,"tte", str.AfterLast("yvette", "yve")) require.Equal(t,"e", str.AfterLast("yvette", "t")) require.Equal(t,"e", str.AfterLast("ééé yvette", "t")) From 8e09b06399bdbd7e6a4edc33847f56ef1d3ce266 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 02:17:32 +0300 Subject: [PATCH 14/22] fix Contains helper & added tests for Contains --- str/str.go | 7 +++-- test/str_test.go | 67 +++++++++++++++++++++++++++--------------------- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/str/str.go b/str/str.go index 1c1766b..0603c99 100644 --- a/str/str.go +++ b/str/str.go @@ -82,8 +82,11 @@ func Between(subject string, from string, to string) string { } func Contains(haystack string, needle string) bool { - // TODO create tests - return strings.Index(haystack, needle) == -1 + if len(needle) == 0 { + return false + } + + return strings.Contains(haystack, needle) } // func ContainsFromSlice(haystack string, needle []string) bool { diff --git a/test/str_test.go b/test/str_test.go index d261293..2566a6c 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -55,15 +55,15 @@ func Test_AfterLast(t *testing.T) { require.Equal(t, "", str.After("", "")) require.Equal(t, "", str.After("", "han")) require.Equal(t, "hannah", str.After("hannah", "")) - require.Equal(t,"tte", str.AfterLast("yvette", "yve")) - require.Equal(t,"e", str.AfterLast("yvette", "t")) - require.Equal(t,"e", str.AfterLast("ééé yvette", "t")) - require.Equal(t,"", str.AfterLast("yvette", "tte")) - require.Equal(t,"yvette", str.AfterLast("yvette", "xxxx")) - require.Equal(t,"te", str.AfterLast("yv0et0te", "0")) - require.Equal(t,"te", str.AfterLast("yv0et0te", "0")) - require.Equal(t,"te", str.AfterLast("yv2et2te", "2")) - require.Equal(t,"foo", str.AfterLast("----foo", "---")) + require.Equal(t, "tte", str.AfterLast("yvette", "yve")) + require.Equal(t, "e", str.AfterLast("yvette", "t")) + require.Equal(t, "e", str.AfterLast("ééé yvette", "t")) + require.Equal(t, "", str.AfterLast("yvette", "tte")) + require.Equal(t, "yvette", str.AfterLast("yvette", "xxxx")) + require.Equal(t, "te", str.AfterLast("yv0et0te", "0")) + require.Equal(t, "te", str.AfterLast("yv0et0te", "0")) + require.Equal(t, "te", str.AfterLast("yv2et2te", "2")) + require.Equal(t, "foo", str.AfterLast("----foo", "---")) } func Test_Before(t *testing.T) { @@ -77,27 +77,36 @@ func Test_Before(t *testing.T) { } func Test_BeforeLast(t *testing.T) { - require.Equal(t,"yve", str.BeforeLast("yvette", "tte")) - require.Equal(t,"yvet", str.BeforeLast("yvette", "t")) - require.Equal(t,"ééé ", str.BeforeLast("ééé yvette", "yve")) - require.Equal(t,"", str.BeforeLast("yvette", "yve")) - require.Equal(t,"yvette", str.BeforeLast("yvette", "xxxx")) - require.Equal(t,"yvette", str.BeforeLast("yvette", "")) - require.Equal(t,"yv0et", str.BeforeLast("yv0et0te", "0")) - require.Equal(t,"yv0et", str.BeforeLast("yv0et0te", "0")) - require.Equal(t,"yv2et", str.BeforeLast("yv2et2te", "2")) + require.Equal(t, "yve", str.BeforeLast("yvette", "tte")) + require.Equal(t, "yvet", str.BeforeLast("yvette", "t")) + require.Equal(t, "ééé ", str.BeforeLast("ééé yvette", "yve")) + require.Equal(t, "", str.BeforeLast("yvette", "yve")) + require.Equal(t, "yvette", str.BeforeLast("yvette", "xxxx")) + require.Equal(t, "yvette", str.BeforeLast("yvette", "")) + require.Equal(t, "yv0et", str.BeforeLast("yv0et0te", "0")) + require.Equal(t, "yv0et", str.BeforeLast("yv0et0te", "0")) + require.Equal(t, "yv2et", str.BeforeLast("yv2et2te", "2")) } func Test_Between(t *testing.T) { - require.Equal(t,"abc", str.Between("abc", "", "c")) - require.Equal(t,"abc", str.Between("abc", "a", "")) - require.Equal(t,"abc", str.Between("abc", "", "")) - require.Equal(t,"b", str.Between("abc", "a", "c")) - require.Equal(t,"b", str.Between("dddabc", "a", "c")) - require.Equal(t,"b", str.Between("abcddd", "a", "c")) - require.Equal(t,"b", str.Between("dddabcddd", "a", "c")) - require.Equal(t,"nn", str.Between("hannah", "ha", "ah")) - require.Equal(t,"a]ab[b", str.Between("[a]ab[b]", "[", "]")) - require.Equal(t,"foo", str.Between("foofoobar", "foo", "bar")) - require.Equal(t,"bar", str.Between("foobarbar", "foo", "bar")) + require.Equal(t, "abc", str.Between("abc", "", "c")) + require.Equal(t, "abc", str.Between("abc", "a", "")) + require.Equal(t, "abc", str.Between("abc", "", "")) + require.Equal(t, "b", str.Between("abc", "a", "c")) + require.Equal(t, "b", str.Between("dddabc", "a", "c")) + require.Equal(t, "b", str.Between("abcddd", "a", "c")) + require.Equal(t, "b", str.Between("dddabcddd", "a", "c")) + require.Equal(t, "nn", str.Between("hannah", "ha", "ah")) + require.Equal(t, "a]ab[b", str.Between("[a]ab[b]", "[", "]")) + require.Equal(t, "foo", str.Between("foofoobar", "foo", "bar")) + require.Equal(t, "bar", str.Between("foobarbar", "foo", "bar")) +} + +func Test_Contains(t *testing.T) { + require.True(t, str.Contains("taylor", "ylo")) + require.True(t, str.Contains("taylor", "taylor")) + + require.False(t, str.Contains("taylor", "xxx")) + require.False(t, str.Contains("taylor", "")) + require.False(t, str.Contains("", "")) } From e96352302c87af3f367779f6a63d0959756bb106 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 02:29:03 +0300 Subject: [PATCH 15/22] added ContainsFromSlice helper method & tests --- str/str.go | 19 ++++++++++++++----- test/str_test.go | 9 ++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/str/str.go b/str/str.go index 0603c99..b7bade8 100644 --- a/str/str.go +++ b/str/str.go @@ -89,11 +89,20 @@ func Contains(haystack string, needle string) bool { return strings.Contains(haystack, needle) } -// func ContainsFromSlice(haystack string, needle []string) bool { -// // TODO -// return false -// } -// +func ContainsFromSlice(haystack string, needles []string) bool { + if len(needles) == 0 { + return false + } + + for _, needle := range needles { + if Contains(haystack, needle){ + return true + } + } + + return false +} + // func ContainsAllFromSlice(haystack string, needle []string) bool { // // TODO // return false diff --git a/test/str_test.go b/test/str_test.go index 2566a6c..cb4abde 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -105,8 +105,15 @@ func Test_Between(t *testing.T) { func Test_Contains(t *testing.T) { require.True(t, str.Contains("taylor", "ylo")) require.True(t, str.Contains("taylor", "taylor")) - require.False(t, str.Contains("taylor", "xxx")) require.False(t, str.Contains("taylor", "")) require.False(t, str.Contains("", "")) } + +func Test_ContainsFromSlice(t *testing.T) { + require.True(t, str.ContainsFromSlice("taylor", []string{"ylo"})) + require.True(t, str.ContainsFromSlice("taylor", []string{"xxx", "ylo"})) + require.False(t, str.ContainsFromSlice("taylor", []string{"xxx"})) + require.False(t, str.ContainsFromSlice("taylor", []string{})) + require.False(t, str.ContainsFromSlice("taylor", []string{""})) +} \ No newline at end of file From 4d9d08b762b500af3f06b3c8dd7205eb487c877d Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 02:37:21 +0300 Subject: [PATCH 16/22] added ContainsAllFromSlice helper method & tests --- str/str.go | 18 ++++++++++++++---- test/str_test.go | 12 +++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/str/str.go b/str/str.go index b7bade8..0e45e62 100644 --- a/str/str.go +++ b/str/str.go @@ -103,10 +103,20 @@ func ContainsFromSlice(haystack string, needles []string) bool { return false } -// func ContainsAllFromSlice(haystack string, needle []string) bool { -// // TODO -// return false -// } +func ContainsAllFromSlice(haystack string, needles []string) bool { + if len(needles) == 0 { + return false + } + + for _, needle := range needles { + if !Contains(haystack, needle){ + return false + } + } + + return true +} + // // func EndsWith(haystack string, needle string) bool { // // TODO diff --git a/test/str_test.go b/test/str_test.go index cb4abde..a71ae59 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -116,4 +116,14 @@ func Test_ContainsFromSlice(t *testing.T) { require.False(t, str.ContainsFromSlice("taylor", []string{"xxx"})) require.False(t, str.ContainsFromSlice("taylor", []string{})) require.False(t, str.ContainsFromSlice("taylor", []string{""})) -} \ No newline at end of file +} +func Test_ContainsAllFromSlice(t *testing.T) { + require.True(t, str.ContainsAllFromSlice("This is my name", []string{"This", "is"})) + require.True(t, str.ContainsAllFromSlice("This is my name", []string{"my", "ame"})) + require.True(t, str.ContainsAllFromSlice("taylor", []string{"tay", "ylo"})) + require.False(t, str.ContainsAllFromSlice("taylor", []string{"xxx", "ylo"})) + require.False(t, str.ContainsAllFromSlice("taylor", []string{"xxx", "tay"})) + require.False(t, str.ContainsAllFromSlice("This is my name", []string{"are", "name"})) + require.False(t, str.ContainsAllFromSlice("taylor", []string{})) + require.False(t, str.ContainsAllFromSlice("taylor", []string{"", ""})) +} From e6db0bf86b9962f746b57dd95f635ab926537cb8 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 02:58:28 +0300 Subject: [PATCH 17/22] added EndsWith & StartsWith helper method & tests --- str/str.go | 28 +++++++++++++++++----------- test/str_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/str/str.go b/str/str.go index 0e45e62..0f2291e 100644 --- a/str/str.go +++ b/str/str.go @@ -117,12 +117,23 @@ func ContainsAllFromSlice(haystack string, needles []string) bool { return true } -// -// func EndsWith(haystack string, needle string) bool { -// // TODO -// return false -// } -// + +func EndsWith(haystack string, needle string) bool { + if len(needle) == 0 { + return false + } + + return strings.HasSuffix(haystack, needle) +} + +func StartsWith(haystack string, needle string) bool { + if len(needle) == 0 { + return false + } + + return strings.HasPrefix(haystack, needle) +} + // func Finish(value string, cap string) string { // // TODO // return "" @@ -207,11 +218,6 @@ func ContainsAllFromSlice(haystack string, needles []string) bool { // return "" // } // -// func StartsWith(haystack string, needle string) string { -// // TODO -// return "" -// } -// // func Studly(value string) string { // // TODO // return "" diff --git a/test/str_test.go b/test/str_test.go index a71ae59..fa54811 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -117,6 +117,7 @@ func Test_ContainsFromSlice(t *testing.T) { require.False(t, str.ContainsFromSlice("taylor", []string{})) require.False(t, str.ContainsFromSlice("taylor", []string{""})) } + func Test_ContainsAllFromSlice(t *testing.T) { require.True(t, str.ContainsAllFromSlice("This is my name", []string{"This", "is"})) require.True(t, str.ContainsAllFromSlice("This is my name", []string{"my", "ame"})) @@ -127,3 +128,41 @@ func Test_ContainsAllFromSlice(t *testing.T) { require.False(t, str.ContainsAllFromSlice("taylor", []string{})) require.False(t, str.ContainsAllFromSlice("taylor", []string{"", ""})) } + +func Test_EndsWith(t *testing.T) { + require.True(t, str.EndsWith("This is my name", "name")) + require.True(t, str.EndsWith("This is my name", "e")) + require.True(t, str.EndsWith("jason", "on")) + require.True(t, str.EndsWith("7", "7")) + require.True(t, str.EndsWith("a7", "7")) + require.False(t, str.EndsWith("jason", "no")) + require.False(t, str.EndsWith("jason", "")) + require.False(t, str.EndsWith("", "")) + // Test for multibyte string support + require.True(t, str.EndsWith("Jönköping", "öping")) + require.True(t, str.EndsWith("Malmö", "mö")) + require.True(t, str.EndsWith("Malmö", "mö")) + require.False(t, str.EndsWith("Jönköping", "oping")) + require.False(t, str.EndsWith("Malmö", "mo")) + require.True(t, str.EndsWith("你好", "好")) + require.False(t, str.EndsWith("你好", "你")) + require.False(t, str.EndsWith("你好", "a")) +} + +func Test_StartsWith(t *testing.T) { + require.True(t, str.StartsWith("jason", "jas")) + require.True(t, str.StartsWith("jason", "jason")) + require.True(t, str.StartsWith("7a", "7")) + require.True(t, str.StartsWith("7", "7")) + require.False(t, str.StartsWith("jason", "J")) + require.False(t, str.StartsWith("jason", "")) + require.False(t, str.StartsWith("", "")) + // Test for multibyte string support + require.True(t, str.StartsWith("Jönköping", "Jö")) + require.True(t, str.StartsWith("Malmö", "Malmö")) + require.True(t, str.StartsWith("你好", "你")) + require.False(t, str.StartsWith("Jönköping", "Jonko")) + require.False(t, str.StartsWith("Malmö", "Malmo")) + require.False(t, str.StartsWith("你好", "好")) + require.False(t, str.StartsWith("你好", "a")) +} \ No newline at end of file From 0dd4e00d5276644f485d82379d5c84548df37295 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 03:42:39 +0300 Subject: [PATCH 18/22] adds newline at test/str_test.go --- test/str_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/str_test.go b/test/str_test.go index fa54811..fa91d2e 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -165,4 +165,4 @@ func Test_StartsWith(t *testing.T) { require.False(t, str.StartsWith("Malmö", "Malmo")) require.False(t, str.StartsWith("你好", "好")) require.False(t, str.StartsWith("你好", "a")) -} \ No newline at end of file +} From bd57e4954d7924f1a4268279d043dc81d5e4a4d5 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 03:56:30 +0300 Subject: [PATCH 19/22] added Lower & Upper helpers & tests --- str/str.go | 19 ++++++++----------- test/str_test.go | 10 ++++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/str/str.go b/str/str.go index 0f2291e..3cff021 100644 --- a/str/str.go +++ b/str/str.go @@ -117,7 +117,6 @@ func ContainsAllFromSlice(haystack string, needles []string) bool { return true } - func EndsWith(haystack string, needle string) bool { if len(needle) == 0 { return false @@ -134,6 +133,14 @@ func StartsWith(haystack string, needle string) bool { return strings.HasPrefix(haystack, needle) } +func Lower(value string) string { + return strings.ToLower(value) +} + +func Upper(value string) string { + return strings.ToUpper(value) +} + // func Finish(value string, cap string) string { // // TODO // return "" @@ -159,11 +166,6 @@ func StartsWith(haystack string, needle string) bool { // return "" // } // -// func Lower(value string) string { -// // TODO -// return "" -// } -// // func PadBoth(value string, length int, pad string) string { // // TODO // return "" @@ -228,11 +230,6 @@ func StartsWith(haystack string, needle string) bool { // return "" // } // -// func Upper(value string) string { -// // TODO -// return "" -// } -// // func Title(value string) string { // // TODO // return "" diff --git a/test/str_test.go b/test/str_test.go index fa91d2e..c93ab69 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -166,3 +166,13 @@ func Test_StartsWith(t *testing.T) { require.False(t, str.StartsWith("你好", "好")) require.False(t, str.StartsWith("你好", "a")) } + +func Test_Lower(t *testing.T) { + require.Equal(t, "foo bar baz", str.Lower("FOO BAR BAZ")) + require.Equal(t, "foo bar baz", str.Lower("fOo Bar bAz")) +} + +func Test_Upper(t *testing.T) { + require.Equal(t, "FOO BAR BAZ", str.Upper("foo bar baz")) + require.Equal(t, "FOO BAR BAZ", str.Upper("fOo Bar bAZ")) +} From c923384623389c36e1fc1aec5f89eab81db5ef20 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 04:28:34 +0300 Subject: [PATCH 20/22] added Finish helper & tests --- str/str.go | 47 +++++++++++++---------------------------------- test/str_test.go | 8 ++++++++ 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/str/str.go b/str/str.go index 3cff021..a68b148 100644 --- a/str/str.go +++ b/str/str.go @@ -1,6 +1,7 @@ package str import ( + "regexp" "strings" "unicode" ) @@ -141,10 +142,18 @@ func Upper(value string) string { return strings.ToUpper(value) } -// func Finish(value string, cap string) string { -// // TODO -// return "" -// } +func Finish(value string, cap string) string { + quoted := regexp.QuoteMeta(cap) + + re := regexp.MustCompile("(?:"+ quoted +")+$") + return re.ReplaceAllString(value, "") + cap +} + +// +//func Title(value string) string { +// // TODO +// return "" +//} // // func Kebab(vale string) string { // // TODO @@ -215,11 +224,6 @@ func Upper(value string) string { // return "" // } // -// func SnakeWithDelimiter(value string, delimiter string) string { -// // TODO -// return "" -// } -// // func Studly(value string) string { // // TODO // return "" @@ -230,28 +234,3 @@ func Upper(value string) string { // return "" // } // -// func Title(value string) string { -// // TODO -// return "" -// } - - - - - - - - - - - - - - - - - - - - - diff --git a/test/str_test.go b/test/str_test.go index c93ab69..e9a1d18 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -176,3 +176,11 @@ func Test_Upper(t *testing.T) { require.Equal(t, "FOO BAR BAZ", str.Upper("foo bar baz")) require.Equal(t, "FOO BAR BAZ", str.Upper("fOo Bar bAZ")) } + +func Test_Finish(t *testing.T) { + require.Equal(t, "abbc", str.Finish("ab", "bc")) + require.Equal(t, "abbc", str.Finish("abbcbc", "bc")) + require.Equal(t, "abcbbc", str.Finish("abcbbcbc", "bc")) + require.Equal(t, "this/string/", str.Finish("this/string", "/")) + require.Equal(t, "this/string/", str.Finish("this/string/", "/")) +} From 767badb36995e246215a69e519a023ac3d1fbbf5 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 04:33:13 +0300 Subject: [PATCH 21/22] added Start helper & tests --- str/str.go | 12 +++++++----- test/str_test.go | 6 ++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/str/str.go b/str/str.go index a68b148..10b6d8a 100644 --- a/str/str.go +++ b/str/str.go @@ -149,6 +149,13 @@ func Finish(value string, cap string) string { return re.ReplaceAllString(value, "") + cap } +func Start(value string, prefix string) string { + quoted := regexp.QuoteMeta(prefix) + + re := regexp.MustCompile("^(?:"+ quoted +")+") + return prefix + re.ReplaceAllString(value, "") +} + // //func Title(value string) string { // // TODO @@ -205,11 +212,6 @@ func Finish(value string, cap string) string { // return "" // } // -// func Start(value string, prefix string) string { -// // TODO -// return "" -// } -// // func Slug(value string) string { // // TODO // return "" diff --git a/test/str_test.go b/test/str_test.go index e9a1d18..102d515 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -184,3 +184,9 @@ func Test_Finish(t *testing.T) { require.Equal(t, "this/string/", str.Finish("this/string", "/")) require.Equal(t, "this/string/", str.Finish("this/string/", "/")) } + +func Test_Start(t *testing.T) { + require.Equal(t, "/test/string", str.Start("test/string", "/")) + require.Equal(t, "/test/string", str.Start("/test/string", "/")) + require.Equal(t, "/test/string", str.Start("//test/string", "/")) +} From ef97656ec2448e63ead42dff86d4b372af7fd7b1 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Mon, 12 Apr 2021 04:41:20 +0300 Subject: [PATCH 22/22] fix tests for Before st helper --- str/str.go | 1 + test/str_test.go | 1 + 2 files changed, 2 insertions(+) diff --git a/str/str.go b/str/str.go index 10b6d8a..856a124 100644 --- a/str/str.go +++ b/str/str.go @@ -216,6 +216,7 @@ func Start(value string, prefix string) string { // // TODO // return "" // } +// // func SlugWithDelimiter(value string, delimiter string) string { // // TODO // return "" diff --git a/test/str_test.go b/test/str_test.go index 102d515..3780880 100644 --- a/test/str_test.go +++ b/test/str_test.go @@ -67,6 +67,7 @@ func Test_AfterLast(t *testing.T) { } func Test_Before(t *testing.T) { + require.Equal(t, "hannah", str.Before("hannah", "")) require.Equal(t, "han", str.Before("hannah", "nah")) require.Equal(t, "ha", str.Before("hannah", "n")) require.Equal(t, "ééé ", str.Before("ééé hannah", "han"))