From a6cc809c2e81d7db9c3f1141d03b26d7a130fdef Mon Sep 17 00:00:00 2001 From: Pouyan Heyratpour Date: Sat, 21 Dec 2024 16:14:46 +0330 Subject: [PATCH] feat(tricks): strings is empty Signed-off-by: Pouyan Heyratpour --- tricks/string.go | 6 ++++++ tricks/string_test.go | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/tricks/string.go b/tricks/string.go index b049bba..a42930f 100644 --- a/tricks/string.go +++ b/tricks/string.go @@ -1,5 +1,11 @@ package tricks +import "strings" + func StringToRunes(src string) []rune { return []rune(src) } + +func IsEmptyString(s string) bool { + return len(strings.TrimSpace(s)) == 0 +} diff --git a/tricks/string_test.go b/tricks/string_test.go index 5b9c0f4..25c90ff 100644 --- a/tricks/string_test.go +++ b/tricks/string_test.go @@ -14,3 +14,25 @@ func TestStringToRunes(t *testing.T) { assert.Equal(t, []rune{'a', 'b', ' ', 'c', 'd'}, tricks.StringToRunes("ab cd")) assert.Equal(t, []rune{'a', 'b', 'b', 'a'}, tricks.StringToRunes("abba")) } + +func TestIsEmptyString(t *testing.T) { + tcc := []struct { + s string + result bool + }{ + {"", true}, + {" ", true}, + {" ", true}, + {".", false}, + {" .", false}, + {" . ", false}, + {" _ ", false}, + {" - ", false}, + {"hello", false}, + } + + for k, tc := range tcc { + result := tricks.IsEmptyString(tc.s) + assert.Equalf(t, tc.result, result, "test case #%d '%s': expected %v, got %v", k, tc.s, tc.result, result) + } +}