npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dessert

v0.3.2

Published

Extensible assertions

Downloads

24

Readme

Dessert

Extensible assertion library

Simple assertion

For asserting a boolean expression with an optional message or messages.

dessert.assert(expr, message)

On failure, ie. when expr is falsey, assert throws an exception with the specified message. On success, assert returns the namespace dessert, thus making asserts chainable.

Note that while only the first argument is used for detecting failure, any number of arguments may be passed to .assert() and all of those arguments will end up in the assertion message, in their original order.

Default assertions

Dessert comes with a set of default assertion methods, each testing for a specific type. Most are adequately described by their names. Methods suffixed with "Optional" allow expr to be undefined.

  • .hasValue(expr): tests whether expr is not undefined
  • .isString(expr)
  • .isStringOptional(expr)
  • .isBoolean(expr)
  • .isBooleanOptional(expr)
  • .isNumber(expr)
  • .isNumberOptional(expr)
  • .isFunction(expr)
  • .isFunctionOptional(expr)
  • .isObject(expr)
  • .isObjectOptional(expr)
  • .isPlainObject(expr): tests whether expr is a direct descendant of Object.prototype, eg. when it's declared as an object literal.
  • .isArray(expr)
  • .isArrayOptional(expr)
  • .isRegExp(expr)
  • .isRegExpOptional(expr)
  • .isDate(expr)
  • .isDateOptional(expr)
  • .isJQuery(expr)
  • .isJQueryOptional(expr)

Assertions, default or user-defined (default assertions are added the same way user-defined ones are), may be invoked as follows:

dessert.isString(myString, myMessage);

User-defined assertions

In order to extend Dessert with your own assertions, you'll need to add validators using Dessert's API created for this specific purpose. Validators are functions that take any number of arguments, and return true if the arguments satisfy the condition, and false if they don't.

There are two ways of adding validators. The shorter one takes a method name and a validator function:

dessert.addType(methodName, validator)

However, for code clarity and aiding IDEs in identifying methods, there's another way for validator addition, where multiple functions may be batched together in a single object:

dessert.addTypes({
    methodName: validator,
    methodName2: validator2
});

When a user-defined assertion is invoked as dessert.methodName(), the validator is first evaluated, then its result and all arguments are passed to dessert.assert().

Validator composition

It is often necessary to invoke a validator from another validator, thus composing new validators out of existing ones. Existing validators may be accessed on the context object (this) of every validator added using either .addType() or .addTypes().

Example:

dessert.addTypes({
    isBigArray: function (expr) {
        return this.isArray(expr) && expr.length > 1000;
    }
});

Accessing validators from code

There may be occasions when it would be fit to use an assertion validator without actually asserting the result. To this end, validators may be accessed directly through dessert.validators.

var success = dessert.validators.isObject(1); // false

Global custom handler

For each assertion our application may have to run some custom code dealing with user output or logging. A custom handler may be specified via the method .customHandler(). A custom handler may return false, preventing the assertion mechanism to throw an exception.

Example:

dessert.customHandler(function () {
    alert("this is a very intrusive assertion");

    // don't throw assertion
    return false;
});

dessert.assert(false, "foo"); // will pop up alert window, won't throw exception

The custom handler may be removed as:

dessert.customHandler(undefined);

Although it is assumed that the custom handler is global in the scope of the application.