slices

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SlicesRegistry

type SlicesRegistry struct {
	// contains filtered or unexported fields
}

func NewRegistry

func NewRegistry() *SlicesRegistry

NewRegistry creates a new instance of your registry with the embedded Handler.

func (*SlicesRegistry) Append

func (sr *SlicesRegistry) Append(list any, v any) []any

Append adds an element to the end of the list.

Parameters:

list any - the original list to append to.
v any - the element to append.

Returns:

[]any - the new list with the element appended.

Example:

{{ append ["a", "b"], "c" }} // Output: ["a", "b", "c"]

func (*SlicesRegistry) Chunk

func (sr *SlicesRegistry) Chunk(size int, list any) [][]any

Chunk divides a list into chunks of specified size.

Parameters:

size int - the size of each chunk.
list any - the list to divide.

Returns:

[][]any - a list of chunks.

Example:

{{ chunk 2, ["a", "b", "c", "d"] }} // Output: [["a", "b"], ["c", "d"]]

func (*SlicesRegistry) Compact

func (sr *SlicesRegistry) Compact(list any) []any

Compact removes nil and zero-value elements from a list.

Parameters:

list any - the list to compact.

Returns:

[]any - the list without nil or zero-value elements.

Example:

{{ [0, 1, nil, 2, "", 3] | compact }} // Output: [1, 2, 3]

func (*SlicesRegistry) Concat

func (sr *SlicesRegistry) Concat(lists ...any) any

Concat merges multiple lists into a single list.

Parameters:

lists ...any - the lists to concatenate.

Returns:

any - a single concatenated list containing elements from all provided lists.

Example:

{{ ["c", "d"] | concat ["a", "b"] }} // Output: ["a", "b", "c", "d"]

func (*SlicesRegistry) First

func (sr *SlicesRegistry) First(list any) any

First returns the first element of a list.

Parameters:

list any - the list from which to take the first element.

Returns:

any - the first element of the list.

Example:

{{ [1, 2, 3, 4] | first }} // Output: 1

func (*SlicesRegistry) Has

func (sr *SlicesRegistry) Has(element any, list any) bool

Has checks if the specified element is present in the collection.

Parameters:

element any - the element to search for.
list any - the collection to search.

Returns:

bool - true if the element is found, otherwise false.

Example:

{{ ["value", "other"] | has "value" }} // Output: true

func (*SlicesRegistry) Initial

func (sr *SlicesRegistry) Initial(list any) []any

Initial returns all elements of a list except the last.

Parameters:

list any - the list to process.

Returns:

[]any - the list without the last element.

Example:

{{ [1, 2, 3, 4] | initial }} // Output: [1, 2, 3]

func (*SlicesRegistry) Last

func (sr *SlicesRegistry) Last(list any) any

Last returns the last element of a list.

Parameters:

list any - the list from which to take the last element.

Returns:

any - the last element of the list.

Example:

{{ [1, 2, 3, 4] | last }} // Output: 4

func (*SlicesRegistry) LinkHandler

func (sr *SlicesRegistry) LinkHandler(fh sprout.Handler) error

LinkHandler links the handler to the registry at runtime.

func (*SlicesRegistry) List

func (sr *SlicesRegistry) List(values ...any) []any

List creates a list from the provided elements.

Parameters:

values ...any - the elements to include in the list.

Returns:

[]any - the created list containing the provided elements.

Example:

{{ 1, 2, 3 | list }} // Output: [1, 2, 3]

func (*SlicesRegistry) MustAppend

func (sr *SlicesRegistry) MustAppend(list any, v any) ([]any, error)

MustAppend appends an element to a slice or array, returning an error if the operation isn't applicable.

Parameters:

list any - the original list to append to.
v any - the element to append.

Returns:

[]any - the new list with the element appended.
error - error if the list is nil or not a slice/array.

Example:

{{ mustAppend ["a", "b"], "c"  }} // Output: ["a", "b", "c"], nil

func (*SlicesRegistry) MustChunk

func (sr *SlicesRegistry) MustChunk(size int, list any) ([][]any, error)

MustChunk divides a list into chunks of specified size, returning an error if the list is nil or not a slice/array.

Parameters:

size int - the maximum size of each chunk.
list any - the list to chunk.

Returns:

[][]any - a list of chunks.
error - error if the list is nil or not a slice/array.

Example:

{{ ["a", "b", "c", "d"] | mustChunk 2 }} // Output: [["a", "b"], ["c", "d"]], nil

func (*SlicesRegistry) MustCompact

func (sr *SlicesRegistry) MustCompact(list any) ([]any, error)

MustCompact removes nil or zero-value elements from a list.

Parameters:

list any - the list to compact.

Returns:

