Skip to content

Commit

Permalink
Simplifies signatures in js.go
Browse files Browse the repository at this point in the history
References dennwc#45
  • Loading branch information
Steve Moyer committed Jun 3, 2019
1 parent 5d59134 commit 7fe0129
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type Obj = map[string]interface{}
type Arr = []interface{}

// Get is a shorthand for Global().Get().
func Get(name string, path ...string) Value {
return Value{global}.Get(name, path...)
func Get(name ...string) Value {
return Value{global}.Get(name...)
}

// Set is a shorthand for Global().Set().
Expand All @@ -38,22 +38,19 @@ func Call(name string, args ...interface{}) Value {

// Class searches for a class in global scope.
// It caches results, so the lookup should be faster than calling Get.
func Class(class string, path ...string) Value {
switch class {
func Class(class ...string) Value {
switch class[0] {
case "Object":
return Value{object}
case "Array":
return Value{array}
}
key := class
if len(path) != 0 {
key += "." + strings.Join(path, ".")
}
key := strings.Join(class, ".")
mu.RLock()
v := classes[key]
mu.RUnlock()
if v.isZero() {
v = Get(class, path...)
v = Get(class...)
mu.Lock()
classes[key] = v
mu.Unlock()
Expand Down Expand Up @@ -152,9 +149,9 @@ func (v Value) Valid() bool {
}

// Get returns the JS property by name.
func (v Value) Get(name string, path ...string) Value {
ref := v.Ref.Get(name)
for _, p := range path {
func (v Value) Get(name ...string) Value {
ref := v.Ref.Get(name[0])
for _, p := range name[1:] {
ref = ref.Get(p)
}
return Value{ref}
Expand Down

0 comments on commit 7fe0129

Please sign in to comment.