util

package
v0.0.102 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package util provides utility functions and helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddrOrNil

func AddrOrNil[T comparable](x T) *T

AddrOrNil returns nil if x is the zero value for T, or &x otherwise.

func Capitalize

func Capitalize(s string) string

Capitalize capitalizes the first letter of the given string. If the input string is empty, it returns the input string itself.

func ChunkBy

func ChunkBy[T any](items []T, chunkSize int) (chunks [][]T)

ChunkBy splits a slice into chunks of a specified size.

func Contains

func Contains[T comparable](collection []T, element T) bool

Contains checks if the given element is present in the collection.

func CopyMap

func CopyMap[K, V comparable](m map[K]V) map[K]V

CopyMap creates a new copy of the given map and returns it.

func Difference

func Difference[T comparable](list1 []T, list2 []T) ([]T, []T)

Difference returns the difference between two collections.

func Filter

func Filter[T any](collection []T, f func(e T, i int) bool) []T

Filter applies a filtering function to a collection and returns a new slice containing the elements that satisfy the provided predicate function.

func Float32ToFloat64

func Float32ToFloat64(v []float32) []float64

Float32ToFloat64 converts a slice of float32 values to a slice of float64 values. It creates a new slice and populates it with the corresponding float64 values.

func Float64ToFloat32

func Float64ToFloat32(v []float64) []float32

Float64ToFloat32 converts a slice of float64 values to a slice of float32 values. It creates a new slice and populates it with the corresponding float32 values.

func Intersect

func Intersect[T comparable](list1 []T, list2 []T) []T

Intersect returns the intersection between two collections.

func KeyDifference

func KeyDifference(map1, map2 map[string]any) []string

KeyDifference finds the keys that are present in one map and not in the other map.

func Keys

func Keys[M ~map[K]V, K comparable, V any](m M) []K

Keys returns the keys of the map m. The keys will be an indeterminate order.

func Map

func Map[T, U any](ts []T, f func(e T, i int) U) []U

Map manipulates a slice and transforms it to a slice of another type.

func MergeMaps

func MergeMaps[M ~map[K]V, K comparable, V any](src ...M) M

MergeMaps merges multiple maps into a single map and returns the merged result.

func Min added in v0.0.102

func Min[T Number](a, b T) T

func OmitByKeys

func OmitByKeys[K comparable, V any](in map[K]V, keys []K) map[K]V

OmitByKeys creates a new map by omitting key-value pairs from the input map based on the specified keys.

func PTR added in v0.0.98

func PTR[T comparable](x T) *T

PTR returns a pointer to the given value x.

func ParseHTMLAndGetStrippedStrings

func ParseHTMLAndGetStrippedStrings(htmlContent string) (string, error)

ParseHTMLAndGetStrippedStrings parses the HTML content and returns the stripped strings. It uses the goquery package to extract the text from HTML elements.

func StructToMap

func StructToMap(obj any) map[string]any

func SumInt

func SumInt[T int | uint](slice []T) T

SumInt calculates the sum of all integers in the given slice.

func Uniq

func Uniq[T comparable](collection []T) []T

Uniq returns a new slice containing unique elements from the given collection.

Types

type Float added in v0.0.102

type Float interface {
	~float32 | ~float64
}

type Integer added in v0.0.102

type Integer interface {
	Signed | Unsigned
}

type Number added in v0.0.102

type Number interface {
	Integer | Float
}

type Set

type Set[K comparable] struct {
	// contains filtered or unexported fields
}

func NewSet

func NewSet[K comparable]() Set[K]

NewSet returns an empty set.

func SetOf

func SetOf[K comparable](elements ...K) Set[K]

SetOf returns a new set initialized with the given elements

func (Set[K]) Clear

func (s Set[K]) Clear()

Clear removes all elements from the set.

func (Set[K]) Each

func (s Set[K]) Each(fn func(key K))

Each calls 'fn' on every element in the set in no particular order.

func (Set[K]) Has

func (s Set[K]) Has(element K) bool

Has returns true only if the element is in the set.

func (Set[K]) Put

func (s Set[K]) Put(element K)

Put adds the element to the set.

func (Set[K]) Remove

func (s Set[K]) Remove(element K)

Remove removes the element from the set.

func (Set[K]) Size

func (s Set[K]) Size() int

Size returns the number of elements in the set.

func (Set[K]) ToSlice

func (s Set[K]) ToSlice() []K

type Signed added in v0.0.102

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned added in v0.0.102

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Jump to

Keyboard shortcuts

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