-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
42 lines (37 loc) · 1.12 KB
/
common.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
38
39
40
41
42
package jsutil
import (
"fmt"
"strings"
"syscall/js"
)
// GetFromScope retrieves an expression from the given scope.
func GetFromScope(scope js.Value, expr string) (js.Value, error) {
value := scope
for _, part := range strings.Split(expr, ".") {
if value.IsUndefined() {
return js.Undefined(), fmt.Errorf("cannot read properties of undefined (reading '%s' during parsing of '%s')", part, expr)
}
value = value.Get(part)
}
return value, nil
}
// Get retrieves an expression from the global scope.
func Get(expr string) (js.Value, error) {
return GetFromScope(js.Global(), expr)
}
// AssertTypeEquals returns nil if a given JavaScript value conforms to the given type.
func AssertTypeEquals(jsValue js.Value, jsType js.Type) error {
if jsValue.Type() != jsType {
return fmt.Errorf("not a %s", jsType.String())
}
return nil
}
// AssertTypeOneOf returns nil if a given JavaScript value conforms to one the given types
func AssertTypeOneOf(jsValue js.Value, jsTypes ...js.Type) error {
for _, jsType := range jsTypes {
if jsValue.Type() == jsType {
return nil
}
}
return fmt.Errorf("not any of %v", jsTypes)
}