3
0

Added splice workflow function

This commit is contained in:
Peter Grlica 2023-08-25 14:48:04 +02:00
parent 7254628033
commit 7bc36eb46f
No known key found for this signature in database
2 changed files with 20 additions and 11 deletions

View File

@ -19,6 +19,7 @@ func ArrayFunctions() []gval.Language {
gval.Function("hasAll", hasAll),
gval.Function("find", find),
gval.Function("sort", sortSlice),
gval.Function("splice", splice),
}
}
@ -210,9 +211,17 @@ func find(arr interface{}, v interface{}) (p int, err error) {
return -1, nil
}
// slice slices slices
func slice(arr interface{}, start, end int) interface{} {
arr = UntypedValue(arr)
// splice slices slice
func splice(arr interface{}, s, e float64) interface{} {
var (
err error
start = int(s)
end = int(e)
)
if arr, err = toSlice(arr); err != nil {
return nil
}
v := reflect.ValueOf(arr)

View File

@ -447,34 +447,34 @@ func Test_hasAll(t *testing.T) {
}
}
func Test_slice(t *testing.T) {
func Test_splice(t *testing.T) {
tcc := []struct {
vals []int
vals []float64
arr interface{}
expect interface{}
}{
{
vals: []int{0, 3},
vals: []float64{0, 3},
arr: []string{"1", "2", "3"},
expect: []string{"1", "2", "3"},
},
{
vals: []int{0, 1},
vals: []float64{0, 1},
arr: []string{"1", "2", "3"},
expect: []string{"1"},
},
{
vals: []int{2, 3},
vals: []float64{2, 3},
arr: []bool{true, true},
expect: []bool{true, true},
},
{
vals: []int{1, -1},
vals: []float64{1, -1},
arr: []int{4, 5, 6, 7},
expect: []int{5, 6, 7},
},
{
vals: []int{3, -1},
vals: []float64{3, -1},
arr: []float64{11.1, 12.4},
expect: []float64{11.1, 12.4},
},
@ -484,7 +484,7 @@ func Test_slice(t *testing.T) {
t.Run(fmt.Sprintf("%d", p), func(t *testing.T) {
var (
req = require.New(t)
ss = slice(tc.arr, tc.vals[0], tc.vals[1])
ss = splice(tc.arr, tc.vals[0], tc.vals[1])
)
req.Equal(tc.expect, ss)