Skip to content

Commit

Permalink
Merge pull request #288 from nyaruka/num_parse_fix
Browse files Browse the repository at this point in the history
Parse numbers from strings like "24ans"
  • Loading branch information
rowanseymour authored May 3, 2018
2 parents 4b0ac06 + 6774c0d commit bab80e6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
11 changes: 1 addition & 10 deletions excellent/types/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"math"
"strings"

"github.com/nyaruka/goflow/utils"

Expand Down Expand Up @@ -95,7 +94,7 @@ func ToXNumber(x XValue) (XNumber, XError) {
case XNumber:
return typed, nil
case XText:
parsed, err := parseDecimalFuzzy(typed.Native())
parsed, err := decimal.NewFromString(typed.Native())
if err == nil {
return NewXNumber(parsed), nil
}
Expand All @@ -119,11 +118,3 @@ func ToInteger(x XValue) (int, XError) {

return int(intPart), nil
}

func parseDecimalFuzzy(val string) (decimal.Decimal, error) {
// common SMS foibles
val = strings.ToLower(val)
val = strings.Replace(val, "o", "0", -1)
val = strings.Replace(val, "l", "1", -1)
return decimal.NewFromString(val)
}
1 change: 0 additions & 1 deletion excellent/types/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func TestToXNumberAndInteger(t *testing.T) {
{types.NewXErrorf("Error"), types.XNumberZero, 0, true},
{types.NewXNumberFromInt(123), types.NewXNumberFromInt(123), 123, false},
{types.NewXText("15.5"), types.RequireXNumberFromString("15.5"), 15, false},
{types.NewXText("lO.5"), types.RequireXNumberFromString("10.5"), 10, false},
{types.NewXText("12345678901234567890"), types.RequireXNumberFromString("12345678901234567890"), 0, true}, // out of int range
{NewTestXObject("Hello", 123), types.XNumberZero, 0, true},
{NewTestXObject("123.45000", 123), types.RequireXNumberFromString("123.45"), 123, false},
Expand Down
35 changes: 30 additions & 5 deletions flows/routers/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,9 @@ func HasNumberBetween(env utils.Environment, arg1 types.XValue, arg2 types.XValu

// for each of our values, try to evaluate to a decimal
for _, value := range strings.Fields(str.Native()) {
num, xerr := types.ToXNumber(types.NewXText(value))
if xerr == nil {
parsed, err := parseDecimalFuzzy(value)
if err == nil {
num := types.NewXNumber(parsed)
if num.Compare(min) >= 0 && num.Compare(max) <= 0 {
return XTestResult{true, num}
}
Expand Down Expand Up @@ -851,15 +852,39 @@ func hasOnlyPhraseTest(origHays []string, hays []string, pins []string) XTestRes
// Numerical Test Functions
//------------------------------------------------------------------------------------------

var parseableNumberRegex = regexp.MustCompile(`^[$£€]?([\d,][\d,\.]*([\.,]\d+)?)\D*$`)

func parseDecimalFuzzy(val string) (decimal.Decimal, error) {
// common SMS foibles
cleaned := strings.ToLower(val)
cleaned = strings.Replace(cleaned, "o", "0", -1)
cleaned = strings.Replace(cleaned, "l", "1", -1)

num, err := decimal.NewFromString(cleaned)
if err == nil {
return num, nil
}

// we only try this hard if we haven't already substituted characters
if cleaned == val {
// does this start with a number? just use that part if so
match := parseableNumberRegex.FindStringSubmatch(val)
if match != nil {
return decimal.NewFromString(match[1])
}
}
return decimal.Zero, err
}

type decimalTest func(value decimal.Decimal, test decimal.Decimal) bool

func testNumber(env utils.Environment, str types.XText, testNum types.XNumber, testFunc decimalTest) types.XValue {
// for each of our values, try to evaluate to a decimal
for _, value := range strings.Fields(str.Native()) {
num, xerr := types.ToXNumber(types.NewXText(value))
num, xerr := parseDecimalFuzzy(value)
if xerr == nil {
if testFunc(num.Native(), testNum.Native()) {
return XTestResult{true, num}
if testFunc(num, testNum.Native()) {
return XTestResult{true, types.NewXNumber(num)}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions flows/routers/tests/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ var testTests = []struct {
{"has_beginning", []types.XValue{xs("one"), xs("two"), xs("three")}, false, nil, true},

{"has_number", []types.XValue{xs("the number 10")}, true, xn("10"), false},
{"has_number", []types.XValue{xs("24ans")}, true, xn("24"), false},
{"has_number", []types.XValue{xs("the number 1o")}, true, xn("10"), false},
{"has_number", []types.XValue{xs("the number lo")}, true, xn("10"), false},
{"has_number", []types.XValue{xs("another is -12.51")}, true, xn("-12.51"), false},
Expand Down Expand Up @@ -145,6 +146,7 @@ var testTests = []struct {
{"has_number_gt", []types.XValue{xs("one"), xs("two"), xs("three")}, false, nil, true},

{"has_number_between", []types.XValue{xs("the number 10"), xs("8"), xs("12")}, true, xn("10"), false},
{"has_number_between", []types.XValue{xs("24ans"), xn("20"), xn("24")}, true, xn("24"), false},
{"has_number_between", []types.XValue{xs("another is -12.51"), xs("-12.51"), xs("-10")}, true, xn("-12.51"), false},
{"has_number_between", []types.XValue{xs("nothing here"), xs("10"), xs("15")}, false, nil, false},
{"has_number_between", []types.XValue{xs("one"), xs("two")}, false, nil, true},
Expand Down

0 comments on commit bab80e6

Please sign in to comment.