@kingjs/assert

Throw an exception if a condition fails.

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
04Jun 21, 2021Jul 20, 2018Minified + gzip package size for @kingjs/assert in KB

Readme

@kingjs/assert

Throw an exception if a condition fails.

Usage

Assert a condition in your code or test like this:

var assert = require('@kingjs/assert');

assert(0 == 1);

throws:

"An assertion failed."

Simply fail all the time like this:

var assert = require('@kingjs/assert');

assert();

throws:

"An assertion failed."

Provide a more descriptive error message like this:

var assert = require('@kingjs/assert');

var expected = 0;
var actual = 1;

assert(
  actual == expected,
  "Expected '" + expected + "' but actually got '" + actual + "'."
);

throws:

"Expected '0' but actually got '1'."

API

declare function assert(condition?: boolean, message?: string);

Parameters

condition: If false (or omitted), throws an exception.

message: Message to throw if condition fails, else will throw "An assertion failed.".

Install

With npm installed, run

$ npm install @kingjs/assert

Acknowledgments

assert is old as dirt and just as underappreciated.

License

MIT

Analytics

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.