diff --git a/core/util/utils.go b/core/util/utils.go index 71bbac6..75eba2e 100644 --- a/core/util/utils.go +++ b/core/util/utils.go @@ -10,6 +10,7 @@ import ( "io" "net/http" "regexp" + "slices" "strings" "sync/atomic" "time" @@ -45,6 +46,8 @@ var ( } ) +var lHSCurrencies = []string{"pen"} + var defaultCurrencies = map[string]string{ "rub": "₽", "uah": "₴", @@ -271,6 +274,11 @@ func DefaultCurrencies() map[string]string { return defaultCurrencies } +// LHSCurrencies will return left-hand side currencies. +func LHSCurrencies() []string { + return lHSCurrencies +} + // GetCurrencySymbol returns currency symbol by it's ISO 4127 code. // It returns provided currency code in uppercase if currency symbol cannot be found. func GetCurrencySymbol(code string) string { @@ -281,6 +289,13 @@ func GetCurrencySymbol(code string) string { return strings.ToUpper(code) } +// GetCurrencySymbolPosition returns currency symbol position. +// true - left-hand side, +// (default) false - right-hand side. +func GetCurrencySymbolPosition(code string) bool { + return slices.Contains(LHSCurrencies(), strings.ToLower(code)) +} + func FormatCurrencyValue(value float32) string { return fmt.Sprintf("%.2f", value) } diff --git a/core/util/utils_test.go b/core/util/utils_test.go index cc041e9..e57c18d 100644 --- a/core/util/utils_test.go +++ b/core/util/utils_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "net/http" + "slices" "strings" "testing" "time" @@ -320,6 +321,19 @@ func TestUtils_GetCurrencySymbol(t *testing.T) { assert.Equal(t, "MXN", GetCurrencySymbol("mxn")) } +func TestUtils_GetCurrencySymbolPosition(t *testing.T) { + for code := range DefaultCurrencies() { + if slices.Contains(LHSCurrencies(), strings.ToLower(code)) { + assert.True(t, GetCurrencySymbolPosition(code)) + continue + } + + assert.False(t, GetCurrencySymbolPosition(code)) + } + + assert.False(t, GetCurrencySymbolPosition("extra_code")) +} + func TestUtils_ReplaceMarkdownSymbols(t *testing.T) { test := "this *is* _test_ `string` [markdown" expected := "this \\*is\\* \\_test\\_ \\`string\\` \\[markdown"