X Tutup
Skip to content

Commit b0321a6

Browse files
theclappbradfitz
authored andcommitted
syscall/js: add the Value.Truthy method
Truthy returns the JavaScript "truthiness" of the given value. In JavaScript, false, 0, "", null, undefined, and NaN are "falsy", and everything else is "truthy". Fixes golang#28264 Change-Id: I4586f98646c05a4147d06a7c4a5d9c61d956fc83 GitHub-Last-Rev: 649b353 GitHub-Pull-Request: golang#28358 Reviewed-on: https://go-review.googlesource.com/c/144384 Reviewed-by: Richard Musiol <neelance@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 64967ff commit b0321a6

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/syscall/js/js.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,26 @@ func (v Value) Bool() bool {
370370
}
371371
}
372372

373+
// Truthy returns the JavaScript "truthiness" of the value v. In JavaScript,
374+
// false, 0, "", null, undefined, and NaN are "falsy", and everything else is
375+
// "truthy". See https://developer.mozilla.org/en-US/docs/Glossary/Truthy.
376+
func (v Value) Truthy() bool {
377+
switch v.Type() {
378+
case TypeUndefined, TypeNull:
379+
return false
380+
case TypeBoolean:
381+
return v.Bool()
382+
case TypeNumber:
383+
return v.ref != valueNaN.ref && v.ref != valueZero.ref
384+
case TypeString:
385+
return v.String() != ""
386+
case TypeSymbol, TypeFunction, TypeObject:
387+
return true
388+
default:
389+
panic("bad type")
390+
}
391+
}
392+
373393
// String returns the value v converted to string according to JavaScript type conversions.
374394
func (v Value) String() string {
375395
str, length := valuePrepareString(v.ref)

src/syscall/js/js_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55
// +build js,wasm
66

7+
// To run these tests:
8+
//
9+
// - Install Node
10+
// - Add /path/to/go/misc/wasm to your $PATH (so that "go test" can find
11+
// "go_js_wasm_exec").
12+
// - GOOS=js GOARCH=wasm go test
13+
//
14+
// See -exec in "go help test", and "go help run" for details.
15+
716
package js_test
817

918
import (
@@ -19,11 +28,19 @@ var dummys = js.Global().Call("eval", `({
1928
someInt: 42,
2029
someFloat: 42.123,
2130
someArray: [41, 42, 43],
31+
someDate: new Date(),
2232
add: function(a, b) {
2333
return a + b;
2434
},
2535
zero: 0,
36+
stringZero: "0",
2637
NaN: NaN,
38+
emptyObj: {},
39+
emptyArray: [],
40+
Infinity: Infinity,
41+
NegInfinity: -Infinity,
42+
objNumber0: new Number(0),
43+
objBooleanFalse: new Boolean(false),
2744
})`)
2845

2946
func TestBool(t *testing.T) {
@@ -331,3 +348,40 @@ func ExampleNewCallback() {
331348
})
332349
js.Global().Get("document").Call("getElementById", "myButton").Call("addEventListener", "click", cb)
333350
}
351+
352+
// See
353+
// - https://developer.mozilla.org/en-US/docs/Glossary/Truthy
354+
// - https://stackoverflow.com/questions/19839952/all-falsey-values-in-javascript/19839953#19839953
355+
// - http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
356+
func TestTruthy(t *testing.T) {
357+
want := true
358+
for _, key := range []string{
359+
"someBool", "someString", "someInt", "someFloat", "someArray", "someDate",
360+
"stringZero", // "0" is truthy
361+
"add", // functions are truthy
362+
"emptyObj", "emptyArray", "Infinity", "NegInfinity",
363+
// All objects are truthy, even if they're Number(0) or Boolean(false).
364+
"objNumber0", "objBooleanFalse",
365+
} {
366+
if got := dummys.Get(key).Truthy(); got != want {
367+
t.Errorf("%s: got %#v, want %#v", key, got, want)
368+
}
369+
}
370+
371+
want = false
372+
if got := dummys.Get("zero").Truthy(); got != want {
373+
t.Errorf("got %#v, want %#v", got, want)
374+
}
375+
if got := dummys.Get("NaN").Truthy(); got != want {
376+
t.Errorf("got %#v, want %#v", got, want)
377+
}
378+
if got := js.ValueOf("").Truthy(); got != want {
379+
t.Errorf("got %#v, want %#v", got, want)
380+
}
381+
if got := js.Null().Truthy(); got != want {
382+
t.Errorf("got %#v, want %#v", got, want)
383+
}
384+
if got := js.Undefined().Truthy(); got != want {
385+
t.Errorf("got %#v, want %#v", got, want)
386+
}
387+
}

0 commit comments

Comments
 (0)
X Tutup