feat: add urlEncode and urlDecode functions to template rendering

This commit is contained in:
rick 2025-02-27 09:44:23 +00:00
parent 09fbe6dae8
commit 255579bd23
2 changed files with 24 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import (
"fmt"
"io"
mathrand "math/rand"
"net/url"
"strings"
"text/template"
"time"
@ -224,6 +225,17 @@ var advancedFuncs = []AdvancedFunc{{
Func: func() float64 {
return time.Since(uptime).Seconds()
},
}, {
FuncName: "urlEncode",
Func: func(text string) string {
return url.QueryEscape(text)
},
}, {
FuncName: "urlDecode",
Func: func(text string) (result string) {
result, _ = url.QueryUnescape(text)
return
},
}}
// WeightEnum is a weight enum

View File

@ -127,6 +127,18 @@ func TestRender(t *testing.T) {
verify: func(t *testing.T, s string) {
assert.NotEmpty(t, s)
},
}, {
name: "url encode",
text: `{{urlEncode "hello world"}}`,
verify: func(t *testing.T, s string) {
assert.Equal(t, "hello+world", s)
},
}, {
name: "url decode",
text: `{{urlDecode "hello+world"}}`,
verify: func(t *testing.T, s string) {
assert.Equal(t, "hello world", s)
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {