1// Package jsbuiltin provides minimal wrappers around some JavasScript
2// built-in functions.
3package jsbuiltin
4
5import (
6	"errors"
7
8	"github.com/gopherjs/gopherjs/js"
9)
10
11// DecodeURI decodes a Uniform Resource Identifier (URI) previously created
12// by EncodeURI() or by a similar routine. If the underlying JavaScript
13// function throws an error, it is returned as an error.
14func DecodeURI(uri string) (raw string, err error) {
15	defer func() {
16		if r := recover(); r != nil {
17			err = r.(*js.Error)
18		}
19	}()
20	raw = js.Global.Call("decodeURI", uri).String()
21	return
22}
23
24// EncodeURI encodes a Uniform Resource Identifier (URI) by replacing each
25// instance of certain characters by one, two, three, or four escape sequences
26// representing the UTF-8 encoding of the character (will only be four escape
27// sequences for characters composed of two "surrogate" characters).
28func EncodeURI(uri string) string {
29	return js.Global.Call("encodeURI", uri).String()
30}
31
32// EncodeURIComponent encodes a Uniform Resource Identifier (URI) component
33// by replacing each instance of certain characters by one, two, three, or
34// four escape sequences representing the UTF-8 encoding of the character
35// (will only be four escape sequences for characters composed of two
36// "surrogate" characters).
37func EncodeURIComponent(uri string) string {
38	return js.Global.Call("encodeURIComponent", uri).String()
39}
40
41// DecodeURIComponent decodes a Uniform Resource Identifier (URI) component
42// previously created by EncodeURIComponent() or by a similar routine. If the
43// underlying JavaScript function throws an error, it is returned as an error.
44func DecodeURIComponent(uri string) (raw string, err error) {
45	defer func() {
46		if r := recover(); r != nil {
47			err = r.(*js.Error)
48		}
49	}()
50	raw = js.Global.Call("decodeURIComponent", uri).String()
51	return
52}
53
54// IsFinite determines whether the passed value is a finite number, and returns
55// true if it is. If needed, the parameter is first converted to a number.
56func IsFinite(value interface{}) bool {
57	return js.Global.Call("isFinite", value).Bool()
58}
59
60// IsNaN determines whether a value is NaN (Not-a-Number) or not. A return
61// value of true indicates the input value is considered NaN by JavaScript.
62func IsNaN(value interface{}) bool {
63	return js.Global.Call("isNaN", value).Bool()
64}
65
66// Type constants represent the JavaScript builtin types, which may be returned
67// by TypeOf().
68const (
69	TypeUndefined = "undefined"
70	TypeNull      = "null"
71	TypeObject    = "object"
72	TypeBoolean   = "boolean"
73	TypeNumber    = "number"
74	TypeString    = "string"
75	TypeFunction  = "function"
76	TypeSymbol    = "symbol"
77)
78
79// TypeOf returns the JavaScript type of the passed value
80func TypeOf(value interface{}) string {
81	return js.Global.Get("$jsbuiltin$").Call("typeoffunc", value).String()
82}
83
84// InstanceOf returns true if value is an instance of object according to the
85// built-in 'instanceof' operator. `object` must be a *js.Object representing
86// a javascript constructor function.
87func InstanceOf(value interface{}, object *js.Object) bool {
88	return js.Global.Get("$jsbuiltin$").Call("instanceoffunc", value, object).Bool()
89}
90
91// In returns true if key is a member of obj. An error is returned if obj is not
92// a JavaScript object.
93func In(key string, obj *js.Object) (ok bool, err error) {
94	if obj == nil || obj == js.Undefined || TypeOf(obj) != TypeObject {
95		return false, errors.New("obj not a JavaScript object")
96	}
97	return js.Global.Get("$jsbuiltin$").Call("infunc", key, obj).Bool(), nil
98}
99