collections

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package collections provides template functions for manipulating collections such as arrays, maps, and slices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncLooker

type FuncLooker interface {
	GetFunc(name string) (reflect.Value, bool)
}

type Language

type Language interface {
	Collator() *collate.Collator
}

type Namespace

type Namespace struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Namespace provides template functions for the "collections" namespace.

func New

func New(timezone compare.TimeZone, looker FuncLooker, lang Language) *Namespace

New returns a new instance of the collections-namespaced template functions.

func (*Namespace) After

func (ns *Namespace) After(n any, l any) (any, error)

After returns all the items after the first n items in list l.

func (*Namespace) Append

func (ns *Namespace) Append(args ...any) (any, error)

Append appends args up to the last one to the slice in the last argument. This construct allows template constructs like this:

{{ $pages = $pages | append $p2 $p1 }}

Note that with 2 arguments where both are slices of the same type, the first slice will be appended to the second:

{{ $pages = $pages | append .Site.RegularPages }}

func (*Namespace) Apply

func (ns *Namespace) Apply(ctx context.Context, c any, fname string, args ...any) (any, error)

Apply takes an array or slice c and returns a new slice with the function fname applied over it.

func (*Namespace) Complement

func (ns *Namespace) Complement(ls ...any) (any, error)

Complement gives the elements in the last element of ls that are not in any of the others.

All elements of ls must be slices or arrays of comparable types.

The reasoning behind this rather clumsy API is so we can do this in the templates:

{{ $c := .Pages | complement $last4 }}

func (*Namespace) Delimit

func (ns *Namespace) Delimit(ctx context.Context, l, sep any, last ...any) (string, error)

Delimit takes a given list l and returns a string delimited by sep. If last is passed to the function, it will be used as the final delimiter.

func (*Namespace) Dictionary

func (ns *Namespace) Dictionary(values ...any) (map[string]any, error)

Dictionary creates a new map from the given parameters by treating values as key-value pairs. The number of values must be even. The keys can be string slices, which will create the needed nested structure.

func (*Namespace) First

func (ns *Namespace) First(limit any, l any) (any, error)

First returns the first limit items in list l.

func (*Namespace) Group

func (ns *Namespace) Group(key any, items any) (any, error)

Group groups a set of items by the given key. This is currently only supported for Pages.

func (*Namespace) In

func (ns *Namespace) In(l any, v any) (bool, error)

In returns whether v is in the list l. l may be an array or slice.

func (*Namespace) Index

func (ns *Namespace) Index(item any, args ...any) (any, error)

Index returns the result of indexing its first argument by the following arguments. Thus "index x 1 2 3" is, in Go syntax, x[1][2][3]. Each indexed item must be a map, slice, or array.

Adapted from Go stdlib src/text/template/funcs.go.

We deviate from the stdlib mostly because of https://github.com/golang/go/issues/14751.

func (*Namespace) Intersect

func (ns *Namespace) Intersect(l1, l2 any) (any, error)

Intersect returns the common elements in the given sets, l1 and l2. l1 and l2 must be of the same type and may be either arrays or slices.

func (*Namespace) IsSet

func (ns *Namespace) IsSet(c any, key any) (bool, error)

IsSet returns whether a given array, channel, slice, or map in c has the given key defined.

func (*Namespace) KeyVals

func (ns *Namespace) KeyVals(key any, values ...any) (types.KeyValues, error)

KeyVals creates a key and values wrapper.

func (*Namespace) Last

func (ns *Namespace) Last(limit any, l any) (any, error)

Last returns the last limit items in the list l.

func (*Namespace) Merge

func (ns *Namespace) Merge(params ...any) (any, error)

Merge creates a copy of the final parameter in params and merges the preceding parameters into it in reverse order.

Currently only maps are supported. Key handling is case insensitive.

func (*Namespace) NewScratch

func (ns *Namespace) NewScratch() *maps.Scratch

NewScratch creates a new Scratch which can be used to store values in a thread safe way.

func (*Namespace) Querify

func (ns *Namespace) Querify(params ...any) (string, error)

Querify encodes the given params in URL-encoded form ("bar=baz&foo=quux") sorted by key.

func (*Namespace) Reverse

func (ns *Namespace) Reverse(l any) (any, error)

Reverse creates a copy of the list l and reverses it.

func (*Namespace) Seq

func (ns *Namespace) Seq(args ...any) ([]int, error)

Seq creates a sequence of integers from args. It's named and used as GNU's seq.

Examples:

3 => 1, 2, 3
1 2 4 => 1, 3
-3 => -1, -2, -3
1 4 => 1, 2, 3, 4
1 -2 => 1, 0, -1, -2

func (*Namespace) Shuffle

func (ns *Namespace) Shuffle(l any) (any, error)

Shuffle returns list l in a randomized order.

func (*Namespace) Slice

func (ns *Namespace) Slice(args ...any) any

Slice returns a slice of all passed arguments.

func (*Namespace) Sort

func (ns *Namespace) Sort(ctx context.Context, l any, args ...any) (any, error)

Sort returns a sorted copy of the list l.

func (*Namespace) SymDiff

func (ns *Namespace) SymDiff(s2, s1 any) (any, error)

SymDiff returns the symmetric difference of s1 and s2. Arguments must be either a slice or an array of comparable types.

func (*Namespace) Union

func (ns *Namespace) Union(l1, l2 any) (any, error)

Union returns the union of the given sets, l1 and l2. l1 and l2 must be of the same type and may be either arrays or slices. If l1 and l2 aren't of the same type then l1 will be returned. If either l1 or l2 is nil then the non-nil list will be returned.

func (*Namespace) Uniq

func (ns *Namespace) Uniq(l any) (any, error)

Uniq returns a new list with duplicate elements in the list l removed.

func (*Namespace) Where

func (ns *Namespace) Where(ctx context.Context, c, key any, args ...any) (any, error)

Where returns a filtered subset of collection c.

Jump to

Keyboard shortcuts

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