From f23723eca49927faf01b7ce54f25b7938164e755 Mon Sep 17 00:00:00 2001 From: elvinchan Date: Wed, 11 Mar 2020 10:05:56 +0800 Subject: [PATCH] #109 Add more functions for converter --- converter/convert.go | 63 ++++++++++++++++++++++++++++++++++++++- converter/convert_test.go | 34 +++++++++++++++++++-- 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/converter/convert.go b/converter/convert.go index 09db27c..16c3f7e 100644 --- a/converter/convert.go +++ b/converter/convert.go @@ -8,13 +8,14 @@ import ( func StringToIntSlice(s string) []int64 { var list []int64 for _, v := range strings.Split(strings.TrimSpace(s), ",") { - i, _ := strconv.ParseInt(v, 10, 64) + i, _ := strconv.ParseInt(strings.TrimSpace(v), 10, 64) if i != 0 { list = append(list, i) } } return list } + func StringToStringSlice(s string) []string { var list []string for _, v := range strings.Split(strings.TrimSpace(s), ",") { @@ -22,6 +23,66 @@ func StringToStringSlice(s string) []string { list = append(list, vv) } } + return list +} +func StringSliceToInt64Slice(s []string) []int64 { + var list []int64 + for _, v := range s { + i, _ := strconv.ParseInt(strings.TrimSpace(v), 10, 64) + list = append(list, i) + } return list } + +func Int64SliceToStringSlice(s []int64) []string { + var list []string + for _, v := range s { + list = append(list, strconv.FormatInt(v, 10)) + } + return list +} + +func Int64SliceToString(s []int64) string { + return strings.Join(Int64SliceToStringSlice(s), ",") +} + +func ContainsInt64(s []int64, t int64) bool { + for _, v := range s { + if v == t { + return true + } + } + return false +} + +func UniqueInt64(s []int64, positive bool) []int64 { + m := make(map[int64]struct{}, len(s)) + i := 0 + for _, t := range s { + if _, ok := m[t]; ok { + continue + } + if positive && t <= 0 { + continue + } + m[t] = struct{}{} + s[i] = t + i++ + } + return s[:i] +} + +func UniqueString(s []string) []string { + m := make(map[string]struct{}, len(s)) + i := 0 + for _, t := range s { + if _, ok := m[t]; ok { + continue + } + m[t] = struct{}{} + s[i] = t + i++ + } + return s[:i] +} diff --git a/converter/convert_test.go b/converter/convert_test.go index f20c87e..9ead551 100644 --- a/converter/convert_test.go +++ b/converter/convert_test.go @@ -9,13 +9,43 @@ import ( func TestConvert(t *testing.T) { t.Run("StringToIntSlice", func(t *testing.T) { - s := "1,2,3,4,5,6" + s := "1,2,3,4, 5,6 " list := converter.StringToIntSlice(s) test.Equals(t, list, []int64{1, 2, 3, 4, 5, 6}) }) t.Run("StringToStringSlice", func(t *testing.T) { - s := "1,2,3,4,5,6" + s := "1,2,3,4, 5,6 " list := converter.StringToStringSlice(s) test.Equals(t, list, []string{"1", "2", "3", "4", "5", "6"}) }) + t.Run("StringSliceToInt64Slice", func(t *testing.T) { + s := []string{"1", "2", "3", "4", " 5", "6"} + list := converter.StringSliceToInt64Slice(s) + test.Equals(t, list, []int64{1, 2, 3, 4, 5, 6}) + }) + t.Run("Int64SliceToStringSlice", func(t *testing.T) { + s := []int64{1, 2, 3, 4, 5, 6} + list := converter.Int64SliceToStringSlice(s) + test.Equals(t, list, []string{"1", "2", "3", "4", "5", "6"}) + }) + t.Run("Int64SliceToString", func(t *testing.T) { + s := []int64{1, 2, 3, 4, 5, 6} + list := converter.Int64SliceToString(s) + test.Equals(t, list, "1,2,3,4,5,6") + }) + t.Run("ContainsInt64", func(t *testing.T) { + s := []int64{1, 2, 3, 4, 5, 6} + b := converter.ContainsInt64(s, 5) + test.Equals(t, true, b) + }) + t.Run("UniqueInt64", func(t *testing.T) { + s := []int64{-1, 6, 3, 4, 3, 6} + list := converter.UniqueInt64(s, true) + test.Equals(t, list, []int64{6, 3, 4}) + }) + t.Run("UniqueString", func(t *testing.T) { + s := []string{"-1", "6", "3", "4", "3", "6"} + list := converter.UniqueString(s) + test.Equals(t, list, []string{"-1", "6", "3", "4"}) + }) }