Skip to content

Commit de6b22d

Browse files
committed
Add splitN
1 parent a12442b commit de6b22d

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ func newTemplate(name string) *template.Template {
366366
"queryEscape": url.QueryEscape,
367367
"sha1": hashSha1,
368368
"split": strings.Split,
369+
"splitN": strings.SplitN,
369370
"trimPrefix": trimPrefix,
370371
"trimSuffix": trimSuffix,
371372
"trim": trim,

template_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,17 @@ func TestHasSuffix(t *testing.T) {
468468
}
469469
}
470470

471+
func TestSplitN(t *testing.T) {
472+
tests := templateTestList{
473+
{`{{index (splitN . "/" 2) 0}}`, "example.com/path", `example.com`},
474+
{`{{index (splitN . "/" 2) 1}}`, "example.com/path", `path`},
475+
{`{{index (splitN . "/" 2) 1}}`, "example.com/a/longer/path", `a/longer/path`},
476+
{`{{len (splitN . "/" 2)}}`, "example.com", `1`},
477+
}
478+
479+
tests.run(t, "splitN")
480+
}
481+
471482
func TestTrimPrefix(t *testing.T) {
472483
const prefix = "tcp://"
473484
const str = "tcp://127.0.0.1:2375"

0 commit comments

Comments
 (0)