functions

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

All returns all of the templating functions

func Bracket added in v0.6.3

func Bracket(s interface{}) string

Bracket adds brackets around the given string

func BracketWith added in v0.6.3

func BracketWith(b string, s interface{}) (string, error)

BracketWith adds brackets of a given type around the given string

func Contains added in v0.7.2

func Contains(list interface{}, item interface{}) (bool, error)

Contains returns true if the item is present in the list

Example
helperApplyAndRenderTemplate(contains, a)
Output:

{{.a}}              = [a b c d e]
{{contains .a "x"}} = false
{{contains .a "d"}} = true
{{contains .a 3}}   = false

func Filter added in v0.7.2

func Filter(list interface{}, item interface{}) (interface{}, error)

Filter returns list with all instances of item removed

Example
helperApplyAndRenderTemplate(filter, a)
Output:

{{.a}}            = [a b c d e]
{{filter .a "x"}} = [a b c d e]
{{filter .a 3}}   = [a b c d e]
{{filter .a "c"}} = [a b d e]

{{.b}}            = [f o o b a r]
{{filter .b "o"}} = [f b a r]

func First

func First(list interface{}) (interface{}, error)

First returns the head of a list

Example
helperApplyAndRenderTemplate(first, a)
Output:

{{.a}}       = [a b c d e]
{{first .a}} = a

func FormatJSON added in v0.7.0

func FormatJSON(indent string, j string) (string, error)

FormatJSON returns the given json string, formatted with the given indent string

func FromBase64 added in v0.8.1

func FromBase64(s string) (string, error)

FromBase64 decodes the given encoded string to plain

func GenerateIncludeFn

func GenerateIncludeFn(t *template.Template) func(string, interface{}) (string, error)

GenerateIncludeFn creates a function to be used as an "include" function in templates

func Indent

func Indent(t int, content string) string

Indent prints the given string with the given number of spaces prepended before each line

func IsZero added in v0.6.3

func IsZero(val interface{}) bool

IsZero returns true if the value given corresponds to it's types zero value, points to something zero valued, or if it's a type with a length which is 0

func Join added in v0.7.0

func Join(list interface{}) (string, error)

Join joins the given strings together

func JoinWith added in v0.7.0

func JoinWith(glue string, list interface{}) (string, error)

JoinWith joins the given strings together using the given string as glue

func Last

func Last(list interface{}) (interface{}, error)

Last returns the last item of a list

Example
helperApplyAndRenderTemplate(last, a)
Output:

{{.a}}      = [a b c d e]
{{last .a}} = e

func List added in v0.7.2

func List(s ...interface{}) (interface{}, error)

List returns a new list comprised of the given elements

Example
helperApplyAndRenderTemplate(list, a)
Output:

{{list "a" "b" "c"}} = [a b c]
{{list "a" 5 false}} = [a 5 false]
{{range list 1 2 3}}{{bracket .}}{{end}} = (1)(2)(3)

func Newline

func Newline(c ...int) string

Newline prints a newline (handy for trying to format templates)

func Now

func Now() string

Now returns the current time in the format "2006-01-02T15:04:05Z07:00"

func PadLeft

func PadLeft(n int, s string) string

PadLeft prints the given string in the given number of columns, left aligned

func PadRight

func PadRight(n int, s string) string

PadRight prints the given string in the given number of columns, right aligned

func Pop added in v0.7.2

func Pop(list interface{}) (interface{}, error)

Pop removes the first element of the list, returning the list

Example
helperApplyAndRenderTemplate(pop, a)
Output:

{{.a}}     = [a b c d e]
{{pop .a}} = [a b c d]

func Prefix added in v0.8.0

func Prefix(prefix string, t int, content string) string

Prefix prints the given string with the given number of 'prefix' prepended before each line

func Push added in v0.7.2

func Push(list interface{}, item interface{}) (interface{}, error)

Push returns the list with item appended

Example
helperApplyAndRenderTemplate(push, a)
Output:

{{.a}}          = [a b c d e]
{{push .a "x"}} = [a b c d e x]

func Rep

func Rep(n int, s ...string) string

Rep repeats the given string(s) the given number of times

func Rest

func Rest(list interface{}) (interface{}, error)

Rest / Shift returns the tail of a list

Example
helperApplyAndRenderTemplate(rest, a)
Output:

{{.a}}      = [a b c d e]
{{rest .a}} = [b c d e]

func Slice added in v0.7.2

func Slice(i, j int, list interface{}) (interface{}, error)

Slice returns a slice of a list where i is the lower index (inclusive) and j is the upper index (exclusive) to extract

Example
helperApplyAndRenderTemplate(slice, a)
Output:

{{.a}}           = [a b c d e]
{{slice 1 3 .a}} = [b c]

func Space

func Space(n int) string

Space prints a space character the given number of times

func SplitOn added in v0.7.0

func SplitOn(glue string, s string) []string

SplitOn creates an array from the given string by separating it by the glue string

func Suffix added in v0.8.0

func Suffix(suffix string, t int, content string) string

Suffix prints the given string with the given number of 'suffix' appended to each line

func Tab

func Tab(n int) string

Tab prints a tab character the given number of times

func TerminalWidth added in v0.9.0

func TerminalWidth() int

TerminalWidth returns the number of columns that the terminal currently has, or 0 if the program isn't run in one, or it can't otherwise be determined

func TitleCaseWithAbbr added in v0.9.1

func TitleCaseWithAbbr(abbrv interface{}, word string) (string, error)

TitleCaseWithAbbr uppercases the first letter of each word, or the whole word if it matches a given abbreviation

func ToBase64 added in v0.8.1

func ToBase64(s string) string

ToBase64 converts the given string to a base64 encoding

func ToColumn added in v0.9.0

func ToColumn(w int, s string) string

ToColumn formats the given text to not take more than 'w' characters per line, splitting on the space before the word that would take the line over. If no space can be found, the line isn't split (ie words bigger than the line size are printed unsplit)

Example

ExampleToColumn

w := 40
s := "Lorem ipsum dolor\nsit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."

got := ToColumn(w, s)

fmt.Println(got)
Output:

Lorem ipsum dolor
sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.

func ToJSON added in v0.7.0

func ToJSON(val interface{}) (string, error)

ToJSON returns the given value as a json string

func ToYAML added in v0.7.0

func ToYAML(val interface{}) (string, error)

ToYAML returns the given value as a yaml string

func TypeKind added in v0.9.2

func TypeKind(val interface{}) string

TypeKind returns the 'kind” of the given value as a string

func TypeName added in v0.7.0

func TypeName(val interface{}) string

TypeName returns the type of the given value as a string

func Unshift added in v0.7.2

func Unshift(list interface{}, item interface{}) (interface{}, error)

Unshift returns the list with item prepended

Example
helperApplyAndRenderTemplate(unshift, a)
Output:

{{.a}}             = [a b c d e]
{{unshift .a "x"}} = [x a b c d e]

func UppercaseFirst

func UppercaseFirst(s string) string

UppercaseFirst converts the first character in a string to uppercase

func WhenEmpty

func WhenEmpty(d, s interface{}) interface{}

WhenEmpty returns the second argument if the first is "empty", otherwise it returns the first

Types

This section is empty.

Jump to

Keyboard shortcuts

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