diff --git a/currency.go b/currency.go index b8022fd..488f871 100644 --- a/currency.go +++ b/currency.go @@ -1,4 +1,4 @@ -package currency +package dough import ( "math" @@ -7,15 +7,15 @@ import ( "strings" ) -// FormattedStringToUint : returns a uint from a string value -func FormattedStringToUint(num string, alpha string) (uint, error) { +// StringToInt : returns a uint from a string value +func StringToInt(num string, alpha string) (int, error) { ISO, err := GetISOFromAlpha(alpha) if err != nil { return 0, err } // Find all numbers and a decimal - reg := regexp.MustCompile("[0-9" + ISO.Decimal + "]+") + reg := regexp.MustCompile("[-0-9" + ISO.Decimal + "]+") strArray := reg.FindAllString(num, -1) str := strings.Join(strArray, "") @@ -29,16 +29,7 @@ func FormattedStringToUint(num string, alpha string) (uint, error) { } // Return a mulitple of the fraction to give us our uint - return uint(fl * math.Pow10(ISO.Fraction)), nil -} - -// PlainStringToInt returns a int64 from a purely numerical string -func PlainStringToInt(str string, alpha string) (int64, error) { - _, err := GetISOFromAlpha(alpha) - if err != nil { - return 0, err - } - return strconv.ParseInt(str, 10, 64) + return int(fl * math.Pow10(ISO.Fraction)), nil } // DisplayFull : returns a string with full currency formatting... "num" being the amount, "alpha" being the ISO three digit alphabetic code. diff --git a/currency_list.go b/currency_list.go index 4d0367a..692016a 100644 --- a/currency_list.go +++ b/currency_list.go @@ -1,4 +1,4 @@ -package currency +package dough // Currency - struct containing currency variables type Currency struct { diff --git a/currency_test.go b/currency_test.go index 2a2f510..ff4ab3b 100644 --- a/currency_test.go +++ b/currency_test.go @@ -1,11 +1,16 @@ -package currency +package dough import ( "reflect" "testing" ) -var TestFormattedStringToUintData = []struct { +func TestCurrencyCount(t *testing.T) { + num := len(CurrencyList) + t.Log("Currency Count: ", num) +} + +var TestStringToIntData = []struct { Num string Alpha string Output interface{} @@ -14,59 +19,32 @@ var TestFormattedStringToUintData = []struct { {" ", "USD", ErrorUnableToFormatCurrencyFromString.Error()}, {"abcd", "USD", ErrorUnableToFormatCurrencyFromString.Error()}, {"$5", "USA", ErrorInvalidISO.Error()}, - {"$5", "USD", uint(500)}, - {"$500", "USD", uint(50000)}, - {"$05", "USD", uint(500)}, - {"$0.05", "USD", uint(5)}, - {"$5.0", "USD", uint(500)}, - {"$5.52", "USD", uint(552)}, - {"$0.00", "USD", uint(0)}, - {"$0.01", "USD", uint(1)}, - {"$0.10", "USD", uint(10)}, - {"$1.00", "USD", uint(100)}, - {"$10.00", "USD", uint(1000)}, - {"$100.00", "USD", uint(10000)}, - {"$1,000.00", "USD", uint(100000)}, - {"$10,000.00", "USD", uint(1000000)}, - {"$100,000.00", "USD", uint(10000000)}, - {"$1,000,000.00", "USD", uint(100000000)}, + {"$5", "USD", 500}, + {"$500", "USD", 50000}, + {"-500", "USD", -50000}, + {"$05", "USD", 500}, + {"$0.05", "USD", 5}, + {"$5.0", "USD", 500}, + {"$5.52", "USD", 552}, + {"$0.00", "USD", 0}, + {"$0.01", "USD", 1}, + {"$0.10", "USD", 10}, + {"$1.00", "USD", 100}, + {"$10.00", "USD", 1000}, + {"$100.00", "USD", 10000}, + {"$1,000.00", "USD", 100000}, + {"$10,000.00", "USD", 1000000}, + {"$100,000.00", "USD", 10000000}, + {"$1,000,000.00", "USD", 100000000}, // Non USD - {"$100.00,00", "ARS", uint(1000000)}, - {"$10,000,000", "JPY", uint(10000000)}, -} - -func TestFormattedStringToUint(t *testing.T) { - for _, v := range TestFormattedStringToUintData { - result, err := FormattedStringToUint(v.Num, v.Alpha) - if err != nil { - if err.Error() != v.Output { - t.Error("input: ", v.Num, " error: ", err) - } - } else if result != v.Output { - t.Error("got: ", result, " expected: ", v.Output) - } - } -} - -var TestPlainStringToIntData = []struct { - Num string - Alpha string - Output interface{} -}{ - {"0", "USA", ErrorInvalidISO.Error()}, - {"0", "USD", int64(0)}, - {"1", "USD", int64(1)}, - {"10", "USD", int64(10)}, - {"100", "USD", int64(100)}, - {"1000", "USD", int64(1000)}, - {"10000", "USD", int64(10000)}, - {"100000", "USD", int64(100000)}, + {"$100.00,00", "ARS", 1000000}, + {"$10,000,000", "JPY", 10000000}, } func TestPlainStringToInt(t *testing.T) { - for _, v := range TestPlainStringToIntData { - result, err := PlainStringToInt(v.Num, v.Alpha) + for _, v := range TestStringToIntData { + result, err := StringToInt(v.Num, v.Alpha) if err != nil { if err.Error() != v.Output { t.Error(err.Error()) diff --git a/error.go b/error.go index a721aab..b5c995e 100644 --- a/error.go +++ b/error.go @@ -1,4 +1,4 @@ -package currency +package dough import "errors" diff --git a/function.go b/function.go index cae1f80..6d88cbe 100644 --- a/function.go +++ b/function.go @@ -1,4 +1,4 @@ -package currency +package dough import ( "fmt" diff --git a/function_test.go b/function_test.go index 9efd7c4..82bfbe8 100644 --- a/function_test.go +++ b/function_test.go @@ -1,4 +1,4 @@ -package currency +package dough import "testing" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ff618ef --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/fluidpay/dough + +go 1.12