diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9cf44a8 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +test: + go test -v -race ./... \ No newline at end of file diff --git a/currency.go b/currency.go index 3a1f076..2a38d2c 100644 --- a/currency.go +++ b/currency.go @@ -15,8 +15,37 @@ type Currency struct { Thousand string } +type Currencies map[string]*Currency + +// CurrencyByNumericCode returns the currency given the numeric code defined in ISO-4271. +func (c Currencies) CurrencyByNumericCode(code string) *Currency { + for _, sc := range c { + if sc.NumericCode == code { + return sc + } + } + + return nil +} + +// CurrencyByCode returns the currency given the currency code defined as a constant. +func (c Currencies) CurrencyByCode(code string) *Currency { + sc, ok := c[code] + if !ok { + return nil + } + + return sc +} + +// Add updates currencies list by adding a given Currency to it. +func (c Currencies) Add(currency *Currency) Currencies { + c[currency.Code] = currency + return c +} + // currencies represents a collection of currency. -var currencies = map[string]*Currency{ +var currencies = Currencies{ AED: {Decimal: ".", Thousand: ",", Code: AED, Fraction: 2, NumericCode: "784", Grapheme: ".\u062f.\u0625", Template: "1 $"}, AFN: {Decimal: ".", Thousand: ",", Code: AFN, Fraction: 2, NumericCode: "971", Grapheme: "\u060b", Template: "1 $"}, ALL: {Decimal: ".", Thousand: ",", Code: ALL, Fraction: 2, NumericCode: "008", Grapheme: "L", Template: "$1"}, @@ -190,7 +219,7 @@ var currencies = map[string]*Currency{ // AddCurrency lets you insert or update currency in currencies list. func AddCurrency(code, Grapheme, Template, Decimal, Thousand string, Fraction int) *Currency { - currencies[code] = &Currency{ + c := Currency{ Code: code, Grapheme: Grapheme, Template: Template, @@ -198,8 +227,8 @@ func AddCurrency(code, Grapheme, Template, Decimal, Thousand string, Fraction in Thousand: Thousand, Fraction: Fraction, } - - return currencies[code] + currencies.Add(&c) + return &c } func newCurrency(code string) *Currency { @@ -208,7 +237,7 @@ func newCurrency(code string) *Currency { // GetCurrency returns the currency given the code. func GetCurrency(code string) *Currency { - return currencies[code] + return currencies.CurrencyByCode(code) } // Formatter returns currency formatter representing diff --git a/currency_test.go b/currency_test.go index 2d748d8..324ecd5 100644 --- a/currency_test.go +++ b/currency_test.go @@ -88,3 +88,52 @@ func TestCurrency_GetNonExistingCurrency(t *testing.T) { t.Errorf("Unexpected currency returned %+v", currency) } } + +func TestCurrencies(t *testing.T) { + const currencyFooCode = "FOO" + const currencyFooNumericCode = "1234" + curFoo := &Currency{ + Code: currencyFooCode, + NumericCode: currencyFooNumericCode, + Fraction: 10, + Grapheme: "1", + Template: "2", + Decimal: "3", + Thousand: "4", + } + var cs = Currencies{ + currencyFooCode: curFoo, + } + const currencyBarCode = "BAR" + const currencyBarNumericCode = "4321" + curBar := &Currency{ + Code: currencyBarCode, + NumericCode: currencyBarNumericCode, + Fraction: 1, + Grapheme: "2", + Template: "3", + Decimal: "4", + Thousand: "5", + } + cs = cs.Add(curBar) + + ac := cs.CurrencyByCode(currencyFooCode) + if !curFoo.equals(ac) { + t.Errorf("unexpected currency returned. expected: %v, got %v", curFoo, ac) + } + + ac = cs.CurrencyByNumericCode(currencyFooNumericCode) + if !curFoo.equals(ac) { + t.Errorf("unexpected currency returned. expected: %v, got %v", curFoo, ac) + } + + ac = cs.CurrencyByCode(currencyBarCode) + if !curBar.equals(ac) { + t.Errorf("unexpected currency returned. expected: %v, got %v", curBar, ac) + } + + ac = cs.CurrencyByNumericCode(currencyBarNumericCode) + if !curBar.equals(ac) { + t.Errorf("unexpected currency returned. expected: %v, got %v", curBar, ac) + } +}