Documentation
¶
Overview ¶
Package lang ports a selection of lodash "Lang" and utility helpers to Go.
The predicate helpers (IsEmpty, IsNil, IsEqual, ...) operate on values of type any and use the reflect package to inspect dynamic types, mirroring the dynamic behaviour of the original JavaScript utilities. The generic helpers (Times, Identity, Constant, ...) use Go generics where a typed API is more natural than reflection.
Index ¶
- func CastArray(value any) []any
- func Constant[T any](value T) func() T
- func DefaultTo(value, defaultValue any) any
- func Eq(a, b any) bool
- func Gt(a, b any) bool
- func Gte(a, b any) bool
- func Identity[T any](value T) T
- func IsArray(value any) bool
- func IsBool(value any) bool
- func IsEmpty(value any) bool
- func IsEqual(a, b any) bool
- func IsError(value any) bool
- func IsFunc(value any) bool
- func IsMap(value any) bool
- func IsNil(value any) bool
- func IsNumber(value any) bool
- func IsPlainObject(value any) bool
- func IsPointer(value any) bool
- func IsString(value any) bool
- func IsZero(value any) bool
- func Lt(a, b any) bool
- func Lte(a, b any) bool
- func Noop()
- func Once[T any](fn func() T) func() T
- func Range(n int) []int
- func Times[T any](n int, fn func(i int) T) []T
- func ToArray(value any) []any
- func ToFinite(value any) float64
- func ToInteger(value any) int
- func ToNumber(value any) float64
- func ToString(value any) string
- func UniqueId(prefix string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CastArray ¶
CastArray wraps value in a []any unless it is already a slice or array, in which case its elements are copied into a fresh []any. A nil value yields an empty slice.
func Constant ¶
func Constant[T any](value T) func() T
Constant returns a function that always returns value.
func DefaultTo ¶
DefaultTo returns value unless it is nil or (for numbers) NaN, in which case it returns defaultValue.
func Eq ¶
Eq reports whether a and b are equivalent using deep equality, treating two NaN values as equal (unlike Go's == operator).
func Gt ¶
Gt reports whether a is greater than b. Numbers are compared numerically and strings lexicographically; mismatched types return false.
func IsEmpty ¶
IsEmpty reports whether value is considered "empty". Nil values, empty strings, empty collections (array, slice, map, channel) and the zero value of numbers, booleans and structs are all treated as empty, matching lodash which treats primitives that are not collections as empty.
func IsEqual ¶
IsEqual performs a deep comparison between two values and reports whether they are equivalent. It uses reflect.DeepEqual semantics.
func IsNil ¶
IsNil reports whether value is nil. It returns true both for an untyped nil interface and for a typed nil whose underlying kind is nilable (pointer, slice, map, channel, function or interface).
func IsNumber ¶
IsNumber reports whether value is any built-in integer, unsigned integer or floating point type. It returns false for complex numbers and booleans.
func IsPlainObject ¶
IsPlainObject reports whether value is a "plain object": a Go map with string keys or a struct (or a pointer to one). This is the closest analogue to a JavaScript object literal.
func IsZero ¶
IsZero reports whether value is the zero value for its type. An untyped nil is considered zero.
func Once ¶
func Once[T any](fn func() T) func() T
Once returns a function that invokes fn at most once. Every call after the first returns the value computed by the first invocation.
func Range ¶
Range returns a slice of ints from 0 up to, but not including, n. A non-positive n yields an empty slice.
func Times ¶
Times invokes fn n times, collecting the results into a slice. If n is not positive, an empty slice is returned.
func ToArray ¶
ToArray converts value into a []any. Slices and arrays are copied element by element, maps yield their values, strings yield their runes (as strings) and any other value yields an empty slice, matching lodash's handling of non-collection values.
func ToFinite ¶
ToFinite converts value to a finite float64. NaN becomes 0 and infinities are clamped to the largest finite float64 magnitude, matching lodash.
func ToInteger ¶
ToInteger converts value to an integer by truncating toward zero. NaN becomes 0 and infinities are clamped to the extremes of int.
func ToNumber ¶
ToNumber converts value to a float64. Booleans become 0 or 1, numeric strings are parsed and unparseable values yield NaN, mirroring lodash's coercion.
Types ¶
This section is empty.