Skip to content

Commit

Permalink
Merge pull request #13 from flimzy/in
Browse files Browse the repository at this point in the history
Add a wrapper around the 'in' operator
  • Loading branch information
flimzy committed Apr 27, 2017
2 parents 8831a52 + 39f68e6 commit 67703bf
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,6 +1,6 @@
language: go
go:
- 1.7.x
- 1.8.x
addons:
apt:
packages:
Expand Down
11 changes: 11 additions & 0 deletions jsbuiltin.go 100644 → 100755
Expand Up @@ -3,6 +3,8 @@
package jsbuiltin

import (
"errors"

"github.com/gopherjs/gopherjs/js"
)

Expand Down Expand Up @@ -85,3 +87,12 @@ func TypeOf(value interface{}) string {
func InstanceOf(value interface{}, object *js.Object) bool {
return js.Global.Get("$jsbuiltin$").Call("instanceoffunc", value, object).Bool()
}

// In returns true if key is a member of obj. An error is returned if obj is not
// a JavaScript object.
func In(key string, obj *js.Object) (ok bool, err error) {
if obj == nil || obj == js.Undefined || TypeOf(obj) != TypeObject {
return false, errors.New("obj not a JavaScript object")
}
return js.Global.Get("$jsbuiltin$").Call("infunc", key, obj).Bool(), nil
}
3 changes: 2 additions & 1 deletion jsbuiltin.inc.js 100644 → 100755
@@ -1,4 +1,5 @@
$global.$jsbuiltin$ = {
typeoffunc: function(x) { return typeof x },
instanceoffunc: function(x,y) { return x instanceof y }
instanceoffunc: function(x,y) { return x instanceof y },
infunc: function(x,y) { return x in y }
}
39 changes: 36 additions & 3 deletions jsbuiltin_test.go
Expand Up @@ -50,7 +50,7 @@ func TestDecodeURI(t *testing.T) {
}
} else {
if err != nil {
t.Fatal("DecodeURI() resulted in an error: %s", err)
t.Fatalf("DecodeURI() resulted in an error: %s", err)
}
if result != test.ExpectedURL {
t.Fatalf("DecodeURI(%s) returned '%s', not '%s'", test.URL, result, test.ExpectedURL)
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestDecodeURIComponentn(t *testing.T) {
}
} else {
if err != nil {
t.Fatal("DecodeURIComponent() resulted in an error: %s", err)
t.Fatalf("DecodeURIComponent() resulted in an error: %s", err)
}
if result != test.ExpectedURL {
t.Fatalf("DecodeURIComponent(%s) returned '%s', not '%s'", test.URL, result, test.ExpectedURL)
Expand Down Expand Up @@ -194,7 +194,40 @@ func TestInstanceOf(t *testing.T) {
for _, test := range data {
result := InstanceOf(test.value, test.object)
if result != test.result {
t.Fatalf("InstanceOf(%s,%s) returned %s, not %s", test.value, test.object, result, test.result)
t.Errorf("InstanceOf(%s,%s) returned %t, not %t", test.value, test.object, result, test.result)
}
}
}

type inTest struct {
obj *js.Object
key string
result bool
err string
}

func TestIn(t *testing.T) {
obj := js.Global.Get("Object").New()
obj.Set("foo", "bar")
jsString := js.Global.Call("eval", `'"test string"'`)
data := []inTest{
{obj: obj, key: "foo", result: true},
{obj: obj, key: "bar", result: false},
{obj: js.Undefined, key: "foo", err: "obj not a JavaScript function"},
{obj: nil, key: "foo", err: "obj not a JavaScript function"},
{obj: jsString, key: "foo", err: "obj not a JavaScript function"},
}
for _, test := range data {
result, err := In(test.key, test.obj)
var msg string
if err != nil {
msg = err.Error()
}
if msg != test.err {
t.Errorf("Unexpected error: %s", msg)
}
if result != test.result {
t.Errorf("In(%v, %s) returned %t, not %t", test.obj, test.key, result, test.result)
}
}
}

0 comments on commit 67703bf

Please sign in to comment.