forked from dominikh/go-js-xhr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
37 lines (31 loc) · 833 Bytes
/
params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package xhr
import (
"github.com/gopherjs/gopherjs/js"
)
// Params represents a URLSearchParams object
type Params struct {
*js.Object
}
// NewParams returns a new URLSearchParams object.
func NewParams(kv ...js.M) *Params {
o := js.Global.Get("URLSearchParams").New()
p := &Params{Object: o}
if len(kv) > 0 {
p.Append(kv[0])
}
return p
}
// Appends a specified key/value pair as a new search parameter.
func (p *Params) Append(kv js.M) {
for name, val := range kv {
p.Call("append", name, val)
}
}
// String returns a string containing a query string suitable for use in a URL.
func (p *Params) String() string {
return p.Call("toString").String()
}
// ToJSON will convert an object or map into a JSON string.
func ToJSON(obj interface{}) string {
return js.Global.Get("JSON").Call("stringify", obj).String()
}