Skip to content

Commit 89eb698

Browse files
committed
Added trimSuffix, trimPrefix, hasSuffix, hasPrefix.
Aliased a few functions that do not follow the nameing conventions. So abbrevboth now has an abbrevBoth alias. Same for trimall and trimAll.
1 parent bfc1db6 commit 89eb698

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ parse, it returns the time unaltered. See `time.ParseDuration` for info on durat
7272
### String Functions
7373

7474
- trim: strings.TrimSpace
75-
- trimall: strings.Trim, but with the argument order reversed `trimall "$" "$5.00"` or `"$5.00 | trimall "$"`
75+
- trimAll: strings.Trim, but with the argument order reversed `trimAll "$" "$5.00"` or `"$5.00 | trimAll "$"`
76+
- trimSuffix: strings.TrimSuffix, but with the argument order reversed `trimSuffix "-" "5-"`
77+
- trimPrefix: strings.TrimPrefix, but with the argument order reversed `trimPrefix "$" "$5"`
7678
- upper: strings.ToUpper
7779
- lower: strings.ToLower
7880
- title: strings.Title
@@ -93,7 +95,9 @@ parse, it returns the time unaltered. See `time.ParseDuration` for info on durat
9395
- wrap: Wrap text at the given column count
9496
- wrapWith: Wrap text at the given column count, and with the given
9597
string for a line terminator: `wrap 50 "\n\t" $string`
96-
- contains: strings.Contains, but with the arguments switched: `contains substr str`. (This simplifies common pipelines)
98+
- contains: strings.Contains, but with the arguments switched: `contains "cat" "uncatch"`. (This simplifies common pipelines)
99+
- hasPrefix: strings.hasPrefix, but with the arguments switched: `hasPrefix "cat" "catch"`.
100+
- hasSuffix: strings.hasSuffix, but with the arguments switched: `hasSuffix "cat" "ducat"`.
97101
- quote: Wrap strings in double quotes. `quote "a" "b"` returns `"a"
98102
"b"`
99103
- squote: Wrap strings in single quotes.

