templateutils

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var UtilityFunctions = template.FuncMap{
	"split":                strings.Split,
	"join":                 strings.Join,
	"basename":             filepath.Base,
	"filterMatch":          FilterMatch,
	"mapString":            MapString,
	"zipToMap":             ZipToMap,
	"keysToMapWithDefault": KeysToMapWithDefault,
	"replace":              ReplaceAllRegex,
	"hasSuffix":            HasSuffix,
	"toLower":              strings.ToLower,
	"add":                  Add,
	"sub":                  Sub,
	"last":                 Last,
	"makeSlice":            MakeSlice,
	"appendSlice":          AppendSlice,
	"sliceContains":        SliceContains,
	"matches":              Matches,
}

Functions

func Add added in v0.7.0

func Add(args ...int) int

Add returns the sum of all the arguments.

func AppendSlice added in v0.7.0

func AppendSlice(slice []any, value any) []any

AppendSlice appends a value to a slice and returns the updated slice.

func FileBase added in v0.7.0

func FileBase(path string) string

FileBase returns the last element of a filepath.

func FileSep added in v0.7.0

func FileSep() string

FileSep returns the separator for the current OS.

func FileTrimExtFunc added in v0.7.0

func FileTrimExtFunc(path string) string

FileTrimExtFunc returns the path without the extension.

func FilterMatch added in v0.7.0

func FilterMatch(pattern string, values []string) ([]string, error)

FilterMatch returns a json array by filtering the values array with the regex pattern

func HasSuffix added in v0.7.0

func HasSuffix(s, suffix string) bool

HasSuffix checks if a string has a specific suffix.

func KeysToMapWithDefault added in v0.7.0

func KeysToMapWithDefault(defaultValue any, keys []string) (map[string]any, error)

KeysToMapWithDefault returns a json map by mapping the keys array to the static defaultValue Example keysToMapWithDefault(0, ['a', 'b']) => {"a": 0, "b": 0}

func Last added in v0.7.0

func Last(list any) (any, error)

Last returns the last element of a list.

func MakeSlice added in v0.7.0

func MakeSlice() []any

MakeSlice creates and returns a new slice of any type.

func MapString added in v0.7.0

func MapString(pattern, replace string, values []string) ([]string, error)

MapString takes in a regex pattern and replacement as well as a json array of strings roughly `unmarshal value | sed s/pattern/replace/g | marshal`

func Matches added in v0.7.0

func Matches(pattern, value string) (bool, error)

Matches returns true if the value matches the regex pattern.

func MustTemplate

func MustTemplate(fs embed.FS, name string) *template.Template

func ReplaceAll added in v0.7.0

func ReplaceAll(s string, old string, new string) string

ReplaceAll replaces all occurrences of old with new in s.

func ReplaceAllRegex added in v0.7.0

func ReplaceAllRegex(pattern, replace, value string) (string, error)

ReplaceAllRegex replaces all occurrences of the regex pattern with the replace string in value.

func SliceContains added in v0.7.0

func SliceContains(slice []any, value any) bool

SliceContains checks if a slice contains a specific value.

func Sub added in v0.7.0

func Sub(args ...int) int

Sub returns the difference of all the arguments.

func ToJSON added in v0.7.0

func ToJSON(v any) (string, error)

ToJSON converts any value to a JSON string.

func ToJSONPretty added in v0.7.0

func ToJSONPretty(v any) (string, error)

ToJSONPretty converts any value to a pretty-printed JSON string.

func WithCommonFuncs added in v0.7.0

func WithCommonFuncs(funcMap template.FuncMap) template.FuncMap

func ZipToMap added in v0.7.0

func ZipToMap(keys []string, valuesArg any) (map[string]any, error)

ZipToMap returns a json map by zipping the keys and values arrays Example: zipToMap(['a', 'b'], [1, 2]) => {"a": 1, "b": 2}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL