-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfunctions_defs_compare.go
More file actions
263 lines (258 loc) · 7.13 KB
/
functions_defs_compare.go
File metadata and controls
263 lines (258 loc) · 7.13 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package diecast
import (
"fmt"
"regexp"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/typeutil"
)
func loadStandardFunctionsComparisons(_ FuncMap, _ *Server) funcGroup {
return funcGroup{
Name: `Comparison Functions`,
Description: `Used for comparing two values, or for returning one of many options based on a given condition.`,
Functions: []funcDef{
{
Name: `eqx`,
Summary: `Return whether two values are equal (any type).`,
Arguments: []funcArg{
{
Name: `left`,
Type: `any`,
Description: `The first value.`,
}, {
Name: `right`,
Type: `any`,
Description: `The second value.`,
},
},
Examples: []funcExample{
{
Code: `eqx "1" "1"`,
Return: true,
}, {
Code: `eqx "1" "2"`,
Return: false,
}, {
Code: `eqx "1" 1`,
Return: true,
}, {
Code: `eqx 1 1.0`,
Return: true,
}, {
Code: `eqx "1" 2`,
Return: false,
},
},
Function: stringutil.RelaxedEqual,
}, {
Name: `nex`,
Summary: `Return whether two values are not equal (any type).`,
Arguments: []funcArg{
{
Name: `left`,
Type: `any`,
Description: `The first value.`,
}, {
Name: `right`,
Type: `any`,
Description: `The second value.`,
},
},
Examples: []funcExample{
{
Code: `nex "1" "1"`,
Return: false,
}, {
Code: `nex "1" "2"`,
Return: true,
}, {
Code: `nex "1" 1`,
Return: false,
}, {
Code: `nex 1 1.0`,
Return: false,
}, {
Code: `nex "1" 2`,
Return: true,
},
},
Function: func(first any, second any) (bool, error) {
eq, err := stringutil.RelaxedEqual(first, second)
return !eq, err
},
}, {
Name: `gtx`,
Summary: `Return whether the first value is numerically or lexically greater than the second.`,
Arguments: []funcArg{
{
Name: `left`,
Type: `any`,
Description: `The first value.`,
}, {
Name: `right`,
Type: `any`,
Description: `The second value.`,
},
},
Function: func(first any, second any) (bool, error) {
return cmp(`ge`, first, second)
},
}, {
Name: `gex`,
Summary: `Return whether the first value is numerically or lexically greater than or equal to the second.`,
Arguments: []funcArg{
{
Name: `left`,
Type: `any`,
Description: `The first value.`,
}, {
Name: `right`,
Type: `any`,
Description: `The second value.`,
},
},
Function: func(first any, second any) (bool, error) {
return cmp(`ge`, first, second)
},
}, {
Name: `ltx`,
Summary: `Return whether the first value is numerically or lexically less than the second.`,
Arguments: []funcArg{
{
Name: `left`,
Type: `any`,
Description: `The first value.`,
}, {
Name: `right`,
Type: `any`,
Description: `The second value.`,
},
},
Function: func(first any, second any) (bool, error) {
return cmp(`lt`, first, second)
},
}, {
Name: `lex`,
Summary: `Return whether the first value is numerically or lexically less than or equal to the second.`,
Arguments: []funcArg{
{
Name: `left`,
Type: `any`,
Description: `The first value.`,
}, {
Name: `right`,
Type: `any`,
Description: `The second value.`,
},
},
Function: func(first any, second any) (bool, error) {
return cmp(`le`, first, second)
},
}, {
Name: `compare`,
Summary: `A generic comparison function. Accepts operators: "gt", "ge", "lt", "le", "eq", "ne"`,
Arguments: []funcArg{
{
Name: `operator`,
Type: `string`,
Description: `The type of compare operation being performed.`,
}, {
Name: `left`,
Type: `any`,
Description: `The first value.`,
}, {
Name: `right`,
Type: `any`,
Description: `The second value.`,
},
},
Function: func(operator string, first any, second any) (bool, error) {
switch operator {
case `gt`, `ge`, `lt`, `le`:
return cmp(operator, first, second)
case `eq`:
return stringutil.RelaxedEqual(first, second)
case `ne`:
eq, err := stringutil.RelaxedEqual(first, second)
return !eq, err
default:
return false, fmt.Errorf("invalid operator %q", operator)
}
},
}, {
Name: `match`,
Summary: `Return whether the given value matches the given regular expression.`,
Arguments: []funcArg{
{
Name: `pattern`,
Type: `string, array`,
Description: `The regular expression to match with, or an array of regular expressions (any of which may match).`,
}, {
Name: `value`,
Type: `string`,
Description: `The value to match against.`,
},
},
Function: func(patterns any, value any) (bool, error) {
for _, pattern := range sliceutil.Stringify(patterns) {
if rx, err := regexp.Compile(pattern); err == nil {
if rx.MatchString(typeutil.String(value)) {
return true, nil
}
} else {
return false, err
}
}
return false, nil
},
}, {
Name: `switch`,
Summary: `Provide a simple inline switch-case style decision mechanism.`,
Arguments: []funcArg{
{
Name: `input`,
Type: `any`,
Description: `The value being tested.`,
}, {
Name: `fallback`,
Type: `any`,
Description: `The "default" value if none of the subsequent conditions match.`,
}, {
Name: `criteria`,
Type: `array[if, then]`,
Description: `An array of values representing possible values of _input_, and the value to ` +
`return if input matches. Arguments are consumed as an array of value-result pairs.`,
},
},
Examples: []funcExample{
{
Code: `switch "yellow" "danger" "yellow" "warning" "green" "success" "blue" "info"`,
Return: `warning`,
}, {
Code: `switch "green" "danger" "yellow" "warning" "green" "success" "blue" "info"`,
Return: `success`,
}, {
Code: `switch "blue" "danger" "yellow" "warning" "green" "success" "blue" "info"`,
Return: `info`,
}, {
Code: `switch "red" "danger" "yellow" "warning" "green" "success" "blue" "info"`,
Return: `danger`,
}, {
Code: `switch "potato" "danger" "yellow" "warning" "green" "success" "blue" "info"`,
Return: `danger`,
},
},
Function: func(input any, fallback any, pairs ...any) any {
for _, pair := range sliceutil.Chunks(pairs, 2) {
if len(pair) == 2 {
if eq, err := stringutil.RelaxedEqual(input, pair[0]); err == nil && eq {
return pair[1]
}
}
}
return fallback
},
},
},
}
}