utils

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Flip added in v0.5.1

func Flip[T any](test bool, a, b T) (T, T)

Flip returns `[b, a]` if the test is true, otherwise the values are returned in the given order, `[a, b]`.

Example: `max, min := Flip(x < y, x, y)`

func GetMaxStringLen

func GetMaxStringLen(values []string) int

GetMaxStringLen gets the maximum length of the given strings.

func IsNil

func IsNil(value any) bool

IsNil determines if the value is nil or returns false if the value isn't able to check for nil.

func IsZero

func IsZero[T any](value T) bool

IsZero determines if the given value is equivalent to the zero value of the given type.

This will also return true for any nil type values.

func Keys

func Keys[TKey comparable, TValue any, TMap ~map[TKey]TValue](m TMap) []TKey

Keys gets all the keys for the given map in random order.

func LazyMatcher

func LazyMatcher(pattern string) func(value string) bool

LazyMatcher is a regular expression matcher which lazy compiles the pattern until the first time it is needed.

If the pattern is invalid, this will panic the first time it is called to perform a match. It will panic the same error for any following match attempts.

Example:

var hex = LazyMatcher(`^[0-9A-Fa-f]+$`)
func Foo() {
	...
	isHex := hex(`572A6F`) // true
	...
}

func LazyRegex

func LazyRegex(pattern string) func() *regexp.Regexp

LazyRegex is a regular expression which lazy compiles the pattern until the first time it is needed.

If the pattern is invalid, this will panic the first time it is called to get the regex. It will panic the same error for any following attempts.

func Length

func Length[T any](value T) (length int, ok bool)

Length determines the length of the given value or returns false if the value does not have a length.

func Parse

func Parse[T ParsableConstraint](s string) (T, error)

Parse interprets a string and returns the corresponding value of the given type.

func RemoveZeros

func RemoveZeros[T any, S ~[]T](s S) S

RemoveZeros creates a new slice without modifying the given values which has all the zero values remove from it.

func SetToZero

func SetToZero[T any, S ~[]T](s S, start, stop int)

SetToZero sets the given range of the slice to zero. The start is inclusive, the stop is exclusive.

This will panic if the indices are not valid. The start must be `[0..len)` and stop must be `(start..len]`.

func SortedKeys

func SortedKeys[TKey comparable, TValue any, TMap ~map[TKey]TValue](m TMap, comparer ...comp.Comparer[TKey]) []TKey

SortedKeys gets all the keys from the given map in sorted order.

An optional comparer maybe added to override the default comparer or for types that don't have a default comparer.

func String

func String[T any](value T) string

String gets the string for the given value.

func Strings

func Strings[T any, S ~[]T](s S) []string

Strings gets the strings for all the given values in the slice.

func Ternary added in v0.5.1

func Ternary[T any](test bool, a, b T) T

Ternary returns the `a` value if the given test is true, and the `b` value if the test is false.

Since both the `a` and `b` values are evaluated prior to the ternary, this should only be used when neither takes long to compute or with functions.

For example `value := Ternary(x, 1, -1)` or

   value := Ternary(x,
	     func() int { return foo() - 1 },
	     func() int { return bar()*6 + 2 },
   )()

func TryIsNil

func TryIsNil(value any) (isNil, ok bool)

TryIsNil determines if the value is nil or returns false if the value isn't able to check for nil.

func TypeOf

func TypeOf[T any]() reflect.Type

TypeOf gets the reflect type of the given generic value.

func Values

func Values[TKey comparable, TValue any, TMap ~map[TKey]TValue](m TMap) []TValue

Values gets all the values for the given map in random order.

func Zero

func Zero[T any]() T

Zero gets the zero value for the given type.

Types

type FloatingConstraint

type FloatingConstraint interface {
	~float32 | ~float64
}

FloatingConstraint is a type constraint for floating point types.

type IntConstraint

type IntConstraint interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

IntConstraint is a type constraint for integer types.

type NumConstraint

type NumConstraint interface {
	IntConstraint | FloatingConstraint
}

NumConstraint is a type constraint for numerical types.

type ParsableConstraint

type ParsableConstraint interface {
	~string | ~bool | NumConstraint | ~complex64 | ~complex128
}

ParsableConstraint is the set of types that can be parsed.

type StringMatcher

type StringMatcher func(value string) (start, length int)

StringMatcher is a function for finding the first match in the given string.

This returns the starting index of the first character (UTF-8) in the match and the length of the matched substring. If no match found then start should return -1. If a match is found the length must be greater than zero. For a match, the start index plus the length should not go past the end of the given value.

type Stringer

type Stringer interface{ String() string }

Stringer is an interface for an object which returns a string from a `String()` method.

Jump to

Keyboard shortcuts

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