[]any - the list without nil or zero-value elements.
error - error if the list is nil or not a slice/array.

Example:

{{ [0, 1, nil, 2, "", 3] | mustCompact }} // Output: [1, 2, 3], nil

func (*SlicesRegistry) MustFirst

func (sr *SlicesRegistry) MustFirst(list any) (any, error)

MustFirst returns the first element of a list.

Parameters:

list any - the list from which to take the first element.

Returns:

any - the first element of the list.
error - error if the list is nil, empty, or not a slice/array.

Example:

{{ [1, 2, 3, 4] | mustFirst }} // Output: 1, nil

func (*SlicesRegistry) MustHas

func (sr *SlicesRegistry) MustHas(element any, list any) (bool, error)

MustHas checks if a specified element is present in a collection and handles type errors.

Parameters:

element any - the element to search for in the collection.
list any - the collection in which to search for the element.

Returns:

bool - true if the element is found, otherwise false.
error - error if the list is not a type that can be searched (not a slice or array).

Example:

{{ [1, 2, 3, 4] | mustHas 3 }} // Output: true, nil

func (*SlicesRegistry) MustInitial

func (sr *SlicesRegistry) MustInitial(list any) ([]any, error)

MustInitial returns all elements of a list except the last.

Parameters:

list any - the list to process.

Returns:

[]any - the list without the last element.
error - error if the list is nil or not a slice/array.

Example:

{{ [1, 2, 3, 4] | mustInitial }} // Output: [1, 2, 3], nil

func (*SlicesRegistry) MustLast

func (sr *SlicesRegistry) MustLast(list any) (any, error)

MustLast returns the last element of a list.

Parameters:

list any - the list from which to take the last element.

Returns:

any - the last element of the list.
error - error if the list is nil, empty, or not a slice/array.

Example:

{{ [1, 2, 3, 4] | mustLast }} // Output: 4, nil

func (*SlicesRegistry) MustPrepend

func (sr *SlicesRegistry) MustPrepend(list any, v any) ([]any, error)

MustPrepend prepends an element to a slice or array, returning an error if the operation isn't applicable.

Parameters:

list any - the original list to prepend to.
v any - the element to prepend.

Returns:

[]any - the new list with the element prepended.
error - error if the list is nil or not a slice/array.

Example:

{{ mustPrepend ["b", "c"], "a" }} // Output: ["a", "b", "c"], nil

func (*SlicesRegistry) MustRest

func (sr *SlicesRegistry) MustRest(list any) ([]any, error)

MustRest returns all elements of a list except the first.

Parameters:

list any - the list to process.

Returns:

[]any - the list without the first element.
error - error if the list is nil or not a slice/array.

Example:

{{ [1, 2, 3, 4] | mustRest }} // Output: [2, 3, 4], nil

func (*SlicesRegistry) MustReverse

func (sr *SlicesRegistry) MustReverse(list any) ([]any, error)

MustReverse returns a new list with the elements in reverse order.

Parameters:

list any - the list to reverse.

Returns:

[]any - the list in reverse order.
error - error if the list is nil or not a slice/array.

Example:

{{ [1, 2, 3, 4] | mustReverse }} // Output: [4, 3, 2, 1], nil

func (*SlicesRegistry) MustSlice

func (sr *SlicesRegistry) MustSlice(list any, indices ...any) (any, error)

MustSlice extracts a slice from a list between two indices.

Parameters:

list any - the list to slice.
indices ...any - the start and optional end indices; if end is omitted,

slices to the end.

Returns:

any - the sliced part of the list.
error - error if the list is nil or not a slice/array.

Example:

{{ mustSlice [1, 2, 3, 4, 5], 1, 3 }} // Output: [2, 3], nil

func (*SlicesRegistry) MustUniq

func (sr *SlicesRegistry) MustUniq(list any) ([]any, error)

MustUniq returns a new slice containing unique elements of the given list, preserving order.

Parameters:

list any - the list from which to remove duplicates.

Returns:

[]any - a list containing only the unique elements.
error - error if the list is nil or not a slice/array.

Example:

{{ ["a", "b", "a", "c"] | mustUniq }} // Output: ["a", "b", "c"], nil

func (*SlicesRegistry) MustWithout

func (sr *SlicesRegistry) MustWithout(list any, omit ...any) ([]any, error)

MustWithout returns a new list excluding specified elements.

Parameters:

list any - the original list.
omit ...any - elements to exclude from the new list.

Returns:

[]any - the list excluding the specified elements.
error - error if the list is nil or not a slice/array.

Example:

{{ mustWithout [1, 2, 3, 4], 2, 4 }} // Output: [1, 3], nil

func (*SlicesRegistry) Prepend

func (sr *SlicesRegistry) Prepend(list any, v any) []any

