forked from expr-lang/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc_text.go
More file actions
61 lines (50 loc) · 1.34 KB
/
func_text.go
File metadata and controls
61 lines (50 loc) · 1.34 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
package expr
import (
"regexp"
"strings"
"github.com/datasweet/cast"
)
// CONCAT(x, y)
// Used with operator ~
var concat binaryOperatorFunc = func(x, y interface{}) interface{} {
cx, _ := cast.AsString(x)
cy, _ := cast.AsString(y)
return cx + cy
}
// LENGTH(x string)
// Returns the number of characters in X
var length unaryOperatorFunc = func(x interface{}) interface{} {
cx, _ := cast.AsString(x)
return len(cx)
}
// LOWER(x string)
// Converts X to lowercase
var lower unaryOperatorFunc = func(x interface{}) interface{} {
cx, _ := cast.AsString(x)
return strings.ToLower(cx)
}
// MATCHES(x string, y string or regexp)
// Returns true if X matches Y, false otherwise
var matches binaryOperatorFunc = func(x interface{}, y interface{}) interface{} {
cx, _ := cast.AsString(x)
if rg, ok := y.(*regexp.Regexp); ok && rg != nil {
return rg.MatchString(cx)
}
cy, _ := cast.AsString(y)
if matched, err := regexp.MatchString(cy, cx); err == nil {
return matched
}
return false
}
// TRIM(x string)
// Returns X with leading and trailing whitespace removed
var trim unaryOperatorFunc = func(x interface{}) interface{} {
cx, _ := cast.AsString(x)
return strings.TrimSpace(cx)
}
// UPPER(x string)
// Converts X to uppercase
var upper unaryOperatorFunc = func(x interface{}) interface{} {
cx, _ := cast.AsString(x)
return strings.ToUpper(cx)
}