forked from diegoholiveira/jsonlogic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays.go
89 lines (63 loc) · 1.46 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
package jsonlogic
func filter(values, data interface{}) interface{} {
parsed := values.([]interface{})
var subject interface{}
if isSlice(parsed[0]) {
subject = parsed[0]
} else {
subject = apply(parsed[0], data)
}
result := make([]interface{}, 0)
if subject == nil {
return result
}
logic := solveVars(parsed[1], data)
for _, value := range subject.([]interface{}) {
v := parseValues(logic, value)
if isTrue(v) {
result = append(result, value)
}
}
return result
}
func _map(values, data interface{}) interface{} {
parsed := values.([]interface{})
var subject interface{}
if isSlice(parsed[0]) {
subject = parsed[0]
} else {
subject = apply(parsed[0], data)
}
result := make([]interface{}, 0)
if subject == nil {
return result
}
logic := solveVars(parsed[1], data)
for _, value := range subject.([]interface{}) {
v := parseValues(logic, value)
if isTrue(v) || isNumber(v) {
result = append(result, v)
}
}
return result
}
func reduce(values, data interface{}) interface{} {
parsed := values.([]interface{})
subject := apply(parsed[0], data)
if subject == nil {
return float64(0)
}
context := map[string]interface{}{
"current": float64(0),
"accumulator": toNumber(parsed[2]),
}
for _, value := range subject.([]interface{}) {
context["current"] = value
v := apply(parsed[1], context)
if v == nil {
continue
}
context["accumulator"] = toNumber(v)
}
return context["accumulator"]
}