Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new initialization function - NewFromFormattedString #184

Merged
merged 2 commits into from Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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