Skip to main content

Built and signed on GitHub Actions

Simple helper functions for nullish values.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
11 months ago (0.5.0)

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

Examples

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')
Built and signed on
GitHub Actions
View transparency log

Add Package

deno add jsr:@garretmh/nullish

Import symbol

import * as nullish from "@garretmh/nullish";

---- OR ----

Import directly with a jsr specifier

import * as nullish from "jsr:@garretmh/nullish";

Add Package

npx jsr add @garretmh/nullish

Import symbol

import * as nullish from "@garretmh/nullish";

Add Package

yarn dlx jsr add @garretmh/nullish

Import symbol

import * as nullish from "@garretmh/nullish";

Add Package

pnpm dlx 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";