utils

package
v0.0.0-...-41ddf2c Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal[T any](a, b T) bool

Equal determines if the two values are equal.

This will check if the objects are Equatable, otherwise it will fallback to a DeepEqual. This will not check for Equatable within a slice, array, map, etc only in the top level object.

This will not check for Comparable types. Any struct that implements Comparable should also implement Equatable where both agree.

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 ...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 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 Comparable

type Comparable[T any] interface {
	// CompareTo returns a comparison result of this object and the given object.
	//
	// The comparison results should be:
	// `< 0` if this is less than the given other,
	// `== 0` if this equals the given other,
	// `> 0` if this is greater than the given other.
	CompareTo(other T) int
}

Comparable is an object which can be compared against another object.

The given T variable type is typically the type that is implementing this interface.

type Comparer

type Comparer[T any] func(x, y T) int

Comparer returns the comparison result between the two given values.

The comparison results should be: `< 0` if x is less than y, `== 0` if x equals y, `> 0` if x is greater than y.

func ComparableComparer

func ComparableComparer[T Comparable[T]]() Comparer[T]

ComparableComparer returns a comparer which compares the given comparable type.

func ComparerForLess

func ComparerForLess[T any](less IsLessThan[T]) Comparer[T]

LessComparer returns a comparer which compares two values using a IsLessThan function to perform the comparison.

func DefaultComparer

func DefaultComparer[T any]() Comparer[T]

DefaultComparer tries to create a comparer for the given type. If a comparer isn't able to be created, this will return nil.

func Descender

func Descender[T any](cmp Comparer[T]) Comparer[T]

Descender returns a comparer which negates the given comparer.

Typically negating a comparer will change a sort from ascending to descending.

func DurationComparer

func DurationComparer() Comparer[time.Duration]

DurationComparer returns a comparer which compares the given time duration.

func EpsilonComparer

func EpsilonComparer[T NumConstraint](epsilon T) Comparer[T]

EpsilonComparer returns an epsilon comparer which compares the given floating point types.

An epsilon comparator should be used when comparing calculated floating point numbers since calculations may accrue small errors and make the actual value very close to the expected value but not exactly equal. The two floating point values are equal if very close to each other. The values must be within the given epsilon to be considered equal.

The given epsilon must be greater than zero. If the epsilon is less than or equal to zero, this will fallback to an ordered comparer.

func OrderedComparer

func OrderedComparer[T cmp.Ordered]() Comparer[T]

OrderedComparer returns a comparer which compares the given ordered type.

func TimeComparer

func TimeComparer() Comparer[time.Time]

TimeComparer returns a comparer which compares the given time duration.

type Equatable

type Equatable interface {
	// Equals returns true if this object and the given object are equal.
	Equals(other any) bool
}

Equatable is an object which can be checked for equality against another object.

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 IsLessThan

type IsLessThan[T any] func(x, y T) bool

IsLessThan returns true if the given x is less than the given y, otherwise false.

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