slices

package
v1.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: MIT Imports: 8 Imported by: 1

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(args ...any) ([]any, error)

Append 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 - protect against undesired behavior due to migration to new signature.

Example:

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

func (*SlicesRegistry) Chunk

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

Chunk 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"] | chunk 2 }} // Output: [["a", "b"], ["c", "d"]], nil

func (*SlicesRegistry) Compact

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

Compact 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] | compact }} // Output: [1, 2, 3], nil

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, error)

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.
error - error if the list is nil, empty, or not a slice/array.

Example:

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

func (*SlicesRegistry) Flatten

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

Flatten flattens a nested list into a single list of elements.

Parameters:

list any - the list to flatten.

Returns:

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

Example:

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

func (*SlicesRegistry) FlattenDepth

func (sr *SlicesRegistry) FlattenDepth(deep int, list any) ([]any, error)

FlattenDepth flattens a nested list into a single list of elements up to a specified depth.

Parameters:

deep int - the maximum depth to flatten to; -1 for infinite depth.
list any - the list to flatten.

Returns:

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

Example:

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

func (*SlicesRegistry) Has

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

Has 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] | has 3 }} // Output: true, nil

func (*SlicesRegistry) Initial

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

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.
error - error if the list is nil or not a slice/array.

Example:

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

func (*SlicesRegistry) Last

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

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.
error - error if the list is nil, empty, or not a slice/array.

Example:

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

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) Prepend

func (sr *SlicesRegistry) Prepend(args ...any) ([]any, error)

Prepend 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 - protect against undesired behavior due to migration to new signature.

Example:

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

func (*SlicesRegistry) RegisterAliases added in v0.6.0

func (sr *SlicesRegistry) RegisterAliases(aliasesMap sprout.FunctionAliasMap) error

func (*SlicesRegistry) RegisterFunctions

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

RegisterFunctions registers all functions of the registry.

func (*SlicesRegistry) RegisterNotices added in v0.6.0

func (sr *SlicesRegistry) RegisterNotices(notices *[]sprout.FunctionNotice) error

func (*SlicesRegistry) Rest

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

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.
error - error if the list is nil or not a slice/array.

Example:

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

func (*SlicesRegistry) Reverse

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

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.
error - error if the list is nil or not a slice/array.

Example:

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

func (*SlicesRegistry) Slice

func (sr *SlicesRegistry) Slice(args ...any) (any, error)

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.
error - protect against undesired behavior due to migration to new signature.

Example:

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

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, error)

Uniq 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"] | uniq }} // Output: ["a", "b", "c"], nil

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(args ...any) ([]any, error)

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.
error - protect against undesired behavior due to migration to new signature.

Example:

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

Jump to

Keyboard shortcuts

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