From bea23f0bf1a41292e77caab5a07de0b50eff5546 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Wed, 30 Mar 2022 21:10:35 +0200 Subject: [PATCH] Add basic string manipulation `join` and `split` Signed-off-by: Knut Ahlers --- README.md | 12 ++++++++++++ functions/func_strings.go | 8 ++++++++ 2 files changed, 20 insertions(+) create mode 100644 functions/func_strings.go diff --git a/README.md b/README.md index 2f7484c..208b51a 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,12 @@ $ echo '{{ hash "sha256" "this is a test" }}' | korvike 2e99758548972a8e8822ad47fa1017ff72f06f3ff6a016851f45c398732bc50c ``` +- `{{ join }}` + Joins the given slice of strings with given delimiter. + ```console + $ echo '{{ join (split "a,b,c" ",") "|" }}' | korvike + a|b|c + ``` - `{{ markdown }}` Format the source using a markdown parser ```console @@ -59,6 +65,12 @@ $ echo '{{ now "2006-01-02 15:04:05" }}' | korvike 2017-04-17 16:27:34 ``` +- `{{ split }}` + Splits the given string by given separator. + ```console + $ echo '{{ range (split "a,b,c" ",") }}{{ . }} {{ end }}' | korvike + a b c + ``` - `{{ tplexec (file "my.tpl") }}` Execute the given template with the same function set and variables as the parent template. ```console diff --git a/functions/func_strings.go b/functions/func_strings.go new file mode 100644 index 0000000..b1b6739 --- /dev/null +++ b/functions/func_strings.go @@ -0,0 +1,8 @@ +package functions + +import "strings" + +func init() { + registerFunction("join", strings.Join) + registerFunction("split", strings.Split) +}