Documentation
¶
Overview ¶
Package obj provides utility functions for object/struct/map manipulation.
Index ¶
- func Assign[K comparable, V any](dest map[K]V, sources ...map[K]V) map[K]V
- func Clone[K comparable, V any](obj map[K]V) map[K]V
- func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V
- func Get[T any](obj map[string]any, path string) (T, bool)
- func Has[K comparable, V any](obj map[K]V, key K) bool
- func IsEmpty[K comparable, V any](obj map[K]V) bool
- func IsEqual[K comparable, V comparable](obj1, obj2 map[K]V) bool
- func Keys[K comparable, V any](obj map[K]V) []K
- func KeysSorted[K comparable, V any](obj map[K]V) []K
- func MapKeys[K comparable, V any, R comparable](obj map[K]V, iteratee func(K) R) map[R]V
- func MapValues[K comparable, V any, R any](obj map[K]V, iteratee func(V) R) map[K]R
- func Merge[K comparable, V any](dest map[K]V, sources ...map[K]V) map[K]V
- func Omit[K comparable, V any](obj map[K]V, keys ...K) map[K]V
- func OmitBy[K comparable, V any](obj map[K]V, predicate func(V) bool) map[K]V
- func Pick[K comparable, V any](obj map[K]V, keys ...K) map[K]V
- func PickBy[K comparable, V any](obj map[K]V, predicate func(V) bool) map[K]V
- func Size[K comparable, V any](obj map[K]V) int
- func Values[K comparable, V any](obj map[K]V) []V
- type Entry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Assign ¶
func Assign[K comparable, V any](dest map[K]V, sources ...map[K]V) map[K]V
Assign assigns properties of source objects to the destination object. It's similar to Object.assign() in JavaScript. Example: Assign(map[string]any{"a": 1}, map[string]any{"b": 2}) -> map[string]any{"a": 1, "b": 2}
func Clone ¶
func Clone[K comparable, V any](obj map[K]V) map[K]V
Clone creates a shallow clone of an object. Example: Clone(map[string]any{"a": 1, "b": 2}) -> map[string]any{"a": 1, "b": 2}
func FromEntries ¶
func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V
FromEntries returns an object composed from key-value pairs. Example: FromEntries([]Entry[string, int]{{"a", 1}, {"b", 2}}) -> map[string]int{"a": 1, "b": 2}
func Get ¶
Get retrieves a value from a nested map using dot notation path Example: Get(map[string]interface{}{"a": map[string]interface{}{"b": 2}}, "a.b") -> 2
func Has ¶
func Has[K comparable, V any](obj map[K]V, key K) bool
Has checks if path is a direct property of object. Example: Has(map[string]any{"a": 1, "b": 2}, "a") -> true
func IsEmpty ¶
func IsEmpty[K comparable, V any](obj map[K]V) bool
IsEmpty checks if an object is empty. Example: IsEmpty(map[string]int{}) -> true
func IsEqual ¶
func IsEqual[K comparable, V comparable](obj1, obj2 map[K]V) bool
IsEqual performs a deep comparison between two objects to determine if they are equivalent. Example: IsEqual(map[string]int{"a": 1}, map[string]int{"a": 1}) -> true
func Keys ¶
func Keys[K comparable, V any](obj map[K]V) []K
Keys returns an array of object's own enumerable property names. Example: Keys(map[string]any{"a": 1, "b": 2}) -> []string{"a", "b"}
func KeysSorted ¶
func KeysSorted[K comparable, V any](obj map[K]V) []K
KeysSorted returns a sorted array of object's own enumerable property names. Example: KeysSorted(map[string]any{"b": 2, "a": 1}) -> []string{"a", "b"}
func MapKeys ¶
func MapKeys[K comparable, V any, R comparable](obj map[K]V, iteratee func(K) R) map[R]V
MapKeys creates an object with keys generated by running the property names of object through iteratee. Example: MapKeys(map[string]int{"a": 1, "b": 2}, func(k string) string { return k + "x" }) -> map[string]int{"ax": 1, "bx": 2}
func MapValues ¶
func MapValues[K comparable, V any, R any](obj map[K]V, iteratee func(V) R) map[K]R
MapValues creates an object with the same keys as object and values generated by running each property through iteratee. Example: MapValues(map[string]int{"a": 1, "b": 2}, func(v int) int { return v * 2 }) -> map[string]int{"a": 2, "b": 4}
func Merge ¶
func Merge[K comparable, V any](dest map[K]V, sources ...map[K]V) map[K]V
Merge merges properties of source objects into the destination object. Note: This is a simplified version that doesn't do deep merging due to Go's type system limitations. Example: Merge(map[string]any{"a": 1}, map[string]any{"b": 2}) -> map[string]any{"a": 1, "b": 2}
func Omit ¶
func Omit[K comparable, V any](obj map[K]V, keys ...K) map[K]V
Omit creates an object composed of the object properties not included in the keys. Example: Omit(map[string]int{"a": 1, "b": 2, "c": 3}, "a", "c") -> map[string]int{"b": 2}
func OmitBy ¶
func OmitBy[K comparable, V any](obj map[K]V, predicate func(V) bool) map[K]V
OmitBy creates an object composed of the object properties for which predicate returns falsey. Example: OmitBy(map[string]int{"a": 1, "b": 2, "c": 3}, func(v int) bool { return v > 2 }) -> map[string]int{"a": 1, "b": 2}
func Pick ¶
func Pick[K comparable, V any](obj map[K]V, keys ...K) map[K]V
Pick creates an object composed of the picked object properties. Example: Pick(map[string]int{"a": 1, "b": 2, "c": 3}, "a", "c") -> map[string]int{"a": 1, "c": 3}
func PickBy ¶
func PickBy[K comparable, V any](obj map[K]V, predicate func(V) bool) map[K]V
PickBy creates an object composed of the object properties for which predicate returns truthy. Example: PickBy(map[string]int{"a": 1, "b": 2, "c": 3}, func(v int) bool { return v > 2 }) -> map[string]int{"c": 3}
func Size ¶
func Size[K comparable, V any](obj map[K]V) int
Size returns the number of own enumerable properties of an object. Example: Size(map[string]int{"a": 1, "b": 2}) -> 2
func Values ¶
func Values[K comparable, V any](obj map[K]V) []V
Values returns an array of object's own enumerable property values. Example: Values(map[string]int{"a": 1, "b": 2}) -> []int{1, 2}
Types ¶
type Entry ¶
type Entry[K comparable, V any] struct { Key K Value V }
Entry Entries returns an array of key-value pairs. Example: Entries(map[string]int{"a": 1, "b": 2}) -> []Entry[string, int]{{"a", 1}, {"b", 2}}
func Entries ¶
func Entries[K comparable, V any](obj map[K]V) []Entry[K, V]