diff --git a/template.go b/template.go index 1c2e536b..466b7458 100644 --- a/template.go +++ b/template.go @@ -366,6 +366,7 @@ func newTemplate(name string) *template.Template { "queryEscape": url.QueryEscape, "sha1": hashSha1, "split": strings.Split, + "splitN": strings.SplitN, "trimPrefix": trimPrefix, "trimSuffix": trimSuffix, "trim": trim, diff --git a/template_test.go b/template_test.go index 1226a20b..4975ba80 100644 --- a/template_test.go +++ b/template_test.go @@ -468,6 +468,17 @@ func TestHasSuffix(t *testing.T) { } } +func TestSplitN(t *testing.T) { + tests := templateTestList{ + {`{{index (splitN . "/" 2) 0}}`, "example.com/path", `example.com`}, + {`{{index (splitN . "/" 2) 1}}`, "example.com/path", `path`}, + {`{{index (splitN . "/" 2) 1}}`, "example.com/a/longer/path", `a/longer/path`}, + {`{{len (splitN . "/" 2)}}`, "example.com", `1`}, + } + + tests.run(t, "splitN") +} + func TestTrimPrefix(t *testing.T) { const prefix = "tcp://" const str = "tcp://127.0.0.1:2375"