Skip to content

Commit

Permalink
Implement new initialization function - NewFromFormattedString (#184)
Browse files Browse the repository at this point in the history
* Implement new initialization function - NewFromFormattedString
* Redefine docstring
  • Loading branch information
mwoss committed Sep 14, 2020
1 parent 5a22b86 commit ebbf8bb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
25 changes: 25 additions & 0 deletions decimal.go
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"math"
"math/big"
"regexp"
"strconv"
"strings"
)
Expand Down Expand Up @@ -176,6 +177,30 @@ func NewFromString(value string) (Decimal, error) {
}, nil
}

// NewFromFormattedString returns a new Decimal from a formatted string representation.
// The second argument - replRegexp, is a regular expression that is used to find characters that should be
// removed from given decimal string representation. All matched characters will be replaced with an empty string.
//
// Example:
//
// r := regexp.MustCompile("[$,]")
// d1, err := NewFromFormattedString("$5,125.99", r)
//
// r2 := regexp.MustCompile("[_]")
// d2, err := NewFromFormattedString("1_000_000", r2)
//
// r3 := regexp.MustCompile("[USD\\s]")
// d3, err := NewFromFormattedString("5000 USD", r3)
//
func NewFromFormattedString(value string, replRegexp *regexp.Regexp) (Decimal, error) {
parsedValue := replRegexp.ReplaceAllString(value, "")
d, err := NewFromString(parsedValue)
if err != nil {
return Decimal{}, err
}
return d, nil
}

// RequireFromString returns a new Decimal from a string representation
// or panics if NewFromString would have returned an error.
//
Expand Down
35 changes: 35 additions & 0 deletions decimal_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"math/big"
"math/rand"
"reflect"
"regexp"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -240,6 +241,35 @@ func TestNewFromString(t *testing.T) {
}
}

func TestNewFromFormattedString(t *testing.T) {
for _, testCase := range []struct {
Formatted string
Expected string
ReplRegex *regexp.Regexp
}{
{"$10.99", "10.99", regexp.MustCompile("[$]")},
{"$ 12.1", "12.1", regexp.MustCompile("[$\\s]")},
{"$61,690.99", "61690.99", regexp.MustCompile("[$,]")},
{"1_000_000.00", "1000000.00", regexp.MustCompile("[_]")},
{"41,410.00", "41410.00", regexp.MustCompile("[,]")},
{"5200 USD", "5200", regexp.MustCompile("[USD\\s]")},
} {
dFormatted, err := NewFromFormattedString(testCase.Formatted, testCase.ReplRegex)
if err != nil {
t.Fatal(err)
}

dExact, err := NewFromString(testCase.Expected)
if err != nil {
t.Fatal(err)
}

if !dFormatted.Equal(dExact) {
t.Errorf("expect %s, got %s", dExact, dFormatted)
}
}
}

func TestFloat64(t *testing.T) {
for _, x := range testTable {
if x.inexact == "" || x.inexact == "-" {
Expand Down Expand Up @@ -294,6 +324,11 @@ func TestNewFromStringErrs(t *testing.T) {
"123.456Easdf",
"123.456e" + strconv.FormatInt(math.MinInt64, 10),
"123.456e" + strconv.FormatInt(math.MinInt32, 10),
"512.99 USD",
"$99.99",
"51,850.00",
"20_000_000.00",
"$20_000_000.00",
}

for _, s := range tests {
Expand Down

0 comments on commit ebbf8bb

Please sign in to comment.