From 61f6225a32bb279c74651c174c1f3119ae61feb7 Mon Sep 17 00:00:00 2001 From: Diego Henrique Oliveira Date: Fri, 17 Jan 2025 16:01:59 -0300 Subject: [PATCH] small cleanup --- comp.go | 62 ++++++------------------------------------------------ helpers.go | 52 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 60 deletions(-) diff --git a/comp.go b/comp.go index 2180364..4d461b2 100644 --- a/comp.go +++ b/comp.go @@ -1,12 +1,11 @@ package jsonlogic import ( - "math" "reflect" - "strconv" - "strings" ) +type undefinedType struct{} + // at simulate undefined in javascript func at(values []any, index int) any { if index >= 0 && index < len(values) { @@ -15,39 +14,6 @@ func at(values []any, index int) any { return undefinedType{} } -type undefinedType struct{} - -func toNumberForLess(v any) float64 { - switch value := v.(type) { - case nil: - return 0 - case undefinedType: - return math.NaN() - case float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: - return reflect.ValueOf(value).Convert(reflect.TypeOf(float64(0))).Float() - case bool: // Boolean values true and false are converted to 1 and 0 respectively. - if value { - return 1 - } else { - return 0 - } - case string: - if strings.TrimSpace(value) == "" { - return 0 - } - - n, err := strconv.ParseFloat(value, 64) - switch err { - case strconv.ErrRange, nil: - return n - default: - return math.NaN() - } - default: - return math.NaN() - } -} - // less reference javascript implementation // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Less_than#description func less(a, b any) bool { @@ -58,7 +24,7 @@ func less(a, b any) bool { } // Otherwise the values are compared as numeric values. - return toNumberForLess(b) > toNumberForLess(a) + return toNumberFromAny(b) > toNumberFromAny(a) } func hardEquals(a, b any) bool { @@ -79,25 +45,9 @@ func equals(a, b any) bool { return a == b } - if isPrimitive(a) && isPrimitive(b) && (isNumber(a) || isNumber(b)) { - return toNumberForLess(a) == toNumberForLess(b) - } - - if isBool(a) || isBool(b) { - return isTruthy(a) == isTruthy(b) - } - - if !isString(a) || !isString(b) { - return false + if isString(a) && isString(b) { + return a == b } - return toString(a) == toString(b) -} - -func isTruthy(v any) bool { - return toNumberForLess(v) == 1 -} - -func isFalsey(v any) bool { - return toNumberForLess(v) == 0 + return toNumberFromAny(a) == toNumberFromAny(b) } diff --git a/helpers.go b/helpers.go index 05a8b63..5212bab 100644 --- a/helpers.go +++ b/helpers.go @@ -1,8 +1,10 @@ package jsonlogic import ( + "math" "reflect" "strconv" + "strings" ) func is(obj any, kind reflect.Kind) bool { @@ -58,13 +60,11 @@ func isTrue(obj any) bool { } if isNumber(obj) { - n := toNumber(obj) - return n != 0 + return toNumber(obj) != 0 } if isString(obj) || isSlice(obj) || isMap(obj) { - length := reflect.ValueOf(obj).Len() - return length > 0 + return reflect.ValueOf(obj).Len() > 0 } return false @@ -95,6 +95,50 @@ func toNumber(value any) float64 { } } +// toNumberFromAny converts various input types to float64. +// +// Examples: +// +// toNumberFromAny(42) // Returns: 42.0 +// toNumberFromAny("3.14") // Returns: 3.14 +// toNumberFromAny(true) // Returns: 1.0 +// toNumberFromAny(false) // Returns: 0.0 +// toNumberFromAny([]int{1, 2, 3}) // Returns: 3.0 (length of slice) +// toNumberFromAny(map[string]int{"a": 1, "b": 2}) // Returns: 2.0 (length of map) +// toNumberFromAny(nil) // Returns: 0.0 +// +// Note: For unsupported types, it returns 0.0 +func toNumberFromAny(v any) float64 { + switch value := v.(type) { + case nil: + return 0 + case undefinedType: + return math.NaN() + case float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: + return reflect.ValueOf(value).Convert(reflect.TypeOf(float64(0))).Float() + case bool: // Boolean values true and false are converted to 1 and 0 respectively. + if value { + return 1 + } else { + return 0 + } + case string: + if strings.TrimSpace(value) == "" { + return 0 + } + + n, err := strconv.ParseFloat(value, 64) + switch err { + case strconv.ErrRange, nil: + return n + default: + return math.NaN() + } + default: + return math.NaN() + } +} + func toString(value any) string { if isNumber(value) { switch value := value.(type) {