mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
common: Replace StringDataCompare with slices.Contains and cleanup string funcs (#1631)
* common: Replace StringDataCompare with slices.Contains and cleanup string funcs * common/docs: Update SliceDifference and remove outdated steps from ADD_NEW_EXCHANGE.md * common: Improve SliceDifference
This commit is contained in:
103
common/common.go
103
common/common.go
@@ -13,7 +13,6 @@ import (
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
@@ -136,66 +135,43 @@ func NewHTTPClientWithTimeout(t time.Duration) *http.Client {
|
||||
return h
|
||||
}
|
||||
|
||||
// StringSliceDifference concatenates slices together based on its index and
|
||||
// returns an individual string array
|
||||
func StringSliceDifference(slice1, slice2 []string) []string {
|
||||
var diff []string
|
||||
for i := range 2 {
|
||||
for _, s1 := range slice1 {
|
||||
found := false
|
||||
for _, s2 := range slice2 {
|
||||
if s1 == s2 {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
diff = append(diff, s1)
|
||||
}
|
||||
}
|
||||
if i == 0 {
|
||||
slice1, slice2 = slice2, slice1
|
||||
// SliceDifference returns the elements that are in slice1 or slice2 but not in both
|
||||
func SliceDifference[T comparable](slice1, slice2 []T) []T {
|
||||
diff := make([]T, 0, len(slice1)+len(slice2))
|
||||
for x := range slice1 {
|
||||
if !slices.Contains(slice2, slice1[x]) {
|
||||
diff = append(diff, slice1[x])
|
||||
continue
|
||||
}
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
// StringDataContains checks the substring array with an input and returns a bool
|
||||
func StringDataContains(haystack []string, needle string) bool {
|
||||
data := strings.Join(haystack, ",")
|
||||
return strings.Contains(data, needle)
|
||||
}
|
||||
|
||||
// StringDataCompare data checks the substring array with an input and returns a bool
|
||||
func StringDataCompare(haystack []string, needle string) bool {
|
||||
for x := range haystack {
|
||||
if haystack[x] == needle {
|
||||
return true
|
||||
for x := range slice2 {
|
||||
if !slices.Contains(slice1, slice2[x]) {
|
||||
diff = append(diff, slice2[x])
|
||||
}
|
||||
}
|
||||
return false
|
||||
return slices.Clip(diff)
|
||||
}
|
||||
|
||||
// StringDataCompareInsensitive data checks the substring array with an input and returns
|
||||
// a bool irrespective of lower or upper case strings
|
||||
func StringDataCompareInsensitive(haystack []string, needle string) bool {
|
||||
for x := range haystack {
|
||||
if strings.EqualFold(haystack[x], needle) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
// StringSliceContains returns whether case sensitive needle is contained within haystack
|
||||
func StringSliceContains(haystack []string, needle string) bool {
|
||||
return slices.ContainsFunc(haystack, func(s string) bool {
|
||||
return strings.Contains(s, needle)
|
||||
})
|
||||
}
|
||||
|
||||
// StringDataContainsInsensitive checks the substring array with an input and returns
|
||||
// a bool irrespective of lower or upper case strings
|
||||
func StringDataContainsInsensitive(haystack []string, needle string) bool {
|
||||
for _, data := range haystack {
|
||||
if strings.Contains(strings.ToUpper(data), strings.ToUpper(needle)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
// StringSliceCompareInsensitive returns whether case insensitive needle exists within haystack
|
||||
func StringSliceCompareInsensitive(haystack []string, needle string) bool {
|
||||
return slices.ContainsFunc(haystack, func(s string) bool {
|
||||
return strings.EqualFold(s, needle)
|
||||
})
|
||||
}
|
||||
|
||||
// StringSliceContainsInsensitive returns whether case insensitive needle is contained within haystack
|
||||
func StringSliceContainsInsensitive(haystack []string, needle string) bool {
|
||||
needleUpper := strings.ToUpper(needle)
|
||||
return slices.ContainsFunc(haystack, func(s string) bool {
|
||||
return strings.Contains(strings.ToUpper(s), needleUpper)
|
||||
})
|
||||
}
|
||||
|
||||
// IsEnabled takes in a boolean param and returns a string if it is enabled
|
||||
@@ -422,27 +398,6 @@ func AddPaddingOnUpperCase(s string) string {
|
||||
return strings.Join(result, " ")
|
||||
}
|
||||
|
||||
// InArray checks if _val_ belongs to _array_
|
||||
func InArray(val, array interface{}) (exists bool, index int) {
|
||||
exists = false
|
||||
index = -1
|
||||
if array == nil {
|
||||
return
|
||||
}
|
||||
switch reflect.TypeOf(array).Kind() {
|
||||
case reflect.Array, reflect.Slice:
|
||||
s := reflect.ValueOf(array)
|
||||
for i := range s.Len() {
|
||||
if reflect.DeepEqual(val, s.Index(i).Interface()) {
|
||||
index = i
|
||||
exists = true
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// fmtError holds a formatted msg and the errors which formatted it
|
||||
type fmtError struct {
|
||||
errs []error
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -236,85 +235,41 @@ func TestIsValidCryptoAddress(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringSliceDifference(t *testing.T) {
|
||||
func TestSliceDifference(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInputOne := []string{"hello"}
|
||||
originalInputTwo := []string{"hello", "moto"}
|
||||
expectedOutput := []string{"hello moto"}
|
||||
actualResult := StringSliceDifference(originalInputOne, originalInputTwo)
|
||||
if reflect.DeepEqual(expectedOutput, actualResult) {
|
||||
t.Errorf("Expected '%s'. Actual '%s'",
|
||||
expectedOutput, actualResult)
|
||||
|
||||
assert.ElementsMatch(t, []string{"world", "go"}, SliceDifference([]string{"hello", "world"}, []string{"hello", "go"}))
|
||||
assert.ElementsMatch(t, []int64{1, 2, 5, 6}, SliceDifference([]int64{1, 2, 3, 4}, []int64{3, 4, 5, 6}))
|
||||
assert.ElementsMatch(t, []float64{1.1, 4.4}, SliceDifference([]float64{1.1, 2.2, 3.3}, []float64{2.2, 3.3, 4.4}))
|
||||
type mixedType struct {
|
||||
A string
|
||||
B int
|
||||
}
|
||||
assert.ElementsMatch(t, []mixedType{{"A", 1}, {"D", 4}}, SliceDifference([]mixedType{{"A", 1}, {"B", 2}, {"C", 3}}, []mixedType{{"B", 2}, {"C", 3}, {"D", 4}}))
|
||||
assert.ElementsMatch(t, []int{1, 2, 3}, SliceDifference([]int{}, []int{1, 2, 3}))
|
||||
assert.ElementsMatch(t, []int{1, 2, 3}, SliceDifference([]int{1, 2, 3}, []int{}))
|
||||
assert.Empty(t, SliceDifference([]int{}, []int{}))
|
||||
}
|
||||
|
||||
func TestStringDataContains(t *testing.T) {
|
||||
func TestStringSliceContains(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalHaystack := []string{"hello", "world", "USDT", "Contains", "string"}
|
||||
originalNeedle := "USD"
|
||||
anotherNeedle := "thing"
|
||||
actualResult := StringDataContains(originalHaystack, originalNeedle)
|
||||
if expectedOutput := true; actualResult != expectedOutput {
|
||||
t.Errorf("Expected '%v'. Actual '%v'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
actualResult = StringDataContains(originalHaystack, anotherNeedle)
|
||||
if expectedOutput := false; actualResult != expectedOutput {
|
||||
t.Errorf("Expected '%v'. Actual '%v'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
assert.True(t, StringSliceContains(originalHaystack, "USD"), "Should contain 'USD'")
|
||||
assert.False(t, StringSliceContains(originalHaystack, "thing"), "Should not contain 'thing'")
|
||||
}
|
||||
|
||||
func TestStringDataCompare(t *testing.T) {
|
||||
func TestStringSliceCompareInsensitive(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalHaystack := []string{"hello", "WoRld", "USDT", "Contains", "string"}
|
||||
originalNeedle := "WoRld"
|
||||
anotherNeedle := "USD"
|
||||
actualResult := StringDataCompare(originalHaystack, originalNeedle)
|
||||
if expectedOutput := true; actualResult != expectedOutput {
|
||||
t.Errorf("Expected '%v'. Actual '%v'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
actualResult = StringDataCompare(originalHaystack, anotherNeedle)
|
||||
if expectedOutput := false; actualResult != expectedOutput {
|
||||
t.Errorf("Expected '%v'. Actual '%v'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
assert.False(t, StringSliceCompareInsensitive(originalHaystack, "USD"), "Should not contain 'USD'")
|
||||
assert.True(t, StringSliceCompareInsensitive(originalHaystack, "WORLD"), "Should find 'WoRld'")
|
||||
}
|
||||
|
||||
func TestStringDataCompareUpper(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalHaystack := []string{"hello", "WoRld", "USDT", "Contains", "string"}
|
||||
originalNeedle := "WoRld"
|
||||
anotherNeedle := "WoRldD"
|
||||
actualResult := StringDataCompareInsensitive(originalHaystack, originalNeedle)
|
||||
if expectedOutput := true; actualResult != expectedOutput {
|
||||
t.Errorf("Expected '%v'. Actual '%v'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
|
||||
actualResult = StringDataCompareInsensitive(originalHaystack, anotherNeedle)
|
||||
if expectedOutput := false; actualResult != expectedOutput {
|
||||
t.Errorf("Expected '%v'. Actual '%v'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStringDataContainsUpper(t *testing.T) {
|
||||
func TestStringSliceContainsInsensitive(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalHaystack := []string{"bLa", "BrO", "sUp"}
|
||||
originalNeedle := "Bla"
|
||||
anotherNeedle := "ning"
|
||||
actualResult := StringDataContainsInsensitive(originalHaystack, originalNeedle)
|
||||
if expectedOutput := true; actualResult != expectedOutput {
|
||||
t.Errorf("Expected '%v'. Actual '%v'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
actualResult = StringDataContainsInsensitive(originalHaystack, anotherNeedle)
|
||||
if expectedOutput := false; actualResult != expectedOutput {
|
||||
t.Errorf("Expected '%v'. Actual '%v'",
|
||||
expectedOutput, actualResult)
|
||||
}
|
||||
assert.True(t, StringSliceContainsInsensitive(originalHaystack, "Bla"), "Should contain 'Bla'")
|
||||
assert.False(t, StringSliceContainsInsensitive(originalHaystack, "ning"), "Should not contain 'ning'")
|
||||
}
|
||||
|
||||
func TestYesOrNo(t *testing.T) {
|
||||
@@ -589,45 +544,6 @@ func TestAddPaddingOnUpperCase(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInArray(t *testing.T) {
|
||||
t.Parallel()
|
||||
InArray(nil, nil)
|
||||
|
||||
array := [6]int{2, 3, 5, 7, 11, 13}
|
||||
isIn, pos := InArray(5, array)
|
||||
if !isIn {
|
||||
t.Errorf("failed to find the value within the array")
|
||||
}
|
||||
if pos != 2 {
|
||||
t.Errorf("failed return the correct position of the value in the array")
|
||||
}
|
||||
isIn, _ = InArray(1, array)
|
||||
if isIn {
|
||||
t.Errorf("found a non existent value in the array")
|
||||
}
|
||||
|
||||
slice := make([]int, 0)
|
||||
slice = append(append(slice, 5), 3)
|
||||
isIn, pos = InArray(5, slice)
|
||||
if !isIn {
|
||||
t.Errorf("failed to find the value within the slice")
|
||||
}
|
||||
if pos != 0 {
|
||||
t.Errorf("failed return the correct position of the value in the slice")
|
||||
}
|
||||
isIn, pos = InArray(3, slice)
|
||||
if !isIn {
|
||||
t.Errorf("failed to find the value within the slice")
|
||||
}
|
||||
if pos != 1 {
|
||||
t.Errorf("failed return the correct position of the value in the slice")
|
||||
}
|
||||
isIn, _ = InArray(1, slice)
|
||||
if isIn {
|
||||
t.Errorf("found a non existent value in the slice")
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrors(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user