Skip to main content
Home

Built and signed on GitHub Actions

A simple function that asserts that a value is not undefined or null and returns the narrowed value

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
2 months ago (0.1.0)

@quentinadam/ensure

JSR NPM CI

A simple function that asserts that a value is not undefined or null and returns the narrowed value.

Usage

import ensure from '@quentinadam/ensure';

const a = ensure(1); // doesn't throw

const b = ensure(undefined); // throws an AssertionError

ensure(undefined, 'message'); // throws an AssertionError with a custom error message

ensure(undefined, new Error('message')); // throws a custom Error

const value: string | undefined = 'hello';
const narrowedValue = ensure(value); // narrows the type of value to string;
value.toUpperCase(); // works
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@quentinadam/ensure

Import symbol

import * as ensure from "@quentinadam/ensure";
or

Import directly with a jsr specifier

import * as ensure from "jsr:@quentinadam/ensure";

Add Package

pnpm i jsr:@quentinadam/ensure
or (using pnpm 10.8 or older)
pnpm dlx jsr add @quentinadam/ensure

Import symbol

import * as ensure from "@quentinadam/ensure";

Add Package

yarn add jsr:@quentinadam/ensure
or (using Yarn 4.8 or older)
yarn dlx jsr add @quentinadam/ensure

Import symbol

import * as ensure from "@quentinadam/ensure";

Add Package

vlt install jsr:@quentinadam/ensure

Import symbol

import * as ensure from "@quentinadam/ensure";

Add Package

npx jsr add @quentinadam/ensure

Import symbol

import * as ensure from "@quentinadam/ensure";

Add Package

bunx jsr add @quentinadam/ensure

Import symbol

import * as ensure from "@quentinadam/ensure";