Skip to content

Commit

Permalink
Merge pull request #101 from diegoholiveira/cleanup
Browse files Browse the repository at this point in the history
small cleanup
  • Loading branch information
diegoholiveira authored Jan 17, 2025
2 parents 3a4ba23 + 61f6225 commit 23f094e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 60 deletions.
62 changes: 6 additions & 56 deletions comp.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
52 changes: 48 additions & 4 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package jsonlogic

import (
"math"
"reflect"
"strconv"
"strings"
)

func is(obj any, kind reflect.Kind) bool {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 23f094e

Please sign in to comment.