Skip to content

Commit

Permalink
Added trimSuffix, trimPrefix, hasSuffix, hasPrefix.
Browse files Browse the repository at this point in the history
Aliased a few functions that do not follow the nameing conventions.
So abbrevboth now has an abbrevBoth alias. Same for trimall and trimAll.
  • Loading branch information
technosophos committed Aug 19, 2016
1 parent bfc1db6 commit 89eb698
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ parse, it returns the time unaltered. See `time.ParseDuration` for info on durat
### String Functions

- trim: strings.TrimSpace
- trimall: strings.Trim, but with the argument order reversed `trimall "$" "$5.00"` or `"$5.00 | trimall "$"`
- trimAll: strings.Trim, but with the argument order reversed `trimAll "$" "$5.00"` or `"$5.00 | trimAll "$"`
- trimSuffix: strings.TrimSuffix, but with the argument order reversed `trimSuffix "-" "5-"`
- trimPrefix: strings.TrimPrefix, but with the argument order reversed `trimPrefix "$" "$5"`
- upper: strings.ToUpper
- lower: strings.ToLower
- title: strings.Title
Expand All @@ -93,7 +95,9 @@ parse, it returns the time unaltered. See `time.ParseDuration` for info on durat
- wrap: Wrap text at the given column count
- wrapWith: Wrap text at the given column count, and with the given
string for a line terminator: `wrap 50 "\n\t" $string`
- contains: strings.Contains, but with the arguments switched: `contains substr str`. (This simplifies common pipelines)
- contains: strings.Contains, but with the arguments switched: `contains "cat" "uncatch"`. (This simplifies common pipelines)
- hasPrefix: strings.hasPrefix, but with the arguments switched: `hasPrefix "cat" "catch"`.
- hasSuffix: strings.hasSuffix, but with the arguments switched: `hasSuffix "cat" "ducat"`.
- quote: Wrap strings in double quotes. `quote "a" "b"` returns `"a"
"b"`
- squote: Wrap strings in single quotes.
Expand Down
28 changes: 19 additions & 9 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ String Functions
- abbrevboth: Abbreviate from both sides, yielding "...lo wo..."
- trunc: Truncate a string (no suffix). `trunc 5 "Hello World"` yields "hello".
- trim: strings.TrimSpace
- trimall: strings.Trim, but with the argument order reversed `trimall "$" "$5.00"` or `"$5.00 | trimall "$"`
- trimAll: strings.Trim, but with the argument order reversed `trimAll "$" "$5.00"` or `"$5.00 | trimAll "$"`
- trimSuffix: strings.TrimSuffix, but with the argument order reversed: `trimSuffix "-" "ends-with-"`
- trimPrefix: strings.TrimPrefix, but with the argument order reversed `trimPrefix "$" "$5"`
- upper: strings.ToUpper
- lower: strings.ToLower
- nospace: Remove all space characters from a string. `nospace "h e l l o"` becomes "hello"
Expand All @@ -49,6 +51,8 @@ String Functions
- wrap: Force a line wrap at the given width. `wrap 80 "imagine a longer string"`
- wrapWith: Wrap a line at the given length, but using 'sep' instead of a newline. `wrapWith 50, "<br>", $html`
- contains: strings.Contains, but with the arguments switched: `contains substr str`. (This simplifies common pipelines)
- hasPrefix: strings.hasPrefix, but with the arguments switched
- hasSuffix: strings.hasSuffix, but with the arguments switched
- quote: Wrap string(s) in double quotation marks.
- squote: Wrap string(s) in double quotation marks.
- cat: Concatenate strings, separating them by spaces. `cat $a $b $c`.
Expand Down Expand Up @@ -266,8 +270,12 @@ var genericMap = map[string]interface{}{
"substr": substring,
// Switch order so that "foo" | repeat 5
"repeat": func(count int, str string) string { return strings.Repeat(str, count) },
// Deprecated: Use trimAll.
"trimall": func(a, b string) string { return strings.Trim(b, a) },
// Switch order so that "$foo" | trimall "$"
"trimall": func(a, b string) string { return strings.Trim(b, a) },
"trimAll": func(a, b string) string { return strings.Trim(b, a) },
"trimSuffix": func(a, b string) string { return strings.TrimSuffix(b, a) },
"trimPrefix": func(a, b string) string { return strings.TrimPrefix(b, a) },
"nospace": util.DeleteWhiteSpace,
"initials": initials,
"randAlphaNum": randAlphaNumeric,
Expand All @@ -278,13 +286,15 @@ var genericMap = map[string]interface{}{
"wrap": func(l int, s string) string { return util.Wrap(s, l) },
"wrapWith": func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
// Switch order so that "foobar" | contains "foo"
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
"quote": quote,
"squote": squote,
"cat": cat,
"indent": indent,
"replace": replace,
"plural": plural,
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
"quote": quote,
"squote": squote,
"cat": cat,
"indent": indent,
"replace": replace,
"plural": plural,

// Wrap Atoi to stop errors.
"atoi": func(a string) int { i, _ := strconv.Atoi(a); return i },
Expand Down
42 changes: 28 additions & 14 deletions functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ func TestSquote(t *testing.T) {
}
}

func TestContains(t *testing.T) {
// Mainly, we're just verifying the paramater order swap.
tests := []string{
`{{if contains "cat" "fair catch"}}1{{end}}`,
`{{if hasPrefix "cat" "catch"}}1{{end}}`,
`{{if hasSuffix "cat" "ducat"}}1{{end}}`,
}
for _, tt := range tests {
if err := runt(tt, "1"); err != nil {
t.Error(err)
}
}
}

func TestTrim(t *testing.T) {
tests := []string{
`{{trim " 5.00 "}}`,
`{{trimAll "$" "$5.00$"}}`,
`{{trimPrefix "$" "$5.00"}}`,
`{{trimSuffix "$" "5.00$"}}`,
}
for _, tt := range tests {
if err := runt(tt, "5.00"); err != nil {
t.Error(err)
}
}
}

func TestAdd(t *testing.T) {
tpl := `{{ 3 | add 1 2}}`
if err := runt(tpl, `6`); err != nil {
Expand Down Expand Up @@ -96,13 +124,6 @@ func TestMin(t *testing.T) {
}
}

func TestTrimall(t *testing.T) {
tpl := `{{"$foo$" | trimall "$"}}`
if err := runt(tpl, "foo"); err != nil {
t.Error(err)
}
}

func TestDefault(t *testing.T) {
tpl := `{{"" | default "foo"}}`
if err := runt(tpl, "foo"); err != nil {
Expand Down Expand Up @@ -353,13 +374,6 @@ func TestRandom(t *testing.T) {

}

func TestContains(t *testing.T) {
tpl := `{{"foobar" | contains "foo"}}`
if err := runt(tpl, "true"); err != nil {
t.Error(err)
}
}

func TestCat(t *testing.T) {
tpl := `{{$b := "b"}}{{"c" | cat "a" $b}}`
if err := runt(tpl, "a b c"); err != nil {
Expand Down

0 comments on commit 89eb698

Please sign in to comment.