expander-base-funcs.go -- Added function __trim and renamed __mapNC as __mac-np

This commit is contained in:
Celestino Amoroso 2025-06-18 19:18:53 +02:00
parent ab722abe2d
commit 549578cb48
2 changed files with 11 additions and 4 deletions

View File

@ -35,9 +35,10 @@ func TestNewDictSetContext(t *testing.T) {
now := time.Now() now := time.Now()
yday := now.Add(-24 * time.Hour) yday := now.Add(-24 * time.Hour)
inputs := []inputType{ inputs := []inputType{
{ctx1, "${|__default two|__map : 'one:1' 'two:2' 'six:6'}", "2", nil}, {ctx1, "${|__default ' abc cba '|__trim}", "abc cba", nil},
{ctx1, "${|__default two|__map : 'one:1' 'TWO:2' 'six:6'}", "", nil}, {ctx1, "${|__default two|__map ':' 'one:1' 'two:2' 'six:6'}", "2", nil},
{ctx1, "${|__default two|__mapNC : 'one:1' 'TWO:2' 'six:6'}", "2", nil}, {ctx1, "${|__default two|__map ':' 'one:1' 'TWO:2' 'six:6'}", "", nil},
{ctx1, "${|__default two|__map-nc ':' 'one:1' 'TWO:2' 'six:6'}", "2", nil},
{ctx2, `form: ${|__time-fmt now ""}`, fmt.Sprintf("form: %d-%02d-%02d %02d:%02d:%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()), nil}, {ctx2, `form: ${|__time-fmt now ""}`, fmt.Sprintf("form: %d-%02d-%02d %02d:%02d:%02d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()), nil},
{ctx2, `form: ${|__time-fmt now "%-D"}`, fmt.Sprintf("form: %d-%d-%d", now.Year(), now.Month(), now.Day()), nil}, {ctx2, `form: ${|__time-fmt now "%-D"}`, fmt.Sprintf("form: %d-%d-%d", now.Year(), now.Month(), now.Day()), nil},
{ctx2, `form: ${|__time-fmt now "%:T"}`, fmt.Sprintf("form: %d:%d:%d", now.Hour(), now.Minute(), now.Second()), nil}, {ctx2, `form: ${|__time-fmt now "%:T"}`, fmt.Sprintf("form: %d:%d:%d", now.Hour(), now.Minute(), now.Second()), nil},

View File

@ -84,6 +84,11 @@ func ExFuncTrimSuffix(ctx ExpanderContext, varValue string, args []string) (resu
return return
} }
func ExFuncTrim(ctx ExpanderContext, varValue string, args []string) (result string, err error) {
result = strings.TrimSpace(varValue)
return
}
func ExFuncMap(ctx ExpanderContext, varValue string, args []string) (result string, err error) { func ExFuncMap(ctx ExpanderContext, varValue string, args []string) (result string, err error) {
sep := args[0] sep := args[0]
for _, pairSpec := range args[1:] { for _, pairSpec := range args[1:] {
@ -120,8 +125,9 @@ func AddBaseFuncs(ctx ExpanderContext) ExpanderContext {
ctx.AddFunc("__join", ExFuncJoin, 1, -1, "Joins its args starting from the second. The first one (arg1) is the separator") ctx.AddFunc("__join", ExFuncJoin, 1, -1, "Joins its args starting from the second. The first one (arg1) is the separator")
ctx.AddFunc("__lower", ExFuncLower, 0, 0, "Changes the flow value to lower case.") ctx.AddFunc("__lower", ExFuncLower, 0, 0, "Changes the flow value to lower case.")
ctx.AddFunc("__upper", ExFuncUpper, 0, 0, "Changes the flow value to upper case.") ctx.AddFunc("__upper", ExFuncUpper, 0, 0, "Changes the flow value to upper case.")
ctx.AddFunc("__trim", ExFuncTrim, 0, 0, "Removes blanks on the left and on the right of the flow.")
ctx.AddFunc("__trim-suffix", ExFuncTrimSuffix, 1, 1, "Removes the specified suffix (arg1) from the flow.") ctx.AddFunc("__trim-suffix", ExFuncTrimSuffix, 1, 1, "Removes the specified suffix (arg1) from the flow.")
ctx.AddFunc("__map", ExFuncMap, 1, 1, "Returns the value mapped by the flow value as a key; e.g. two __map : 'one:1' 'two:2' 'six:6' returns 2") ctx.AddFunc("__map", ExFuncMap, 1, 1, "Returns the value mapped by the flow value as a key; e.g. two __map : 'one:1' 'two:2' 'six:6' returns 2")
ctx.AddFunc("__mapNC", ExFuncMapNC, 1, 1, "Same as __map but this comapares keys ignoring case differencies; e.g. two __map : 'ONE:1' 'Two:2' 'sIX:6' returns 2") ctx.AddFunc("__map-nc", ExFuncMapNC, 1, 1, "Same as __map but this comapares keys ignoring case differencies; e.g. two __map-nc : 'ONE:1' 'Two:2' 'sIX:6' returns 2")
return ctx return ctx
} }