Skip to content

Commit

Permalink
pangpanglabs#109 Add more functions for converter
Browse files Browse the repository at this point in the history
  • Loading branch information
elvinchan committed Mar 11, 2020
1 parent 3f9fcfa commit f23723e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
63 changes: 62 additions & 1 deletion converter/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,81 @@ 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), ",") {
if vv := strings.TrimSpace(v); len(vv) != 0 {
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]
}
34 changes: 32 additions & 2 deletions converter/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
})
}

0 comments on commit f23723e

Please sign in to comment.