Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: arrutil - add new util func IntsToString() and add more test …
…cases
- Loading branch information
Showing
10 changed files
with
165 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package arrutil | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/gookit/goutil/comdef" | ||
) | ||
|
||
// IntsToString convert []T to string | ||
func IntsToString[T comdef.Integer](ints []T) string { | ||
if len(ints) == 0 { | ||
return "[]" | ||
} | ||
|
||
var sb strings.Builder | ||
sb.WriteByte('[') | ||
for i, v := range ints { | ||
if i > 0 { | ||
sb.WriteByte(',') | ||
} | ||
sb.WriteString(strconv.FormatInt(int64(v), 10)) | ||
} | ||
sb.WriteByte(']') | ||
return sb.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package arrutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/arrutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestIntsToString(t *testing.T) { | ||
assert.Eq(t, "[]", arrutil.IntsToString([]int{})) | ||
assert.Eq(t, "[1,2,3]", arrutil.IntsToString([]int{1, 2, 3})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package arrutil | ||
|
||
import "strconv" | ||
|
||
// StringsToSlice convert []string to []any | ||
func StringsToSlice(ss []string) []any { | ||
args := make([]any, len(ss)) | ||
for i, s := range ss { | ||
args[i] = s | ||
} | ||
return args | ||
} | ||
|
||
// StringsAsInts convert and ignore error | ||
func StringsAsInts(ss []string) []int { | ||
ints, _ := StringsTryInts(ss) | ||
return ints | ||
} | ||
|
||
// StringsToInts string slice to int slice | ||
func StringsToInts(ss []string) (ints []int, err error) { | ||
return StringsTryInts(ss) | ||
} | ||
|
||
// StringsTryInts string slice to int slice | ||
func StringsTryInts(ss []string) (ints []int, err error) { | ||
for _, str := range ss { | ||
iVal, err := strconv.Atoi(str) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ints = append(ints, iVal) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package arrutil_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/gookit/goutil/arrutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestStringsToSlice(t *testing.T) { | ||
is := assert.New(t) | ||
|
||
as := arrutil.StringsToSlice([]string{"1", "2"}) | ||
is.Eq(`[]interface {}{"1", "2"}`, fmt.Sprintf("%#v", as)) | ||
} | ||
|
||
func TestStringsToInts(t *testing.T) { | ||
is := assert.New(t) | ||
|
||
ints, err := arrutil.StringsToInts([]string{"1", "2"}) | ||
is.Nil(err) | ||
is.Eq("[]int{1, 2}", fmt.Sprintf("%#v", ints)) | ||
|
||
_, err = arrutil.StringsToInts([]string{"a", "b"}) | ||
is.Err(err) | ||
|
||
ints = arrutil.StringsAsInts([]string{"1", "2"}) | ||
is.Eq("[]int{1, 2}", fmt.Sprintf("%#v", ints)) | ||
is.Nil(arrutil.StringsAsInts([]string{"abc"})) | ||
} |