Documentation
¶
Overview ¶
Package filter provides template filter functions for string manipulation, array operations, date formatting, number formatting, and mathematical computations.
Index ¶
- Variables
- func Abs(input interface{}) (float64, error)
- func Append(input, toAppend string) string
- func AtLeast(input interface{}, minimum interface{}) (float64, error)
- func AtMost(input interface{}, maximum interface{}) (float64, error)
- func Average(input interface{}) (float64, error)
- func Bytes(input interface{}) (string, error)
- func Camelize(input string) string
- func Capitalize(input string) string
- func Ceil(input interface{}) (float64, error)
- func Dasherize(input string) string
- func Date(input interface{}, format string) (string, error)
- func Day(input interface{}) (int, error)
- func Default(input, defaultValue string) string
- func Divide(input interface{}, divisor interface{}) (float64, error)
- func Extract(input interface{}, key string) (interface{}, error)
- func First(input interface{}) (interface{}, error)
- func Floor(input interface{}) (float64, error)
- func Index(input interface{}, index int) (interface{}, error)
- func Join(input interface{}, separator string) (string, error)
- func Last(input interface{}) (interface{}, error)
- func Length(input string) int
- func Lower(input string) string
- func Map(input interface{}, key string) ([]interface{}, error)
- func Max(input interface{}) (float64, error)
- func Min(input interface{}) (float64, error)
- func Minus(input interface{}, subtrahend interface{}) (float64, error)
- func Modulo(input interface{}, modulus interface{}) (float64, error)
- func Month(input interface{}) (int, error)
- func MonthFull(input interface{}) (string, error)
- func Number(input interface{}, format string) (string, error)
- func Ordinalize(number int) string
- func Pascalize(input string) string
- func Pluralize(count int, singular, plural string) string
- func Plus(input interface{}, addend interface{}) (float64, error)
- func Prepend(input, toPrepend string) string
- func Random(input interface{}) (interface{}, error)
- func Remove(input, toRemove string) string
- func Replace(input, old, replacement string) string
- func Reverse(input interface{}) ([]interface{}, error)
- func Round(input interface{}, precision interface{}) (float64, error)
- func Shuffle(input interface{}) ([]interface{}, error)
- func Size(input interface{}) (int, error)
- func Slugify(input string) string
- func Split(input, delimiter string) []string
- func Sum(input interface{}) (float64, error)
- func TimeAgo(input interface{}) (string, error)
- func Times(input interface{}, multiplier interface{}) (float64, error)
- func Titleize(input string) string
- func Trim(input string) string
- func Truncate(input string, maxLength int) string
- func TruncateWords(input string, maxWords int) string
- func Unique(input interface{}) ([]interface{}, error)
- func Upper(input string) string
- func Week(input interface{}) (int, error)
- func Weekday(input interface{}) (string, error)
- func Year(input interface{}) (int, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotNumeric indicates the input is not a numeric type ErrNotNumeric = errors.New("input is not numeric") // ErrInvalidTimeFormat indicates the input has an invalid time format ErrInvalidTimeFormat = errors.New("input has an invalid time format") // ErrUnsupportedType indicates the input is of an unsupported type ErrUnsupportedType = errors.New("input is of an unsupported type") // ErrNotSlice indicates the expected input should be a slice ErrNotSlice = errors.New("expected input to be a slice") // ErrEmptySlice indicates the slice is empty ErrEmptySlice = errors.New("slice is empty") // ErrInvalidArguments indicates invalid number of arguments ErrInvalidArguments = errors.New("invalid number of arguments") // ErrKeyNotFound indicates the key was not found ErrKeyNotFound = errors.New("key not found") // ErrIndexOutOfRange indicates the index is out of range ErrIndexOutOfRange = errors.New("index out of range") // ErrInvalidKeyType indicates an invalid key type ErrInvalidKeyType = errors.New("invalid key type") // ErrDivisionByZero indicates division by zero ErrDivisionByZero = errors.New("division by zero") // ErrModulusByZero indicates modulus by zero ErrModulusByZero = errors.New("modulus by zero") // ErrUnsupportedSizeType indicates the input type is not supported by the size filter ErrUnsupportedSizeType = errors.New("size filter expects a slice, array, or map") )
Functions ¶
func Camelize ¶
Camelize converts a string to camelCase, lowercasing the first letter of the first segment and capitalizing the first letter of each subsequent segment.
func Capitalize ¶
Capitalize will capitalize the first letter of the string.
func Dasherize ¶
Dasherize converts a string to a lowercased, dashed string, removing non-alphanumeric characters.
func Date ¶
Date formats a timestamp into a specified format. Returns a string representation of the date.
func Divide ¶
Divide divides the first value by the second, including error handling for division by zero.
func Extract ¶
Extract retrieves a value from input using dot-separated key notation. It supports maps, slices, arrays, structs, pointers, and interfaces.
The key uses dot notation for nested access:
- "user.name" accesses the "name" field of "user"
- "items.0.title" accesses the "title" field of the first item
- "matrix.1.0" accesses element [1][0] of a 2D array
Supported input types:
- map[string]interface{} and similar map types
- []interface{} and other slice types
- Arrays (including multi-dimensional)
- Structs with json tags or exported field names
- Pointers to any of the above
- Interfaces containing any of the above
Returns ErrUnsupportedType if input is nil. Returns ErrKeyNotFound if the key path doesn't exist. Returns ErrIndexOutOfRange for invalid array/slice indices. Returns ErrInvalidKeyType for invalid path navigation.
func First ¶
func First(input interface{}) (interface{}, error)
First returns the first element of a slice.
func Last ¶
func Last(input interface{}) (interface{}, error)
Last returns the last element of a slice.
func Map ¶
Map returns a slice of values for a specified key from each map in the input slice. If the key does not exist in an item, the corresponding value in the result slice will be nil.
func Ordinalize ¶
Ordinalize converts a numeric input to its ordinal English version as a string.
func Pascalize ¶
Pascalize converts a string to PascalCase, capitalizing the first letter of each segment.
func Pluralize ¶
Pluralize outputs the singular or plural version of a string based on the value of a number.
func Random ¶
func Random(input interface{}) (interface{}, error)
Random selects a random element from a slice.
func Replace ¶
Replace replaces all occurrences of a substring with another string in the input string.
func Reverse ¶
func Reverse(input interface{}) ([]interface{}, error)
Reverse reverses the order of elements in a slice.
func Shuffle ¶
func Shuffle(input interface{}) ([]interface{}, error)
Shuffle randomly rearranges the elements of the slice.
func Size ¶
Size returns the size (length) of a collection (slice, array, or map). For string length, use the Length function in string.go instead.
func Slugify ¶
Slugify converts a string into a URL-friendly "slug", transliterating Unicode characters to ASCII and replacing or removing special characters.
func TimeAgo ¶
TimeAgo returns a human-readable string representing the time difference between the current time and the input date.
func Truncate ¶
Truncate truncates a string to a specified length and appends "..." if it was longer.
func TruncateWords ¶
TruncateWords truncates a string to a specified number of words and appends "..." if it was longer.
func Unique ¶
func Unique(input interface{}) ([]interface{}, error)
Unique removes duplicate elements from a slice.
Types ¶
This section is empty.