-
Notifications
You must be signed in to change notification settings - Fork 272
/
presence.go
207 lines (164 loc) · 4.75 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package funk
import (
"fmt"
"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{} {
_, val := FindKey(arr, predicate)
return val
}
// Find iterates over elements of collection, returning the first
// element of an array and random of a map which predicate returns truthy for.
func FindKey(arr interface{}, predicate interface{}) (matchKey, matchEle 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)
var keyArrs []reflect.Value
isMap := arrValue.Kind() == reflect.Map
if isMap {
keyArrs = arrValue.MapKeys()
}
for i := 0; i < arrValue.Len(); i++ {
var (
elem reflect.Value
key reflect.Value
)
if isMap {
key = keyArrs[i]
elem = arrValue.MapIndex(key)
} else {
key = reflect.ValueOf(i)
elem = arrValue.Index(i)
}
result := funcValue.Call([]reflect.Value{elem})[0].Interface().(bool)
if result {
return key.Interface(), elem.Interface()
}
}
return nil, 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 {
equalTo := equal(elem)
for i := 0; i < inValue.Len(); i++ {
if equalTo(reflect.Value{}, inValue.Index(i)) {
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()
equalTo := equal(elem)
for i := length - 1; i >= 0; i-- {
if equalTo(reflect.Value{}, inValue.Index(i)) {
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:
equalTo := equal(elem, true)
for _, key := range inValue.MapKeys() {
if equalTo(key, inValue.MapIndex(key)) {
return true
}
}
case reflect.Slice, reflect.Array:
equalTo := equal(elem)
for i := 0; i < inValue.Len(); i++ {
if equalTo(reflect.Value{}, inValue.Index(i)) {
return true
}
}
default:
panic(fmt.Sprintf("Type %s is not supported by Contains, supported types are String, Map, Slice, Array", inType.String()))
}
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
}
// Some returns true if atleast one element is present in an iteratee.
func Some(in interface{}, elements ...interface{}) bool {
for _, elem := range elements {
if Contains(in, elem) {
return true
}
}
return false
}