Prepend adds an element to the beginning of the list.

Parameters:

list any - the original list to prepend to.
v any - the element to prepend.

Returns:

[]any - the new list with the element prepended.

Example:

{{ prepend  ["b", "c"], "a" }} // Output: ["a", "b", "c"]

func (*SlicesRegistry) RegisterFunctions

func (sr *SlicesRegistry) RegisterFunctions(funcsMap sprout.FunctionMap) error

RegisterFunctions registers all functions of the registry.

func (*SlicesRegistry) Rest

func (sr *SlicesRegistry) Rest(list any) []any

Rest returns all elements of a list except the first.

Parameters:

list any - the list to process.

Returns:

[]any - the list without the first element.

Example:

{{ [1, 2, 3, 4] | rest }} // Output: [2, 3, 4]

func (*SlicesRegistry) Reverse

func (sr *SlicesRegistry) Reverse(list any) []any

Reverse returns a new list with the elements in reverse order.

Parameters:

list any - the list to reverse.

Returns:

[]any - the list in reverse order.

Example:

{{ [1, 2, 3, 4] | reverse }} // Output: [4, 3, 2, 1]

func (*SlicesRegistry) Slice

func (sr *SlicesRegistry) Slice(list any, indices ...any) any

Slice extracts a slice from a list between two indices.

Parameters:

list any - the list to slice.
indices ...any - the start and optional end indices; if end is omitted,

slices to the end.

Returns:

any - the sliced part of the list.

Example:

{{ slice [1, 2, 3, 4, 5], 1, 3 }} // Output: [2, 3]

func (*SlicesRegistry) SortAlpha

func (sr *SlicesRegistry) SortAlpha(list any) []string

SortAlpha sorts a list of strings in alphabetical order.

Parameters:

list any - the list of strings to sort.

Returns:

[]string - the sorted list.

Example:

{{ ["d", "b", "a", "c"] | sortAlpha }} // Output: ["a", "b", "c", "d"]

func (*SlicesRegistry) SplitList

func (sr *SlicesRegistry) SplitList(sep string, str string) []string

SplitList divides a string into a slice of substrings separated by the specified separator.

! FUTURE: Rename this function to be more explicit

Parameters:

sep string - the delimiter used to split the string.
str string - the string to split.

Returns:

[]string - a slice containing the substrings obtained from splitting the input string.

Example:

{{ "one, two, three" | splitList ", " }} // Output: ["one", "two", "three"]

func (*SlicesRegistry) StrSlice

func (sr *SlicesRegistry) StrSlice(value any) []string

StrSlice converts a value to a slice of strings, handling various types including []string, []any, and other slice types.

Parameters:

value any - the value to convert to a slice of strings.

Returns:

[]string - the converted slice of strings.

Example:

{{ strSlice any["a", "b", "c"] }} // Output: ["a", "b", "c"]

func (*SlicesRegistry) Uid

func (sr *SlicesRegistry) Uid() string

Uid returns the unique identifier of the registry.

func (*SlicesRegistry) Uniq

func (sr *SlicesRegistry) Uniq(list any) []any

Uniq removes duplicate elements from a list.

Parameters:

list any - the list from which to remove duplicates.

Returns:

[]any - a list containing only unique elements.

Example:

{{ ["a", "b", "a", "c"] | uniq }} // Output: ["a", "b", "c"]

func (*SlicesRegistry) Until

func (sr *SlicesRegistry) Until(count int) []int

func (*SlicesRegistry) UntilStep

func (sr *SlicesRegistry) UntilStep(start, stop, step int) []int

UntilStep generates a slice of integers from 'start' to 'stop' (exclusive), incrementing by 'step'. If 'step' is positive, the sequence increases; if negative, it decreases. The function returns an empty slice if the sequence does not make logical sense (e.g., positive step when start is greater than stop or vice versa).

Parameters:

start int - the starting point of the sequence.
stop int - the endpoint (exclusive) of the sequence.
step int - the increment between elements in the sequence.

Returns:

[]int - a dynamically generated slice of integers based on the input
        parameters, or an empty slice if the parameters are inconsistent
        with the desired range and step.

Example:

{{ 0, 10, 2 | untilStep }} // Output: [0 2 4 6 8]
{{ 10, 0, -2 | untilStep }} // Output: [10 8 6 4 2]

func (*SlicesRegistry) Without

func (sr *SlicesRegistry) Without(list any, omit ...any) []any

Without returns a new list excluding specified elements.

Parameters:

list any - the original list.
omit ...any - elements to exclude from the new list.

Returns:

[]any - the list excluding the specified elements.

Example:

{{ without [1, 2, 3, 4], 2, 4 }} // Output: [1, 3]

Jump to

Keyboard shortcuts

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