util

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 2 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cond

func Cond[T, R any](pairs ...CondPair[T, R]) func(T) (R, bool)

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

func RangeStep(start, end, step int) []int

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 StubFalse

func StubFalse() bool

StubFalse always returns false, mirroring lodash.stubFalse.

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.

func StubTrue

func StubTrue() bool

StubTrue always returns true, mirroring lodash.stubTrue.

func ToIndex

func ToIndex(segment string) (int, bool)

ToIndex parses a single path segment as a non-negative array index, returning the index and true when the segment is a valid unsigned integer literal.

func ToPath

func ToPath(path string) []string

ToPath splits a lodash-style property path into its segments. It understands dot notation and bracket indexing with optional quotes, so ToPath("a.b[0].c") returns ["a", "b", "0", "c"] and ToPath(`a["x.y"]`) returns ["a", "x.y"].

Types

type CondPair

type CondPair[T, R any] struct {
	When func(T) bool
	Then func(T) R
}

CondPair is a single predicate/transform branch for Cond: When selects the branch and Then produces its result.

Jump to

Keyboard shortcuts

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