-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
40 lines (32 loc) · 919 Bytes
/
util.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
package js_array_method
import (
"fmt"
"reflect"
)
func callCallback[T any](conditions interface{}, value T, index int) bool {
res := reflect.ValueOf(conditions).Call([]reflect.Value{reflect.ValueOf(value), reflect.ValueOf(index)})
return res[0].Bool()
}
func compareValue[T any](conditions interface{}, value T) bool {
res := reflect.ValueOf(conditions).String()
return res == fmt.Sprintf("%v", value)
}
func getType(conditions interface{}) string {
return reflect.TypeOf(conditions).Kind().String()
}
func calculateEveryElement[T any](input []T, conditions interface{}) []bool {
output := []bool{}
conditionType := getType(conditions)
for index, value := range input {
result := false
if conditionType == "func" {
result = callCallback(conditions, value, index)
} else {
result = compareValue(conditions, value)
}
if result {
output = append(output, true)
}
}
return output
}