forked from snsinfu/reconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.go
67 lines (59 loc) · 1.76 KB
/
funcs.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
package main
import (
"errors"
"strings"
"text/template"
)
// Defines utility functions for use in templates.
var templateFuncs = template.FuncMap{
// These functions are meant to be called in a pipe chain, where
// $input | split ":" | nonempty | joinn ":"
// is equivalent to
// join (":" nonempty (split ":" $input))
// Hence, the functions take arguments in unusual order.
// Splits `s` into all substrings separated by `sep`.
"split": func(sep, s string) []string {
return strings.Split(s, sep)
},
// Concatenates `elems` into a single string with separator `sep`.
"join": func(sep string, elems []string) string {
return strings.Join(elems, sep)
},
// Returns the substring of `s` before `sep` (non-inclusive). It returns
// the entire string `s` if `sep` is not in the string.
"before": func(sep, s string) string {
sub, _ := splitOnce(s, sep)
return sub
},
// Returns the substring of `s` after `sep` (non-inclusive). It returns
// the entire string `s` if `sep` is not in the string.
"after": func(sep, s string) string {
_, sub := splitOnce(s, sep)
return sub
},
// Removes empty strings from an array of strings.
"nonempty": func(arr []string) []string {
r := []string{}
for _, s := range arr {
if s != "" {
r = append(r, s)
}
}
return r
},
// Removes all leading and trailing white space of string. If an array of
// string is given, it works on all the elements.
"strip": func(arg interface{}) (interface{}, error) {
switch arg := arg.(type) {
case string:
return strings.TrimSpace(arg), nil
case []string:
r := []string{}
for _, s := range arg {
r = append(r, strings.TrimSpace(s))
}
return r, nil
}
return nil, errors.New("strip expects string or array of strings as an input")
},
}