package js import ( "errors" "github.com/dop251/goja" "github.com/ssgo/u" ) func toMap(data any) map[string]any { return u.UnJsonMap(u.FixedJson(data)) } type Args struct { This goja.Value Arguments []goja.Value VM *goja.Runtime } func MakeArgs(args *goja.FunctionCall, vm *goja.Runtime) *Args { return &Args{ This: args.This, Arguments: args.Arguments, VM: vm, } } func (args *Args) Check(num int) goja.Value { if len(args.Arguments) < num { return args.VM.NewGoError(errors.New("arguments need " + u.String(num) + ", but given " + u.String(len(args.Arguments)))) } return nil } func (args *Args) Int(index int) int { if len(args.Arguments) > index { return u.Int(args.Arguments[index].Export()) } return 0 } func (args *Args) Any(index int) any { if len(args.Arguments) > index { return args.Arguments[index].Export() } return nil } func (args *Args) Str(index int) string { if len(args.Arguments) > index { return u.String(args.Arguments[index].Export()) } return "" } func (args *Args) Map(index int) map[string]any { out := map[string]any{} if len(args.Arguments) > index { u.Convert(args.Arguments[index].Export(), &out) } return out } func (args *Args) StrArr(startIndex int) []string { if len(args.Arguments) > startIndex { a := make([]string, len(args.Arguments)-startIndex) for index := startIndex; index < len(args.Arguments); index++ { a[index-startIndex] = u.String(args.Arguments[index].Export()) } return a } return make([]string, 0) } func (args *Args) Map2StrArr(index int) []string { headerMap := args.Map(index) headers := make([]string, len(headerMap)*2) i := 0 for k, v := range headerMap { headers[i] = k headers[i+1] = u.String(v) i += 2 } return headers }