Value
General value utilities for common JavaScript values and patterns.
Provides utilities for lazy values, type guards for symbols and dates, identity proxies, and lazy value resolution. Includes helpers for working with deferred computations and value type checking.
Import
import { Value } from '@wollybeard/kit'
import * as Value from '@wollybeard/kit/value'
Lazy Values
[T]
Lazy
type Lazy<$Value> = () => $Value
A lazy value that is computed when called.
$Value
- The type of value that will be returned when the lazy function is invoked
[F]
lazy
<value>(value: value): Lazy<value>
Parameters:
value
- The value to wrap in a lazy computation
Returns: A function that returns the wrapped value when called
Creates a lazy value that returns the given value when invoked.
value
- The type of the value to be lazily returned
Examples:
const lazyNumber = Value.lazy(42)
console.log(lazyNumber()) // 42
const lazyObject = Value.lazy({ foo: 'bar' })
console.log(lazyObject()) // { foo: 'bar' }
[U]
LazyMaybe
type LazyMaybe<$Value = unknown> = $Value | Lazy<$Value>
A value that may be either immediate or lazy.
$Value
- The type of the value, whether immediate or lazy
[T]
resolveLazy
type resolveLazy<$LazyMaybeValue extends LazyMaybe<any>> =
$LazyMaybeValue extends Lazy<infer __value__> ? __value__ : $LazyMaybeValue
Type-level resolution of a LazyMaybe value. Extracts the underlying value type whether it's lazy or immediate.
$LazyMaybeValue
- A value that may be lazy or immediate
[F]
resolveLazy
<lazyMaybeValue extends LazyMaybe>(lazyMaybeValue: lazyMaybeValue): resolveLazy<lazyMaybeValue>
Parameters:
lazyMaybeValue
- A value that may be lazy (function) or immediate
Returns: The resolved value
Resolves a value that may be lazy or immediate. If the value is a function (lazy), it calls it to get the result. If the value is immediate, it returns it as-is.
lazyMaybeValue
- The type of the potentially lazy value
Examples:
console.log(Value.resolveLazy(42)) // 42
console.log(Value.resolveLazy(() => 42)) // 42
const lazyConfig = () => ({ port: 3000 })
console.log(Value.resolveLazy(lazyConfig)) // { port: 3000 }
[F]
resolveLazyFactory
<value>(lazyMaybeValue: LazyMaybe<value>): () => value
Parameters:
lazyMaybeValue
- A value that may be lazy (function) or immediate
Returns: A function that when called, resolves and returns the value
Creates a factory function that resolves a lazy or immediate value when called. This is useful when you want to defer the resolution of a LazyMaybe value.
value
- The type of the value to be resolved
Examples:
const getValue = Value.resolveLazyFactory(42)
console.log(getValue()) // 42
const getLazyValue = Value.resolveLazyFactory(() => 42)
console.log(getLazyValue()) // 42
// Useful for configuration that may be lazy
const getConfig = Value.resolveLazyFactory(() => ({
apiUrl: 'https://api.example.com',
}))
console.log(getConfig()) // { apiUrl: 'https://api.example.com' }
Type Guards
[F]
isSymbol
(value: unknown): boolean
Parameters:
value
- The value to check
Returns: True if the value is a symbol
Type guard to check if a value is a symbol.
Examples:
Value.isSymbol(Symbol('test')) // true
Value.isSymbol('test') // false
[F]
isDate
(value: unknown): boolean
Parameters:
value
- The value to check
Returns: True if the value is a Date
Type guard to check if a value is a Date instance.
Examples:
Value.isDate(new Date()) // true
Value.isDate('2024-01-01') // false
Value.isDate(Date.now()) // false
Utilities
[C]
identityProxy
{}
A proxy that returns itself for any property access. Useful for default values or chaining patterns.
Examples:
Value.identityProxy.foo.bar.baz // Returns identityProxy
Value.identityProxy.anything() // Returns identityProxy