-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharrays.go
95 lines (84 loc) · 2.21 KB
/
arrays.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package common
import "strings"
// Unique returns unique array items.
func Unique[T comparable](values []T) []T {
keys := make(map[T]bool)
list := []T{}
for _, value := range values {
if _, ok := keys[value]; !ok {
keys[value] = true
list = append(list, value)
}
}
return list
}
// EqualArrays compares equality of two arrays. Both input variables must be same type.
func EqualArrays[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
// EqualStringArrays compares equality of two string arrays.
func EqualStringArrays(a, b []string) bool {
return EqualArrays(a, b)
}
// Contains returns true if value is found in array. Both input variables must be same type.
func Contains[T comparable](array []T, value T) bool {
for _, currentValue := range array {
if currentValue == value {
return true
}
}
return false
}
// ContainsInteger returns true if integer is found from array.
func ContainsInteger(array []int, value int) bool {
return Contains(array, value)
}
// ContainsString returns true if string is found from array.
func ContainsString(array []string, word string) bool {
return Contains(array, word)
}
// ContainsIgnoreCase returns true if word is found from array. Case of word and words in array is ignored.
func ContainsIgnoreCase(array []string, word string) bool {
return containsF(array, word, strings.EqualFold)
}
func containsF(array []string, word string, f func(item, word string) bool) bool {
for _, item := range array {
if f(item, word) {
return true
}
}
return false
}
// AnyStartsWith ...
func AnyStartsWith(array []string, word string) bool {
for _, item := range array {
if strings.HasPrefix(item, word) {
return true
}
}
return false
}
// GetResultDiff returns array of strings that were desired but missing from results.
func GetResultDiff[T comparable](results []T, desiredResults []T) []T {
missingResults := []T{}
for _, desiredResult := range desiredResults {
found := false
for _, result := range results {
if desiredResult == result {
found = true
}
}
if !found {
missingResults = append(missingResults, desiredResult)
}
}
return missingResults
}