maputil

package
v0.63.3 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2024 License: MIT Imports: 11 Imported by: 25

Documentation

Index

Constants

View Source
const (
	SortNameAsc   = "name asc"
	SortNameDesc  = "name desc"
	SortValueAsc  = "value asc"
	SortValueDesc = "value desc"
)

Variables

View Source
var (
	ErrKeyNotExist = errors.New("key not found")
	ErrNotString   = errors.New("value not string")
)
View Source
var ErrMapDuplicateValues = errors.New("map has duplicate values")

Functions

func DuplicateValues added in v0.43.0

func DuplicateValues[C comparable](m map[C]C) map[C][]C

func IsSubset added in v0.62.0

func IsSubset[C comparable, K comparable](m, submap map[C]K) bool

IsSubset checks to see if `submap` is a subset of `m`.

func IsSubsetOrValues added in v0.62.0

func IsSubsetOrValues[C comparable, K comparable](m map[C]K, submap map[C][]K) bool

IsSubsetOrValues checks to see if any of the values of `submap` are present in `a` where all keys exist`.

func KeyValueSliceCounts added in v0.50.0

func KeyValueSliceCounts[K comparable, V any](m map[K][]V) map[K]int

func Keys added in v0.45.0

func Keys[K constraints.Ordered, V any](m map[K]V) []K

Keys returns a list of sorted keys.

func KeysExist added in v0.43.0

func KeysExist[K comparable, V any](m map[K]V, keys []K, requireAll bool) bool

KeysExist checks to verify if a set of keys exists within a map with string keys. If `requireAll` is set, then all keys must be present for the function to return `true`. If `requireAll` is not set, then only one key must exist for the function to return `true`.

func MapSSToKeyValues

func MapSSToKeyValues(kvs map[string]string, sep string) string

func MapStringMapStringIntFuncExactMatch

func MapStringMapStringIntFuncExactMatch(valWant int) func(string, string, int) bool

MapStringMapStringIntFuncExactMatch returns a match function that matches an exact vaule.

func MapStringMapStringIntFuncIncludeAll

func MapStringMapStringIntFuncIncludeAll(str1, str2 string, val int) bool

MapStringMapStringIntFuncIncludeAll returns match function that matches all values.

func NumberValuesAverage added in v0.41.0

func NumberValuesAverage[K comparable, V constraints.Float | constraints.Integer](m map[K]V) float64

NumberValuesAverage returns a `float64` average of a map's values.

func NumberValuesMergeSum added in v0.50.0

func NumberValuesMergeSum[K comparable, V constraints.Float | constraints.Integer](m ...map[K]V) map[K]V

func StringKeys

func StringKeys[V any](m map[string]V, xf func(s string) string) []string

StringKeys takes a map where the keys are strings and reurns a slice of key names. An optional transform function, `xf`, can be supplied along with an option to sort the results. If both transform and sort are requested, the sort is performed on the transformed strings.

func UniqueValues added in v0.43.0

func UniqueValues[C comparable](m map[C]C) bool

UniqueValues returns `true` if the number of unique values is the same as the number of keys.

func ValueIntOrDefault added in v0.47.0

func ValueIntOrDefault[K comparable, V constraints.Integer](m map[K]V, key K, def V) V

func ValueStringOrDefault added in v0.47.0

func ValueStringOrDefault[K comparable](m map[K]string, key K, def string) string

func Values added in v0.45.0

func Values[K constraints.Ordered, V any](m map[K]V) []V

func ValuesSorted added in v0.47.0

func ValuesSorted[K comparable, V constraints.Ordered](m map[K]V) []V

ValuesSorted returns a string slice of sorted values.

Types

type MapInt64Int64

type MapInt64Int64 map[int64]int64

func (MapInt64Int64) KeysSorted

func (m MapInt64Int64) KeysSorted() []int64

func (MapInt64Int64) ValuesSortedByKeys

func (m MapInt64Int64) ValuesSortedByKeys() []int64

type MapStrAny added in v0.49.0

type MapStrAny map[string]any

MapStrAny represents a `map[string]any`

func (MapStrAny) ValueString added in v0.49.0

func (msa MapStrAny) ValueString(k string, errOnNotExist bool) (string, error)

type MapStringInt

type MapStringInt map[string]int

MapStringInt represents a `map[string]int`

func NewMapStringIntSlice

func NewMapStringIntSlice(slice []string) MapStringInt

func (MapStringInt) Add

func (msi MapStringInt) Add(key string, val int)

Add adds the value of `val` to `key`.

func (MapStringInt) Exists

func (msi MapStringInt) Exists(key string) bool

Exists returns a boolean whether a string key exists.

func (MapStringInt) Keys

func (msi MapStringInt) Keys(sortKeys bool) []string

Keys returns a string slice of the map's keys.

func (MapStringInt) MinMaxValues

func (msi MapStringInt) MinMaxValues() (int, int)

MinMaxValues returns the minium and maximum values of the `map[string]int`.

func (MapStringInt) MustGet

func (msi MapStringInt) MustGet(key string, defaultValue int) int

MustGet returns a value or a default.

func (MapStringInt) Set

func (msi MapStringInt) Set(key string, val int)

Set sets the value of `val` to `key`.

func (MapStringInt) Sorted

func (msi MapStringInt) Sorted(sortBy string) []Record

Sorted returns a set of key names and values sorted by the sort type.

type MapStringMapStringInt

type MapStringMapStringInt map[string]map[string]int

MapStringMapStringInt represents a `map[string]map[string]int`

func (MapStringMapStringInt) Add

func (msmsi MapStringMapStringInt) Add(str1, str2 string, val int)

Add supports adding values to `MapStringMapStringInt` easily.

func (MapStringMapStringInt) Counts

func (msmsi MapStringMapStringInt) Counts(sep string) (int, int)

Counts returns key counts regardless of value.

func (MapStringMapStringInt) CountsWithVal

func (msmsi MapStringMapStringInt) CountsWithVal(wantVal int, sep string) (int, int)

CountsWithVal returns key counts with the desired value.

func (MapStringMapStringInt) Flatten

func (msmsi MapStringMapStringInt) Flatten(prefix, sep string, fnInclude func(string, string, int) bool, dedupe, sortResults bool) []string

Flatten returns the values in the `MapStringMapStringInt` in an string slice.

func (MapStringMapStringInt) Set

func (msmsi MapStringMapStringInt) Set(str1, str2 string, val int)

Set supports setting values to `MapStringMapStringInt` easily.

type MapStringSlice

type MapStringSlice url.Values

func NewMapStringSlice added in v0.37.15

func NewMapStringSlice() MapStringSlice

func (MapStringSlice) Add

func (mss MapStringSlice) Add(key, value string)

Add adds a key and value to the `map[string][]string`. It will panic on a nil struct, so do not precede with `var mss MapStringSlice`

func (MapStringSlice) CondenseSpace added in v0.62.8

func (mss MapStringSlice) CondenseSpace(dedupeVals, sortVals bool) map[string][]string

CondenseSpace will trim spaces for keys and values, and remove empty values. It will also optionally dedupe and sort values.

func (MapStringSlice) Flatten added in v0.59.0

func (mss MapStringSlice) Flatten(useLast, skipEmpty bool) map[string]string

Flatten converts a `map[string][]string{}` to a `map[string]string{}`. The default is to use the first value unless `useLast` is set to true, in which case the last value is used. The default is to use a key with an empty string to represent a key with an empty slice, unless `skipEmpty` is set to `true`, in which case the key with an empty slice is not represented.

func (MapStringSlice) FlattenAny added in v0.59.0

func (mss MapStringSlice) FlattenAny(useLast, skipEmpty bool) map[string]any

FlattenAny converts the results of `Flatten()` to a `map[string]any{}`.`

