-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
👔 up: arrutil - upgrade some util func to generic version, add more t…
…ests
- Loading branch information
Showing
9 changed files
with
185 additions
and
80 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 was deleted.
Oops, something went wrong.
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,102 @@ | ||
package arrutil | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/gookit/goutil/comdef" | ||
) | ||
|
||
// Reverse any T slice. | ||
// | ||
// eg: []string{"site", "user", "info", "0"} -> []string{"0", "info", "user", "site"} | ||
func Reverse[T any](ls []T) { | ||
ln := len(ls) | ||
for i := 0; i < ln/2; i++ { | ||
li := ln - i - 1 | ||
ls[i], ls[li] = ls[li], ls[i] | ||
} | ||
} | ||
|
||
// Remove give element from slice []T. | ||
// | ||
// eg: []string{"site", "user", "info", "0"} -> []string{"site", "user", "info"} | ||
func Remove[T comdef.Compared](ls []T, val T) []T { | ||
return Filter(ls, func(el T) bool { | ||
return el != val | ||
}) | ||
} | ||
|
||
// Filter given slice, default will filter zero value. | ||
// | ||
// Usage: | ||
// | ||
// // output: [a, b] | ||
// ss := arrutil.Filter([]string{"a", "", "b", ""}) | ||
func Filter[T any](ls []T, filter ...comdef.MatchFunc[T]) []T { | ||
var fn comdef.MatchFunc[T] | ||
if len(filter) > 0 && filter[0] != nil { | ||
fn = filter[0] | ||
} else { | ||
fn = func(el T) bool { | ||
return !reflect.ValueOf(el).IsZero() | ||
} | ||
} | ||
|
||
newLs := make([]T, 0, len(ls)) | ||
for _, el := range ls { | ||
if fn(el) { | ||
newLs = append(newLs, el) | ||
} | ||
} | ||
return newLs | ||
} | ||
|
||
// MapFn map handle function type. | ||
type MapFn[T any, V any] func(input T) (target V, find bool) | ||
|
||
// Map a list to new list | ||
// | ||
// eg: mapping [object0{},object1{},...] to flatten list [object0.someKey, object1.someKey, ...] | ||
func Map[T any, V any](list []T, mapFn MapFn[T, V]) []V { | ||
flatArr := make([]V, 0, len(list)) | ||
|
||
for _, obj := range list { | ||
if target, ok := mapFn(obj); ok { | ||
flatArr = append(flatArr, target) | ||
} | ||
} | ||
return flatArr | ||
} | ||
|
||
// Column alias of Map func | ||
func Column[T any, V any](list []T, mapFn func(obj T) (val V, find bool)) []V { | ||
return Map(list, mapFn) | ||
} | ||
|
||
// Unique value in the given slice data. | ||
func Unique[T ~string | comdef.XintOrFloat](list []T) []T { | ||
if len(list) < 2 { | ||
return list | ||
} | ||
|
||
valMap := make(map[T]struct{}, len(list)) | ||
uniArr := make([]T, 0, len(list)) | ||
|
||
for _, t := range list { | ||
if _, ok := valMap[t]; !ok { | ||
valMap[t] = struct{}{} | ||
uniArr = append(uniArr, t) | ||
} | ||
} | ||
return uniArr | ||
} | ||
|
||
// IndexOf value in given slice. | ||
func IndexOf[T ~string | comdef.XintOrFloat](val T, list []T) int { | ||
for i, v := range list { | ||
if v == val { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} |
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,34 @@ | ||
package arrutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/arrutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestReverse(t *testing.T) { | ||
ss := []string{"a", "b", "c"} | ||
arrutil.Reverse(ss) | ||
assert.Eq(t, []string{"c", "b", "a"}, ss) | ||
|
||
ints := []int{1, 2, 3} | ||
arrutil.Reverse(ints) | ||
assert.Eq(t, []int{3, 2, 1}, ints) | ||
} | ||
|
||
func TestRemove(t *testing.T) { | ||
ss := []string{"a", "b", "c"} | ||
ns := arrutil.Remove(ss, "b") | ||
assert.Eq(t, []string{"a", "c"}, ns) | ||
|
||
ints := []int{1, 2, 3} | ||
ni := arrutil.Remove(ints, 2) | ||
assert.Eq(t, []int{1, 3}, ni) | ||
} | ||
|
||
func TestFilter(t *testing.T) { | ||
is := assert.New(t) | ||
ss := arrutil.Filter([]string{"a", "", "b", ""}) | ||
is.Eq([]string{"a", "b"}, ss) | ||
} |