lang

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CastArray

func CastArray(value any) []any

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

func DefaultTo(value, defaultValue any) any

DefaultTo returns value unless it is nil or (for numbers) NaN, in which case it returns defaultValue.

func Eq

func Eq(a, b any) bool

Eq reports whether a and b are equivalent using deep equality, treating two NaN values as equal (unlike Go's == operator).

func Gt

func Gt(a, b any) bool

Gt reports whether a is greater than b. Numbers are compared numerically and strings lexicographically; mismatched types return false.

func Gte

func Gte(a, b any) bool

Gte reports whether a is greater than or equal to b.

func Identity

func Identity[T any](value T) T

Identity returns its argument unchanged.

func IsArray

func IsArray(value any) bool

IsArray reports whether value is a Go slice or array.

func IsBool

func IsBool(value any) bool

IsBool reports whether value is a boolean.

func IsEmpty

func IsEmpty(value any) bool

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

func IsEqual(a, b any) bool

IsEqual performs a deep comparison between two values and reports whether they are equivalent. It uses reflect.DeepEqual semantics.

func IsError

func IsError(value any) bool

IsError reports whether value implements the error interface.

func IsFunc

func IsFunc(value any) bool

IsFunc reports whether value is a function.

func IsMap

func IsMap(value any) bool

IsMap reports whether value is a Go map.

func IsNil

func IsNil(value any) bool

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

func IsNumber(value any) bool

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

func IsPlainObject(value any) bool

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 IsPointer

func IsPointer(value any) bool

IsPointer reports whether value is a pointer.

func IsString

func IsString(value any) bool

IsString reports whether value is a string.

func IsZero

func IsZero(value any) bool

IsZero reports whether value is the zero value for its type. An untyped nil is considered zero.

func Lt

func Lt(a, b any) bool

Lt reports whether a is less than b.

func Lte

func Lte(a, b any) bool

Lte reports whether a is less than or equal to b.

func Noop

func Noop()

Noop does nothing and returns nothing.

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

func Range(n int) []int

Range returns a slice of ints from 0 up to, but not including, n. A non-positive n yields an empty slice.

func Times

func Times[T any](n int, fn func(i int) T) []T

Times invokes fn n times, collecting the results into a slice. If n is not positive, an empty slice is returned.

func ToArray

func ToArray(value any) []any

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

func ToFinite(value any) float64

ToFinite converts value to a finite float64. NaN becomes 0 and infinities are clamped to the largest finite float64 magnitude, matching lodash.

func ToInteger

func ToInteger(value any) int

ToInteger converts value to an integer by truncating toward zero. NaN becomes 0 and infinities are clamped to the extremes of int.

func ToNumber

func ToNumber(value any) float64

ToNumber converts value to a float64. Booleans become 0 or 1, numeric strings are parsed and unparseable values yield NaN, mirroring lodash's coercion.

func ToString

func ToString(value any) string

ToString converts value to its string representation. Nil becomes an empty string, floating point numbers are formatted without a trailing exponent when possible and everything else uses fmt's default formatting.

func UniqueId

func UniqueId(prefix string) string

UniqueId returns a unique string identifier, optionally prefixed. Successive calls return monotonically increasing suffixes.

Types

This section is empty.

Jump to

Keyboard shortcuts

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