diff --git a/core/util/utils.go b/core/util/utils.go index 71bbac6..88d6e1e 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,12 @@ func GetCurrencySymbol(code string) string { return strings.ToUpper(code) } +// IsLHSCurrency determines whether the currency is left-hand side. +// It returns false if currency symbol cannot be found. +func IsLHSCurrency(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..7bd7a44 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_IsLHSCurrency(t *testing.T) { + for code := range DefaultCurrencies() { + if slices.Contains(LHSCurrencies(), strings.ToLower(code)) { + assert.True(t, IsLHSCurrency(code)) + continue + } + + assert.False(t, IsLHSCurrency(code)) + } + + assert.False(t, IsLHSCurrency("extra_code")) +} + func TestUtils_ReplaceMarkdownSymbols(t *testing.T) { test := "this *is* _test_ `string` [markdown" expected := "this \\*is\\* \\_test\\_ \\`string\\` \\[markdown"