functions.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ String Functions
3333
- abbrevboth: Abbreviate from both sides, yielding "...lo wo..."
3434
- trunc: Truncate a string (no suffix). `trunc 5 "Hello World"` yields "hello".
3535
- trim: strings.TrimSpace
36-
- trimall: strings.Trim, but with the argument order reversed `trimall "$" "$5.00"` or `"$5.00 | trimall "$"`
36+
- trimAll: strings.Trim, but with the argument order reversed `trimAll "$" "$5.00"` or `"$5.00 | trimAll "$"`
37+
- trimSuffix: strings.TrimSuffix, but with the argument order reversed: `trimSuffix "-" "ends-with-"`
38+
- trimPrefix: strings.TrimPrefix, but with the argument order reversed `trimPrefix "$" "$5"`
3739
- upper: strings.ToUpper
3840
- lower: strings.ToLower
3941
- nospace: Remove all space characters from a string. `nospace "h e l l o"` becomes "hello"
@@ -49,6 +51,8 @@ String Functions
4951
- wrap: Force a line wrap at the given width. `wrap 80 "imagine a longer string"`
5052
- wrapWith: Wrap a line at the given length, but using 'sep' instead of a newline. `wrapWith 50, "<br>", $html`
5153
- contains: strings.Contains, but with the arguments switched: `contains substr str`. (This simplifies common pipelines)
54+
- hasPrefix: strings.hasPrefix, but with the arguments switched
55+
- hasSuffix: strings.hasSuffix, but with the arguments switched
5256
- quote: Wrap string(s) in double quotation marks.
5357
- squote: Wrap string(s) in double quotation marks.
5458
- cat: Concatenate strings, separating them by spaces. `cat $a $b $c`.
@@ -266,8 +270,12 @@ var genericMap = map[string]interface{}{
266270
"substr": substring,
267271
// Switch order so that "foo" | repeat 5
268272
"repeat": func(count int, str string) string { return strings.Repeat(str, count) },
273+
// Deprecated: Use trimAll.
274+
"trimall": func(a, b string) string { return strings.Trim(b, a) },
269275
// Switch order so that "$foo" | trimall "$"
270-
"trimall": func(a, b string) string { return strings.Trim(b, a) },
276+
"trimAll": func(a, b string) string { return strings.Trim(b, a) },
277+
"trimSuffix": func(a, b string) string { return strings.TrimSuffix(b, a) },
278+
"trimPrefix": func(a, b string) string { return strings.TrimPrefix(b, a) },
271279
"nospace": util.DeleteWhiteSpace,
272280
"initials": initials,
273281
"randAlphaNum": randAlphaNumeric,
@@ -278,13 +286,15 @@ var genericMap = map[string]interface{}{
278286
"wrap": func(l int, s string) string { return util.Wrap(s, l) },
279287
"wrapWith": func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
280288
// Switch order so that "foobar" | contains "foo"
281-
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
282-
"quote": quote,
283-
"squote": squote,
284-
"cat": cat,
285-
"indent": indent,
286-
"replace": replace,
287-
"plural": plural,
289+
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
290+
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
291+
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
292+
"quote": quote,
293+
"squote": squote,
294+
"cat": cat,
295+
"indent": indent,
296+
"replace": replace,
297+
"plural": plural,
288298

289299
// Wrap Atoi to stop errors.
290300
"atoi": func(a string) int { i, _ := strconv.Atoi(a); return i },

functions_test.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,34 @@ func TestSquote(t *testing.T) {
5151
}
5252
}
5353

54+
func TestContains(t *testing.T) {
55+
// Mainly, we're just verifying the paramater order swap.
56+
tests := []string{
57+
`{{if contains "cat" "fair catch"}}1{{end}}`,
58+
`{{if hasPrefix "cat" "catch"}}1{{end}}`,
59+
`{{if hasSuffix "cat" "ducat"}}1{{end}}`,
60+
}
61+
for _, tt := range tests {
62+
if err := runt(tt, "1"); err != nil {
63+
t.Error(err)
64+
}
65+
}
66+
}
67+
68+
func TestTrim(t *testing.T) {
69+
tests := []string{
70+
`{{trim " 5.00 "}}`,
71+
`{{trimAll "$" "$5.00$"}}`,
72+
`{{trimPrefix "$" "$5.00"}}`,
73+
`{{trimSuffix "$" "5.00$"}}`,
74+
}
75+
for _, tt := range tests {
76+
if err := runt(tt, "5.00"); err != nil {
77+
t.Error(err)
78+
}
79+
}
80+
}
81+
5482
func TestAdd(t *testing.T) {
5583
tpl := `{{ 3 | add 1 2}}`
5684
if err := runt(tpl, `6`); err != nil {
@@ -96,13 +124,6 @@ func TestMin(t *testing.T) {
96124
}
97125
}
98126

99-
func TestTrimall(t *testing.T) {
100-
tpl := `{{"$foo$" | trimall "$"}}`
101-
if err := runt(tpl, "foo"); err != nil {
102-
t.Error(err)
103-
}
104-
}
105-
106127
func TestDefault(t *testing.T) {
107128
tpl := `{{"" | default "foo"}}`
108129
if err := runt(tpl, "foo"); err != nil {
@@ -353,13 +374,6 @@ func TestRandom(t *testing.T) {
353374

354375
}
355376

356-
func TestContains(t *testing.T) {
357-
tpl := `{{"foobar" | contains "foo"}}`
358-
if err := runt(tpl, "true"); err != nil {
359-
t.Error(err)
360-
}
361-
}
362-
363377
func TestCat(t *testing.T) {
364378
tpl := `{{$b := "b"}}{{"c" | cat "a" $b}}`
365379
if err := runt(tpl, "a b c"); err != nil {

0 commit comments

Comments
 (0)