File: value_boolean.go

package info (click to toggle)
golang-github-robertkrimen-otto 0.0~git20200922.ef014fd-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,796 kB
  • sloc: perl: 1,227; makefile: 79
file content (43 lines) | stat: -rw-r--r-- 793 bytes parent folder | download | duplicates (2)
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
43
package otto

import (
	"fmt"
	"math"
	"reflect"
	"unicode/utf16"
)

func (value Value) bool() bool {
	if value.kind == valueBoolean {
		return value.value.(bool)
	}
	if value.IsUndefined() {
		return false
	}
	if value.IsNull() {
		return false
	}
	switch value := value.value.(type) {
	case bool:
		return value
	case int, int8, int16, int32, int64:
		return 0 != reflect.ValueOf(value).Int()
	case uint, uint8, uint16, uint32, uint64:
		return 0 != reflect.ValueOf(value).Uint()
	case float32:
		return 0 != value
	case float64:
		if math.IsNaN(value) || value == 0 {
			return false
		}
		return true
	case string:
		return 0 != len(value)
	case []uint16:
		return 0 != len(utf16.Decode(value))
	}
	if value.IsObject() {
		return true
	}
	panic(fmt.Errorf("toBoolean(%T)", value.value))
}