Skip to content

Instantly share code, notes, and snippets.

@mholt
Last active December 15, 2021 08:48
Show Gist options
  • Save mholt/2a874dd67e830d07ec11 to your computer and use it in GitHub Desktop.
Save mholt/2a874dd67e830d07ec11 to your computer and use it in GitHub Desktop.
Functions from the 'strings' package as template actions. Feel free to copy+paste into your project as a starting point.
// Functions from Go's strings package usable as template actions
// with text/template.
//
// This approach assumes you have some context type as a receiver,
// but if you just need the functions check out the FuncMap variant
// below.
//
// Commented functions are not deemed useful in template actions.
// Haven't actually used this, but this is one possible way of doing it.
// Another option is to fill a FuncMap with most of the strings package.
func (c Context) Contains(s, substr string) bool {
return strings.Contains(s, substr)
}
func (c Context) ContainsAny(s, chars string) bool {
return strings.ContainsAny(s, chars)
}
/*func (c Context) ContainsRune(s string, r rune) bool {
return strings.ContainsRune(s, r)
}*/
func (c Context) Count(s, sep string) int {
return strings.Count(s, sep)
}
func (c Context) EqualFold(s, t string) bool {
return strings.EqualFold(s, t)
}
func (c Context) Fields(s string) []string {
return strings.Fields(s)
}
/*func (c Context) FieldsFunc(s string, f func(rune) bool) []string {
return strings.FieldsFunc(s, f)
}*/
func (c Context) HasPrefix(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}
func (c Context) HasSuffix(s, suffix string) bool {
return strings.HasSuffix(s, suffix)
}
func (c Context) Index(s, sep string) int {
return strings.Index(s, sep)
}
func (c Context) IndexAny(s, chars string) int {
return strings.IndexAny(s, chars)
}
/*func (c Context) IndexByte(s string, c byte) int {
return strings.IndexByte(s, c)
}*/
/*func (c Context) IndexFunc(s string, f func(rune) bool) int {
return strings.IndexFunc(s, f)
}*/
/*func (c Context) IndexRune(s string, r rune) int {
return strings.IndexRune(s, r)
}*/
func (c Context) Join(a []string, sep string) string {
return strings.Join(a, sep)
}
func (c Context) LastIndex(s, sep string) int {
return strings.LastIndex(s, sep)
}
func (c Context) LastIndexAny(s, chars string) int {
return strings.LastIndexAny(s, chars)
}
/*func (c Context) LastIndexFunc(s string, f func(rune) bool) int {
return strings.LastIndexFunc(s, f)
}*/
/*func (c Context) Map(mapping func(rune) rune, s string) string {
return strings.Map(mapping, s)
}*/
/*func (c Context) Repeat(s string, count int) string {
return strings.Repeat(s, count)
}*/
func (c Context) Replace(s, old, new string, n int) string {
return strings.Replace(s, old, new, n)
}
func (c Context) Split(s, sep string) []string {
return strings.Split(s, sep)
}
func (c Context) SplitAfter(s, sep string) []string {
return strings.SplitAfter(s, sep)
}
func (c Context) SplitAfterN(s, sep string, n int) []string {
return strings.SplitAfterN(s, sep, n)
}
func (c Context) SplitN(s, sep string, n int) []string {
return strings.SplitN(s, sep, n)
}
func (c Context) Title(s string) string {
return strings.Title(s)
}
func (c Context) ToLower(s string) string {
return strings.ToLower(s)
}
/*func (c Context) ToLowerSpecial(_case unicode.SpecialCase, s string) string {
return strings.ToLowerSpecial(_case, s)
}*/
func (c Context) ToTitle(s string) string {
return strings.ToTitle(s)
}
/*func (c Context) ToTitleSpecial(_case unicode.SpecialCase, s string) string {
return strings.ToTitleSpecial(_case, s)
}*/
func (c Context) ToUpper(s string) string {
return strings.ToUpper(s)
}
/*func (c Context) ToUpperSpecial(_case unicode.SpecialCase, s string) string {
return strings.ToUpperSpecial(_case, s)
}*/
func (c Context) Trim(s string, cutset string) string {
return strings.Trim(s, cutset)
}
/*func (c Context) TrimFunc(s string, f func(rune) bool) string {
return strings.TrimFunc(s, f)
}*/
func (c Context) TrimLeft(s string, cutset string) string {
return strings.TrimLeft(s, cutset)
}
/*func (c Context) TrimLeftFunc(s string, f func(rune) bool) string {
return strings.TrimLeftFunc(s, f)
}*/
func (c Context) TrimPrefix(s, prefix string) string {
return strings.TrimPrefix(s, prefix)
}
func (c Context) TrimRight(s string, cutset string) string {
return strings.TrimRight(s, cutset)
}
/*func (c Context) TrimRightFunc(s string, f func(rune) bool) string {
return strings.TrimRightFunc(s, f)
}*/
func (c Context) TrimSpace(s string) string {
return strings.TrimSpace(s)
}
func (c Context) TrimSuffix(s, suffix string) string {
return strings.TrimSuffix(s, suffix)
}
// Functions from Go's strings package usable as template actions
// with text/template.
package stringstpl
import "strings"
// StringsFuncs is a template.FuncMap with functions that can be used as template actions.
var StringsFuncs = map[string]interface{}{
"Contains": func(s, substr string) bool { return strings.Contains(s, substr) },
"ContainsAny": func(s, chars string) bool { return strings.ContainsAny(s, chars) },
//"ContainsRune": func(s string, r rune) bool { return strings.ContainsRune(s, r) },
"Count": func(s, sep string) int { return strings.Count(s, sep) },
"EqualFold": func(s, t string) bool { return strings.EqualFold(s, t) },
"Fields": func(s string) []string { return strings.Fields(s) },
//"FieldsFunc": func(s string, f func(rune) bool) []string { return strings.FieldsFunc(s, f) },
"HasPrefix": func(s, prefix string) bool { return strings.HasPrefix(s, prefix) },
"HasSuffix": func(s, suffix string) bool { return strings.HasSuffix(s, suffix) },
"Index": func(s, sep string) int { return strings.Index(s, sep) },
"IndexAny": func(s, chars string) int { return strings.IndexAny(s, chars) },
//"IndexByte": func(s string, c byte) int { return strings.IndexByte(s, c) },
//"IndexFunc": func(s string, f func(rune) bool) int { return strings.IndexFunc(s, f) },
//"IndexRune": func(s string, r rune) int { return strings.IndexRune(s, r) },
"Join": func(a []string, sep string) string { return strings.Join(a, sep) },
"LastIndex": func(s, sep string) int { return strings.LastIndex(s, sep) },
"LastIndexAny": func(s, chars string) int { return strings.LastIndexAny(s, chars) },
//"LastIndexFunc": func(s string, f func(rune) bool) int { return strings.LastIndexFunc(s, f) },
//"Map": func(mapping func(rune) rune, s string) string { return strings.Map(mapping, s) },
//"Repeat": func(s string, count int) string { return strings.Repeat(s, count) },
"Replace": func(s, old, new string, n int) string { return strings.Replace(s, old, new, n) },
"Split": func(s, sep string) []string { return strings.Split(s, sep) },
"SplitAfter": func(s, sep string) []string { return strings.SplitAfter(s, sep) },
"SplitAfterN": func(s, sep string, n int) []string { return strings.SplitAfterN(s, sep, n) },
"SplitN": func(s, sep string, n int) []string { return strings.SplitN(s, sep, n) },
"Title": func(s string) string { return strings.Title(s) },
"ToLower": func(s string) string { return strings.ToLower(s) },
//"ToLowerSpecial": func(_case unicode.SpecialCase, s string) string { return strings.ToLowerSpecial(_case, s) },
"ToTitle": func(s string) string { return strings.ToTitle(s) },
//"ToTitleSpecial": func(_case unicode.SpecialCase, s string) string { return strings.ToTitleSpecial(_case, s) },
"ToUpper": func(s string) string { return strings.ToUpper(s) },
//"ToUpperSpecial": func(_case unicode.SpecialCase, s string) string { return strings.ToUpperSpecial(_case, s) },
"Trim": func(s string, cutset string) string { return strings.Trim(s, cutset) },
//"TrimFunc": func(s string, f func(rune) bool) string { return strings.TrimFunc(s, f) },
"TrimLeft": func(s string, cutset string) string { return strings.TrimLeft(s, cutset) },
//"TrimLeftFunc": func(s string, f func(rune) bool) string { return strings.TrimLeftFunc(s, f) },
"TrimPrefix": func(s, prefix string) string { return strings.TrimPrefix(s, prefix) },
"TrimRight": func(s string, cutset string) string { return strings.TrimRight(s, cutset) },
//"TrimRightFunc": func(s string, f func(rune) bool) string { return strings.TrimRightFunc(s, f) },
"TrimSpace": func(s string) string { return strings.TrimSpace(s) },
"TrimSuffix": func(s, suffix string) string { return strings.TrimSuffix(s, suffix) },
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment