1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-19 21:28:32 +02:00

41 lines
1.1 KiB
Go
Raw Normal View History

package arrays
import (
"context"
2018-10-14 20:06:27 +03:00
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
// NTH returns the element of an array at a given position.
2018-10-14 20:06:27 +03:00
// It is the same as anyArray[position] for positive positions, but does not support negative positions.
// If position is negative or beyond the upper bound of the array, then NONE will be returned.
// @param {Any[]} array - An array with elements of arbitrary type.
// @param {Int} index - Position of desired element in array, positions start at 0.
// @return {Any} - The array element at the given position.
func Nth(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 2, 2)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], types.Array)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[1], types.Int)
if err != nil {
return values.None, err
}
arr := args[0].(*values.Array)
idx := args[1].(values.Int)
return arr.Get(idx), nil
}