From 57420046457c945d5ab9531aa4113deddad91235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=83=D1=85=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=BB=D0=B0?= Date: Thu, 6 Feb 2025 13:03:46 +0300 Subject: [PATCH] left-hand side currencies --- core/util/utils.go | 15 +++++++++++++++ core/util/utils_test.go | 14 ++++++++++++++ 2 files changed, 29 insertions(+) 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"