Skip to content

Commit

Permalink
defaultCurrency implenemted for unknown currencies + ChangeDefaultCur…
Browse files Browse the repository at this point in the history
…rency(), issue Rhymond#69
  • Loading branch information
aliash98 committed Feb 5, 2021
1 parent cec38d4 commit c615b9f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
13 changes: 12 additions & 1 deletion currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ type Currency struct {
Thousand string
}

// default currency
var defaultCurrency = "USD"

// currencies represents a collection of currency.
var currencies = map[string]*Currency{
"AED": {Decimal: ".", Thousand: ",", Code: "AED", Fraction: 2, NumericCode: "784", Grapheme: ".\u062f.\u0625", Template: "1 $"},
Expand Down Expand Up @@ -211,6 +214,14 @@ func GetCurrency(code string) *Currency {
return currencies[code]
}

// ChangeDefaultCurrency changes the default currency
func ChangeDefaultCurrency(code string) {
if _, ok := currencies[code]; ok {
defaultCurrency = code
}
return
}

// Formatter returns currency formatter representing
// used currency structure.
func (c *Currency) Formatter() *Formatter {
Expand All @@ -235,7 +246,7 @@ func (c *Currency) get() *Currency {
return curr
}

return c.getDefault()
return currencies[defaultCurrency]
}

func (c *Currency) equals(oc *Currency) bool {
Expand Down
14 changes: 12 additions & 2 deletions currency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ func TestCurrency_Get1(t *testing.T) {
code := "RANDOM"
c := newCurrency(code).get()

if c.Grapheme != code {
t.Errorf("Expected %s got %s", c.Grapheme, code)
if c.Code != defaultCurrency {
t.Errorf("Expected %s got %s", c.Code, code)
}

}

func TestCurrency_Get2(t *testing.T) {
code := "RANDOM"

ChangeDefaultCurrency("GBP")
c1 := newCurrency(code).get()
if c1.Code != defaultCurrency {
t.Errorf("Expected %s got %s", c1.Code, defaultCurrency)
}
}
func TestCurrency_Equals(t *testing.T) {
tcs := []struct {
code string
Expand Down

0 comments on commit c615b9f

Please sign in to comment.