Skip to content

Commit

Permalink
Finish v0.1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
bvoelker committed May 10, 2019
2 parents 40a5b29 + c03c35f commit b0b0339
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 68 deletions.
19 changes: 5 additions & 14 deletions currency.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package currency
package dough

import (
"math"
Expand All @@ -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, "")

Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion currency_list.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package currency
package dough

// Currency - struct containing currency variables
type Currency struct {
Expand Down
78 changes: 28 additions & 50 deletions currency_test.go
Original file line number Diff line number Diff line change
@@ -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{}
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package currency
package dough

import "errors"

Expand Down
2 changes: 1 addition & 1 deletion function.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package currency
package dough

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion function_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package currency
package dough

import "testing"

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/fluidpay/dough

go 1.12

0 comments on commit b0b0339

Please sign in to comment.