From 0fea98857c490c47c7b02a2d6af4146c3285b4e4 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Mon, 21 Aug 2017 14:50:22 +1000 Subject: [PATCH] Add common StringReplace function --- common/common.go | 5 +++++ common/common_test.go | 22 ++++++++++++++++++++++ exchanges/bittrex/bittrex_wrapper.go | 4 ++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/common/common.go b/common/common.go index 2921454f..1560e5e3 100644 --- a/common/common.go +++ b/common/common.go @@ -159,6 +159,11 @@ func TrimString(input, cutset string) string { return strings.Trim(input, cutset) } +// ReplaceString replaces a string with another +func ReplaceString(input, old, new string, n int) string { + return strings.Replace(input, old, new, n) +} + // StringToUpper changes strings to uppercase func StringToUpper(input string) string { return strings.ToUpper(input) diff --git a/common/common_test.go b/common/common_test.go index 6c5eca04..4422ceef 100644 --- a/common/common_test.go +++ b/common/common_test.go @@ -310,6 +310,28 @@ func TestTrimString(t *testing.T) { } } +// ReplaceString replaces a string with another +func TestReplaceString(t *testing.T) { + t.Parallel() + currency := "BTC-USD" + expectedOutput := "BTCUSD" + + actualResult := ReplaceString(currency, "-", "", -1) + if expectedOutput != actualResult { + t.Errorf( + "Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult, + ) + } + + currency = "BTC-USD--" + actualResult = ReplaceString(currency, "-", "", 3) + if expectedOutput != actualResult { + t.Errorf( + "Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult, + ) + } +} + func TestRoundFloat(t *testing.T) { t.Parallel() originalInput := float64(1.4545445445) diff --git a/exchanges/bittrex/bittrex_wrapper.go b/exchanges/bittrex/bittrex_wrapper.go index 923acbf6..f2f01c42 100644 --- a/exchanges/bittrex/bittrex_wrapper.go +++ b/exchanges/bittrex/bittrex_wrapper.go @@ -2,9 +2,9 @@ package bittrex import ( "log" - "strings" "time" + "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" @@ -34,7 +34,7 @@ func (b *Bittrex) Run() { continue } currencies = append(currencies, - strings.Replace(exchangeProducts[x].MarketName, "-", "", -1)) + common.ReplaceString(exchangeProducts[x].MarketName, "-", "", -1)) } err = b.UpdateAvailableCurrencies(currencies) if err != nil {