From ea3fac2fc13b361832b96a0f0c696686be6e698b Mon Sep 17 00:00:00 2001 From: Braden Hilton Date: Sun, 19 May 2024 21:34:51 +0100 Subject: [PATCH] concat --- .github/workflows/main.yml | 4 ++++ docs/templatefuncs.md | 10 ++++++++++ go.mod | 2 +- templatefuncs.go | 1 + templatefuncs_test.go | 4 ++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ea0a4e9..0efe056 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,12 +12,16 @@ jobs: steps: - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 - uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 + with: + go-version: 'stable' - run: go test ./... lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 - uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 + with: + go-version: 'stable' - uses: golangci/golangci-lint-action@a4f60bb28d35aeee14e6880718e0c85ff1882e64 with: version: v1.58.1 diff --git a/docs/templatefuncs.md b/docs/templatefuncs.md index 1e7fe85..25a57da 100644 --- a/docs/templatefuncs.md +++ b/docs/templatefuncs.md @@ -10,6 +10,16 @@ [one three] ``` +## `concat` *list*... + +`concat` concatenates *list*s into a new list. + +```text +{{ concat (list 0 1 2) (list "a" "b" "c") }} + +[0 1 2 a b c] +``` + ## `contains` *substring* *string* `contains` returns whether *substring* is in *string*. diff --git a/go.mod b/go.mod index 55a7e89..1eee1a6 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/chezmoi/templatefuncs -go 1.21 +go 1.22 require github.com/alecthomas/assert/v2 v2.9.0 diff --git a/templatefuncs.go b/templatefuncs.go index e6cc007..2477da9 100644 --- a/templatefuncs.go +++ b/templatefuncs.go @@ -33,6 +33,7 @@ var fileModeTypeNames = map[fs.FileMode]string{ func NewFuncMap() template.FuncMap { return template.FuncMap{ "compact": compactTemplateFunc, + "concat": slices.Concat[[]any], "contains": reverseArgs2(strings.Contains), "eqFold": eqFoldTemplateFunc, "fromJSON": eachByteSliceErr(fromJSONTemplateFunc), diff --git a/templatefuncs_test.go b/templatefuncs_test.go index 3ad4904..d4093ee 100644 --- a/templatefuncs_test.go +++ b/templatefuncs_test.go @@ -54,6 +54,10 @@ func TestFuncMap(t *testing.T) { template: `{{ list "one" "" list "three" | compact }}`, expected: `[one three]`, }, + { + template: `{{ concat (list 0 1 2) (list "a" "b" "c") }}`, + expected: "[0 1 2 a b c]", + }, { template: `{{ "abc" | contains "bc" }}`, expected: "true",