func (MapStringSlice) Keys added in v0.43.0

func (mss MapStringSlice) Keys() []string

func (MapStringSlice) KeysByValueCounts added in v0.40.8

func (mss MapStringSlice) KeysByValueCounts() map[int][]string

KeysByValueCounts returns a `map[int][]string` where the key is the count of values and the values are the keys with that value count.

func (MapStringSlice) Lines added in v0.62.0

func (mss MapStringSlice) Lines(m map[string][]string, keyPrefix, valPrefix string) []string

func (MapStringSlice) ReveseFlatten added in v0.62.7

func (mss MapStringSlice) ReveseFlatten() map[string]string

func (MapStringSlice) Sort added in v0.37.13

func (mss MapStringSlice) Sort(dedupe bool)

type MapStringString added in v0.53.0

type MapStringString map[string]string

func ParseMapStringString added in v0.53.0

func ParseMapStringString(s string) (MapStringString, error)

func (MapStringString) Encode added in v0.53.0

func (m MapStringString) Encode() string

Encode encodes the values into “URL encoded” form ("bar=baz&foo=quux") sorted by key.

func (MapStringString) Get added in v0.53.0

func (m MapStringString) Get(key string) string

func (MapStringString) Gets added in v0.53.0

func (m MapStringString) Gets(inclNonMatches bool, keys []string) []string

func (MapStringString) Subset added in v0.54.0

func (m MapStringString) Subset(keys []string, trimSpace, inclEmpty, inclUnknown bool) MapStringString

Subset returns a subset of a `MapStringString`. `trimSpace` removes leading/trailing spaces on from the source values. `inclEmpty` includes keys where the value matches the empty string. `inclUnknown` adds desired keys in the resulting map which are not known in the source map.

type Record

type Record struct {
	Name  string
	Value int
}

type RecordSet

type RecordSet []Record

func (RecordSet) Markdown

func (rs RecordSet) Markdown(prefix, sep string, countFirst, addTotal bool) string

func (RecordSet) Total

func (rs RecordSet) Total() int

Jump to

Keyboard shortcuts

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