1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00
otto/value_boolean.go
Steven Hartland 026a1d9a9c
chore: lint and naming refactor (#475)
Enable more linters, address the issues and do a major naming refactor
to use golang lower camelCase identifiers for types, functions, methods
and variable names.

Also: 
* Clean up inline generation so it doesn't rely on temporary variables.
* Remove unused functions generated by inline.pl.
2022-12-04 21:49:38 +00:00

41 lines
754 B
Go

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