Documentation
¶
Overview ¶
Package util ports helpers from lodash's "Util" category (cond, stubArray, stubTrue/False/String, toPath and a stepped range) to idiomatic, generic Go. These are the small building blocks lodash exposes for control flow and for providing constant "stub" values, offered here without any third-party dependency.
Cond builds a function from an ordered list of predicate/transform pairs and runs the transform of the first matching predicate, the Go analogue of lodash.cond and a compact alternative to a switch when the branches are data. The Stub* helpers return fresh empty values (StubArray and StubObject each allocate a new, non-nil container) and the constant StubTrue/StubFalse/ StubString mirror lodash's stubs for use as default callbacks. ToPath parses a lodash-style property path such as "a.b[0].c" into its string segments, and RangeStep generates an arithmetic sequence with an explicit step, filling the gap left by the plain Range helpers.
Everything is deterministic and depends only on the standard library.
Index ¶
- func Cond[T, R any](pairs ...CondPair[T, R]) func(T) (R, bool)
- func RangeStep(start, end, step int) []int
- func StubArray[T any]() []T
- func StubFalse() bool
- func StubObject[K comparable, V any]() map[K]V
- func StubString() string
- func StubTrue() bool
- func ToIndex(segment string) (int, bool)
- func ToPath(path string) []string
- type CondPair
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cond ¶
Cond returns a function that evaluates each pair's predicate in order and, for the first whose predicate is satisfied, returns that pair's transform applied to the argument together with true. If no predicate matches it returns the zero value of R and false. It mirrors lodash.cond.
func RangeStep ¶
RangeStep returns the arithmetic sequence starting at start, advancing by step, up to but not including end. A positive step produces an ascending sequence and a negative step a descending one; a zero step, or a step whose sign cannot reach end, yields an empty slice. It mirrors lodash.range's three-argument form for int.
func StubArray ¶
func StubArray[T any]() []T
StubArray returns a new empty, non-nil slice of T, mirroring lodash.stubArray.
func StubObject ¶
func StubObject[K comparable, V any]() map[K]V
StubObject returns a new empty, non-nil map, mirroring lodash.stubObject.
func StubString ¶
func StubString() string
StubString always returns the empty string, mirroring lodash.stubString.