Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Simple helper functions for nullish values.
Nullish helpers
Simple helper functions for nullish values.
Explanation
I created this in response to the many packages I've seen that attempt to port
Rust Option to JavaScript. I like the
Option type but I don't think it makes much sense in JavaScript. Instead, I
wrote some helpers to perform many of the same actions for "nullable" values.
This largely avoids wrapping values in objects, except when using the chain
helper to chain multiple operations together. It entirely avoids using immutable
wrapper objects as almost all Option implementations do, which avoids creating
and disposing of new objects on every operation.
Note that some of these helpers are fairly pointless. For example, or is
essentially a function that calls ??. But I decided I wanted a certain level
of completeness so here they are.
Usage
Import a single helper
import { andThen } from "@garretmh/nullish/andThen.js";
Import all helpers when using multiple
import * as nullish from "@garretmh/nullish";
Functional programming
Import a functional version of a helper (not all helpers have a functional variant)
import { andThen } from "@garretmh/nullish/fp/andThen.js";
Import all helpers using functional versions of those that have them
import * as nullish from "@garretmh/nullish/fp";
License
MIT
Example 1
Example 1
import * as nullish from "@garretmh/nullish"; nullish.all([]) nullish.allValues({}) nullish.and('foo', 'bar') nullish.andThen('foo', (x) => `${x} bar`) nullish.attempt(() => 'foo') nullish.chain('foo').andThen((x) => `${x} bar`).or('baz').value nullish.filter('foo', (x) => x === 'bar') nullish.isNullish('foo') for (const x of nullish.iter('foo')) {} nullish.notNullish('foo') nullish.or('foo', 'bar') nullish.orElse('foo', () => 'bar') nullish.xor('foo', 'bar')
Add Package
deno add jsr:@garretmh/nullish
Import symbol
import * as nullish from "@garretmh/nullish";
Import directly with a jsr specifier
import * as nullish from "jsr:@garretmh/nullish";
Add Package
pnpm i jsr:@garretmh/nullish
pnpm dlx jsr add @garretmh/nullish
Import symbol
import * as nullish from "@garretmh/nullish";
Add Package
yarn add jsr:@garretmh/nullish
yarn dlx jsr add @garretmh/nullish
Import symbol
import * as nullish from "@garretmh/nullish";
Add Package
vlt install jsr:@garretmh/nullish
Import symbol
import * as nullish from "@garretmh/nullish";
Add Package
npx jsr add @garretmh/nullish
Import symbol
import * as nullish from "@garretmh/nullish";
Add Package
bunx jsr add @garretmh/nullish
Import symbol
import * as nullish from "@garretmh/nullish";