Repository: diegoholiveira/jsonlogic
Pull request #63 (2022-12-12 13:14:28)

Add the toSliceOfNumbers to clear the code in `-` and `/` functions


Patch:
diff --git a/helpers.go b/helpers.go
index aa557d0..cc859ec 100644
--- a/helpers.go
+++ b/helpers.go
@@ -51,6 +51,16 @@ func isTrue(obj interface{}) bool {
 	return false
 }
 
+func toSliceOfNumbers(values interface{}) []float64 {
+	_values := values.([]interface{})
+
+	numbers := make([]float64, len(_values))
+	for i, n := range _values {
+		numbers[i] = toNumber(n)
+	}
+	return numbers
+}
+
 func toNumber(value interface{}) float64 {
 	if isString(value) {
 		w, _ := strconv.ParseFloat(value.(string), 64)
diff --git a/math.go b/math.go
index 8c9f5a3..81aad6e 100644
--- a/math.go
+++ b/math.go
@@ -26,18 +26,11 @@ func sum(values interface{}) interface{} {
 }
 
 func minus(values interface{}) interface{} {
-	var sum float64
+	_values := toSliceOfNumbers(values)
 
-	isFirst := true
-	for _, n := range values.([]interface{}) {
-		if isFirst {
-			isFirst = false
-			sum = toNumber(n)
-
-			continue
-		}
-
-		sum -= toNumber(n)
+	sum := _values[0]
+	for i := 1; len(_values) > i; i++ {
+		sum -= _values[i]
 	}
 
 	return sum
@@ -54,16 +47,11 @@ func mult(values interface{}) interface{} {
 }
 
 func div(values interface{}) interface{} {
-	var sum float64
-
-	for i, n := range values.([]interface{}) {
-		if i == 0 {
-			sum = toNumber(n)
-
-			continue
-		}
+	_values := toSliceOfNumbers(values)
 
-		sum = sum / toNumber(n)
+	sum := _values[0]
+	for i := 1; len(_values) > i; i++ {
+		sum = sum / _values[i]
 	}
 
 	return sum


Test patch:
diff --git a/helpers_test.go b/helpers_test.go
index 665467b..2eb07ba 100644
--- a/helpers_test.go
+++ b/helpers_test.go
@@ -13,3 +13,10 @@ func TestHelperIsTrue(t *testing.T) {
 func TestToString(t *testing.T) {
 	assert.Equal(t, "", toString(nil))
 }
+
+func TestToSliceOfNumbers(t *testing.T) {
+	json_parsed := []interface{}{"0.0", "-10.0"}
+	input := interface{}(json_parsed)
+	expected := []float64{0.0, -10.0}
+	assert.Equal(t, expected, toSliceOfNumbers(input))
+}
diff --git a/jsonlogic_test.go b/jsonlogic_test.go
index 4375244..e7eee3c 100644
--- a/jsonlogic_test.go
+++ b/jsonlogic_test.go
@@ -741,21 +741,3 @@ func TestIssue58_example(t *testing.T) {
 	expected := `{"foo":"is_bar","path":"foo_is_bar"}`
 	assert.JSONEq(t, expected, result.String())
 }
-
-func TestSubOperation(t *testing.T) {
-	var rule json.RawMessage = json.RawMessage(`{
-		"-": [
-			0,
-			10
-		]
-	}`)
-
-	var expected json.RawMessage = json.RawMessage("-10")
-
-	output, err := ApplyRaw(rule, nil)
-	if err != nil {
-		t.Fatal(err)
-	}
-
-	assert.JSONEq(t, string(expected), string(output))
-}
diff --git a/math_test.go b/math_test.go
new file mode 100644
index 0000000..27ccb65
--- /dev/null
+++ b/math_test.go
@@ -0,0 +1,40 @@
+package jsonlogic
+
+import (
+	"encoding/json"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestMinusWithOneNumber(t *testing.T) {
+	json_parsed := []interface{}{"-10.0"}
+	input := interface{}(json_parsed)
+	expected := -10.0
+	assert.Equal(t, expected, minus(input))
+}
+
+func TestDivWithOneNumber(t *testing.T) {
+	json_parsed := []interface{}{"2.0"}
+	input := interface{}(json_parsed)
+	expected := 2.0
+	assert.Equal(t, expected, minus(input))
+}
+
+func TestSubOperation(t *testing.T) {
+	var rule json.RawMessage = json.RawMessage(`{
+		"-": [
+			0,
+			10
+		]
+	}`)
+
+	var expected json.RawMessage = json.RawMessage("-10")
+
+	output, err := ApplyRaw(rule, nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	assert.JSONEq(t, string(expected), string(output))
+}