arg-assert

A tiny argument assertion library

Usage no npm install needed!

<script type="module">
  import argAssert from 'https://cdn.skypack.dev/arg-assert';
</script>

README

Arg Assert

Simple, tiny assertion library designed for making assertions about function arguments.

NOTE: when I use the term "truthy" in these docs, I do not use it in the sense usually understood in javascript. In these docs, "truthy" means not null, undefined, or false.

Installation

npm install --save-dev arg-assert

API

assert

/**
 * Assert a value is truthy.
 * @param thingToBeTruthy The assertion subject.
 * @param {string} [message] The error message to display if thingToBeTruthy is
 *   falsy.
 * @throws {Error} An error with message equal to the passed in message (if
 *   given) if thingToBeTruthy is falsy.
 */

assertAll

/**
 * Assert a collection of values are truthy.
 * @param {{Object.<string, *>}} thingsToBeTruthy An object whose keys are the
 *  names of the args that should be truthy.
 * @throws {Error} An error with message with the form '"[thingToBeTruthy]" must
 *   be truthy.
 */

Tip: This is especially nice when asserting a number of arguments are non-null, and if you are using es6, you can use deconstruction to keep your code compact.

Example:

function(a, b, c) {
  assertAll({a, b, c});
}