-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjs.go
246 lines (229 loc) · 4.96 KB
/
js.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package jsonlogic
import (
"fmt"
"math"
"reflect"
"strconv"
"strings"
"unsafe"
)
// IsTrue implements the truthy/falsy semantics of JsonLogic
// as documented here: https://jsonlogic.com/truthy.html
// in addition, an emptry struct is considered true, as
// per jsonlogic JavaScript handling of "{}"
func IsTrue(i interface{}) bool {
if i == nil {
return false
}
switch v := i.(type) {
case float64:
return v != 0
case map[string]interface{}:
return true
case []interface{}:
return len(v) != 0
case string:
return v != ""
case bool:
return v
default:
return true
}
}
func toNumber(i interface{}) float64 {
switch v := i.(type) {
case string:
tstr := strings.TrimSpace(v)
if tstr == "" {
return 0.0
}
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return math.NaN()
}
return f
case float64:
return v
case bool:
if v {
return 1.0
}
return 0.0
case []interface{}:
if len(v) == 0 {
return 0.0
}
if len(v) == 1 {
return toNumber(v[0])
}
return math.NaN()
case nil:
return 0.0
default:
return math.NaN()
}
}
func toString(i interface{}) string {
switch v := i.(type) {
case string:
return v
case float64:
return strconv.FormatFloat(v, 'f', -1, 64)
case bool:
if v {
return "true"
}
return "false"
case nil:
return "null"
case []interface{}:
var strs = make([]string, len(v))
for i := range v {
strs[i] = toString(v[i])
}
return strings.Join(strs, ",")
default:
return fmt.Sprintf("%v", v)
}
}
// IsEqual is an exact equality check.
func IsEqual(l, r interface{}) bool {
_, lisfloat := l.(float64)
_, lisstr := l.(string)
_, lisbool := l.(bool)
lslice, lisslice := l.([]interface{})
lmap, lismap := l.(map[string]interface{})
_, risfloat := r.(float64)
_, risstr := r.(string)
_, risbool := r.(bool)
rslice, risslice := r.([]interface{})
rmap, rismap := r.(map[string]interface{})
switch {
case l == nil && r == nil:
return true
case l == nil || r == nil:
return false
case lismap && rismap:
// compare two maps, must be the same object
lptr := reflect.ValueOf(lmap)
rptr := reflect.ValueOf(rmap)
return lptr == rptr
case lisslice && risslice:
lhdr := (*reflect.SliceHeader)(unsafe.Pointer(&lslice))
rhdr := (*reflect.SliceHeader)(unsafe.Pointer(&rslice))
return lhdr.Cap == rhdr.Cap &&
lhdr.Len == rhdr.Len &&
lhdr.Data == rhdr.Data
case
lisbool && risbool,
lisfloat && risfloat,
lisstr && risstr:
return l == r
default:
return false
}
}
// IsSoftEqual is an equality check that will
// coerce values according to JavaScript rules.
func IsSoftEqual(l, r interface{}) bool {
_, lisfloat := l.(float64)
_, lisstr := l.(string)
_, lisbool := l.(bool)
lslice, lisslice := l.([]interface{})
lmap, lismap := l.(map[string]interface{})
_, risfloat := r.(float64)
_, risstr := r.(string)
_, risbool := r.(bool)
rslice, risslice := r.([]interface{})
rmap, rismap := r.(map[string]interface{})
switch {
case l == nil && r == nil:
return true
case l == nil || r == nil:
return false
case lismap && rismap:
// compare two maps, must be the same object
lptr := reflect.ValueOf(lmap)
rptr := reflect.ValueOf(rmap)
return lptr == rptr
case lisslice && risslice:
lhdr := (*reflect.SliceHeader)(unsafe.Pointer(&lslice))
rhdr := (*reflect.SliceHeader)(unsafe.Pointer(&rslice))
return lhdr.Cap == rhdr.Cap &&
lhdr.Len == rhdr.Len &&
lhdr.Data == rhdr.Data
case lisslice || risslice:
if lisslice {
return IsSoftEqual(toString(l), r)
}
return IsSoftEqual(l, toString(r))
case lismap || rismap:
return false
case
lisbool && risbool,
lisfloat && risfloat,
lisstr && risstr:
return l == r
default:
return toNumber(l) == toNumber(r)
}
}
// IsDeepEqual is an equality check that will
// coerce values according to JavaScript rules, and
// compare map and array content..
func IsDeepEqual(l, r interface{}) bool {
_, lisfloat := l.(float64)
_, lisstr := l.(string)
_, lisbool := l.(bool)
lslice, lisslice := l.([]interface{})
lmap, lismap := l.(map[string]interface{})
_, risfloat := r.(float64)
_, risstr := r.(string)
_, risbool := r.(bool)
rslice, risslice := r.([]interface{})
rmap, rismap := r.(map[string]interface{})
switch {
case l == nil && r == nil:
return true
case l == nil || r == nil:
return false
case lismap && rismap:
if len(lmap) != len(rmap) {
return false
}
for k, v := range lmap {
rv, ok := rmap[k]
if !ok {
return false
}
if !IsDeepEqual(v, rv) {
return false
}
}
return true
case lisslice && risslice:
if len(lslice) != len(rslice) {
return false
}
for i := range lslice {
if !IsDeepEqual(lslice[i], rslice[i]) {
return false
}
}
return true
case lisslice || risslice:
if lisslice {
return IsSoftEqual(toString(l), r)
}
return IsSoftEqual(l, toString(r))
case lismap || rismap:
return false
case
lisbool && risbool,
lisfloat && risfloat,
lisstr && risstr:
return l == r
default:
return toNumber(l) == toNumber(r)
}
}