+ ArrayGet & ArrayIndexes helper

This commit is contained in:
Max 2022-01-08 17:45:45 +08:00
parent e072d87a7d
commit 443d49cbef
3 changed files with 45 additions and 0 deletions

View file

@ -69,3 +69,25 @@ func ProcessArrayMapSet(process *gou.Process) interface{} {
}
return process.Args[0]
}
// ProcessArrayIndexes xiang.helper.ArrayIndexes 返回数组索引。
func ProcessArrayIndexes(process *gou.Process) interface{} {
process.ValidateArgNums(1)
records := process.ArgsArray(0)
res := []int{}
for index := range records {
res = append(res, index)
}
return res
}
// ProcessArrayGet xiang.helper.ArrayGet 返回指定索引数据
func ProcessArrayGet(process *gou.Process) interface{} {
process.ValidateArgNums(2)
records := process.ArgsArray(0)
index := process.ArgsInt(1)
if index >= len(records) {
return nil
}
return records[index]
}

View file

@ -166,3 +166,24 @@ func TestProcessArrayUnique(t *testing.T) {
assert.True(t, ok)
assert.Equal(t, []interface{}{1, 2, 3}, res)
}
func TestProcessArrayIndexes(t *testing.T) {
args := []interface{}{
[]interface{}{1, 2, 3, 3},
}
response := gou.NewProcess("xiang.helper.ArrayIndexes", args...).Run()
assert.NotNil(t, response)
res, ok := response.([]int)
assert.True(t, ok)
assert.Equal(t, []int{0, 1, 2, 3}, res)
}
func TestProcessArrayGet(t *testing.T) {
response := gou.NewProcess("xiang.helper.ArrayGet", []interface{}{1, 2, 3, 3}, 2).Run()
assert.Equal(t, 3, response)
response = gou.NewProcess("xiang.helper.ArrayGet", []interface{}{1, 2, 3, 3}, 4).Run()
assert.Nil(t, response)
}

View file

@ -7,6 +7,8 @@ import (
func init() {
// 注册处理器
gou.RegisterProcessHandler("xiang.helper.ArrayGet", ProcessArrayGet)
gou.RegisterProcessHandler("xiang.helper.ArrayIndexes", ProcessArrayIndexes)
gou.RegisterProcessHandler("xiang.helper.ArrayPluck", ProcessArrayPluck)
gou.RegisterProcessHandler("xiang.helper.ArraySplit", ProcessArraySplit)
gou.RegisterProcessHandler("xiang.helper.ArrayColumn", ProcessArrayColumn)