helpers

package
v1.0.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Empty

func Empty(given any) bool

Empty evaluates the emptiness of the provided value 'given'. It returns true if 'given' is considered empty based on its type. This method is essential for determining the presence or absence of meaningful value across various data types.

Parameters:

given any - the value to be evaluated for emptiness.

Returns:

bool - true if 'given' is empty, false otherwise.

This method utilizes the reflect package to inspect the type and value of 'given'. Depending on the type, it checks for nil pointers, zero-length collections (arrays, slices, maps, and strings), zero values of numeric types (integers, floats, complex numbers, unsigned ints), and false for booleans.

Example:

Empty(nil) // Output: true
Empty("") // Output: true
Empty(0) // Output: true
Empty(false) // Output: true
Empty(struct{}{}) // Output: false

func StrSlice

func 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 - a slice of strings representing the input value.

Example:

strs := StrSlice([]any{"apple", "banana", "cherry"})
fmt.Println(strs) // Output: ["apple", "banana", "cherry"]

func ToString

func ToString(v any) string

ToString converts the input value to a string based on its type.

Parameters:

given any - the value to be converted to a string.

Returns:

string - the string representation of the input value.

func UntilStep

func 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]

Types

This section is empty.

Jump to

Keyboard shortcuts

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