Skip to content

Commit

Permalink
Get Currency by Numeric Code (Rhymond#104)
Browse files Browse the repository at this point in the history
* find currency by code

* get currency by code
  • Loading branch information
Rhymond authored Mar 31, 2022
1 parent 940609b commit e8fc0e2
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
go test -v -race ./...
39 changes: 34 additions & 5 deletions currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -190,16 +219,16 @@ 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,
Decimal: Decimal,
Thousand: Thousand,
Fraction: Fraction,
}

return currencies[code]
currencies.Add(&c)
return &c
}

func newCurrency(code string) *Currency {
Expand All @@ -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
Expand Down
49 changes: 49 additions & 0 deletions currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit e8fc0e2

Please sign in to comment.