about
Produces exceptions for failing assertions. Includes ready-made checks for truthiness and equality. Combines with tapeling in tapeless for TAP formatted output.
setup
Download from the npm registry:
# Add to package.json
npm install likewise --save-dev
Source from an import map to use with Deno or in-browser directly:
{
"imports": {
"likewise": "https://cdn.jsdelivr.net/npm/likewise@latest/main.js"
}
}
usage
The named export reassert()
is available for creating own assertions. For example,
import { reassert } from "likewise"
// Basic truthiness checking
const truthy = (v) => !!v
const falsy = (v) => !v
// Wrap with `reassert()` to throw an `Error` with
// the 'expected' value and an 'operator' key on failure,
// the 2nd and 3rd arguments respectively
const ok = reassert(truthy, true, "!!")
const notOk = reassert(falsy, false, "!")
// Clean run
console.assert(ok(true), "ok")
console.assert(notOk(0), "not ok")
Additionally, ok()
and equal()
plus counterparts are attached to assert()
already wrapped. For example,
import { assert } from "likewise"
const { ok, notOk, equal, notEqual } = assert
// Clean run
console.assert(ok(true), "ok")
console.assert(notOk(0), "not ok")
// Based on `Object.is()`
console.assert(equal(null, null), "equal")
console.assert(notEqual(null, 0), "not equal")