obj

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package obj provides utility functions for object/struct/map manipulation.

Index

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

func Get[T any](obj map[string]any, path string) (T, bool)

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]

Jump to

Keyboard shortcuts

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