-
Notifications
You must be signed in to change notification settings - Fork 16
/
trim.go
44 lines (38 loc) · 945 Bytes
/
trim.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
package dynaml
import (
"strings"
"github.com/mandelsoft/spiff/yaml"
)
func func_trim(arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) {
info := DefaultInfo()
ok := true
if len(arguments) > 2 {
return info.Error("split takes one or two arguments")
}
cutset := " \t"
if len(arguments) == 2 {
cutset, ok = arguments[1].(string)
if !ok {
return info.Error("second argument of split must be a string")
}
}
var result interface{}
switch v := arguments[0].(type) {
case string:
result = strings.Trim(v, cutset)
case []yaml.Node:
list := make([]yaml.Node, len(v))
for i, e := range v {
t, ok := e.Value().(string)
if !ok {
return info.Error("list elements must be strings to be trimmed")
}
t = strings.Trim(t, cutset)
list[i] = NewNode(t, binding)
}
result = list
default:
return info.Error("trim accepts only a string or list")
}
return result, info, true
}