From 549578cb488d30619cdb814475a0397aafc86b8e Mon Sep 17 00:00:00 2001 From: Celestino Amoroso Date: Wed, 18 Jun 2025 19:18:53 +0200 Subject: [PATCH] expander-base-funcs.go -- Added function __trim and renamed __mapNC as __mac-np --- dict-set-context_test.go | 7 ++++--- expander-base-funcs.go | 8 +++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/dict-set-context_test.go b/dict-set-context_test.go index 1a32709..5eecc9d 100644 --- a/dict-set-context_test.go +++ b/dict-set-context_test.go @@ -35,9 +35,10 @@ func TestNewDictSetContext(t *testing.T) { now := time.Now() yday := now.Add(-24 * time.Hour) inputs := []inputType{ - {ctx1, "${|__default two|__map : 'one:1' 'two:2' 'six:6'}", "2", nil}, - {ctx1, "${|__default two|__map : 'one:1' 'TWO:2' 'six:6'}", "", nil}, - {ctx1, "${|__default two|__mapNC : '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'}", "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 "%-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}, diff --git a/expander-base-funcs.go b/expander-base-funcs.go index 134649c..b3fddb4 100644 --- a/expander-base-funcs.go +++ b/expander-base-funcs.go @@ -84,6 +84,11 @@ func ExFuncTrimSuffix(ctx ExpanderContext, varValue string, args []string) (resu 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) { sep := args[0] 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("__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("__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("__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 }