forked from thoas/go-funk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
presence.go
168 lines (127 loc) · 3.69 KB
/
presence.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package funk
import (
"reflect"
"strings"
)
// Filter iterates over elements of collection, returning an array of
// all elements predicate returns truthy for.
func Filter(arr interface{}, predicate interface{}) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
if !IsFunction(predicate, 1, 1) {
panic("Second argument must be function")
}
funcValue := reflect.ValueOf(predicate)
funcType := funcValue.Type()
if funcType.Out(0).Kind() != reflect.Bool {
panic("Return argument should be a boolean")
}
arrValue := reflect.ValueOf(arr)
arrType := arrValue.Type()
// Get slice type corresponding to array type
resultSliceType := reflect.SliceOf(arrType.Elem())
// MakeSlice takes a slice kind type, and makes a slice.
resultSlice := reflect.MakeSlice(resultSliceType, 0, 0)
for i := 0; i < arrValue.Len(); i++ {
elem := arrValue.Index(i)
result := funcValue.Call([]reflect.Value{elem})[0].Interface().(bool)
if result {
resultSlice = reflect.Append(resultSlice, elem)
}
}
return resultSlice.Interface()
}
// Find iterates over elements of collection, returning the first
// element predicate returns truthy for.
func Find(arr interface{}, predicate interface{}) interface{} {
if !IsIteratee(arr) {
panic("First parameter must be an iteratee")
}
if !IsFunction(predicate, 1, 1) {
panic("Second argument must be function")
}
funcValue := reflect.ValueOf(predicate)
funcType := funcValue.Type()
if funcType.Out(0).Kind() != reflect.Bool {
panic("Return argument should be a boolean")
}
arrValue := reflect.ValueOf(arr)
for i := 0; i < arrValue.Len(); i++ {
elem := arrValue.Index(i)
result := funcValue.Call([]reflect.Value{elem})[0].Interface().(bool)
if result {
return elem.Interface()
}
}
return nil
}
// IndexOf gets the index at which the first occurrence of value is found in array or return -1
// if the value cannot be found
func IndexOf(in interface{}, elem interface{}) int {
inValue := reflect.ValueOf(in)
elemValue := reflect.ValueOf(elem)
inType := inValue.Type()
if inType.Kind() == reflect.String {
return strings.Index(inValue.String(), elemValue.String())
}
if inType.Kind() == reflect.Slice {
for i := 0; i < inValue.Len(); i++ {
if equal(inValue.Index(i).Interface(), elem) {
return i
}
}
}
return -1
}
// LastIndexOf gets the index at which the last occurrence of value is found in array or return -1
// if the value cannot be found
func LastIndexOf(in interface{}, elem interface{}) int {
inValue := reflect.ValueOf(in)
elemValue := reflect.ValueOf(elem)
inType := inValue.Type()
if inType.Kind() == reflect.String {
return strings.LastIndex(inValue.String(), elemValue.String())
}
if inType.Kind() == reflect.Slice {
length := inValue.Len()
for i := length - 1; i >= 0; i-- {
if equal(inValue.Index(i).Interface(), elem) {
return i
}
}
}
return -1
}
// Contains returns true if an element is present in a iteratee.
func Contains(in interface{}, elem interface{}) bool {
inValue := reflect.ValueOf(in)
elemValue := reflect.ValueOf(elem)
inType := inValue.Type()
switch inType.Kind() {
case reflect.String:
return strings.Contains(inValue.String(), elemValue.String())
case reflect.Map:
for _, key := range inValue.MapKeys() {
if equal(key.Interface(), elem) {
return true
}
}
case reflect.Slice:
for i := 0; i < inValue.Len(); i++ {
if equal(inValue.Index(i).Interface(), elem) {
return true
}
}
}
return false
}
// Every returns true if every element is present in a iteratee.
func Every(in interface{}, elements ...interface{}) bool {
for _, elem := range elements {
if !Contains(in, elem) {
return false
}
}
return true
}