@algosail/nil
v0.1.0
Published
Small collection of FP utilities for working with nullable values (null | undefined).
Readme
@algosail/nil
Utilities for working with nullable values (null | undefined). A thin alternative to Maybe for code that deals directly with nullable primitives.
Contents
nil
nil :: a -> Nil aReturns the value if it is non-nil, otherwise returns null.
nil(42) // => 42
nil('hello') // => 'hello'
nil(0) // => 0 — 0 is not nil
nil(null) // => null
nil(undefined) // => nullinit
init :: () -> Nil neverReturns the empty nil value (null). Useful as a typed zero/mempty.
init() // => nullisNil / isNotNil
isNil :: Nil a -> Boolean
isNotNil :: Nil a -> BooleanCheck whether a value is nil (null or undefined).
isNil(null) // => true
isNil(undefined) // => true
isNil(0) // => false
isNil('') // => false
isNil(false) // => false
isNotNil(42) // => true
isNotNil(null) // => falsefromPredicate
fromPredicate :: (a -> Boolean) -> Nil a -> Nil aReturns the value if it is non-nil and satisfies the predicate; otherwise null.
fromPredicate((x) => x > 0)(5) // => 5
fromPredicate((x) => x > 0)(-1) // => null
fromPredicate((x) => x > 0)(null) // => nullfromMaybe
fromMaybe :: Maybe a -> Nil aConverts a Maybe to a nullable value — Nothing becomes null, Just(a) becomes a.
fromMaybe(just(42)) // => 42
fromMaybe(nothing()) // => null
fromMaybe(just(null)) // => null — the wrapped value