From de6b22d4a01d7de56be8ec0325a3cbf6914d5086 Mon Sep 17 00:00:00 2001 From: Mike Dillon Date: Fri, 16 Oct 2015 21:05:28 -0700 Subject: [PATCH] Add splitN --- template.go | 1 + template_test.go | 11 +++++++++++ 2 files changed, 12 insertions(+) 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"