Skip to content

About

🔨 Plugin for Underscore.js: add "_.assert()", which simplify checking states

Topics

Resources

Stars

5 stars

Watchers

3 watching

Forks

Repository files navigation

underscore.assert

node version npm version downloads count size license github-ci

🔨 Plugin for Underscore.js: add _.assert(), which simplify checking states

                         _
                        | |
  __ _ ___ ___  ___ _ __| |_
 / _` / __/ __|/ _ \ '__| __|
| (_| \__ \__ \  __/ |  | |_
 \__,_|___/___/\___|_|   \__|

Usage

Installation:

npm install underscore.assert
var _ = require("underscore");
var assert = require("underscore.assert");
_.mixin({ assert: assert });

_.assert(typeof fn === "function", "*fn* should be function"); // throws AssertionError
_.assert(list.length > 0, "*list* should contains any value"); // throws ONLY when list is empty
_.assert(false); // => throw `AssertionError`
_.assert.ErrorConstructor = MyCustomError;
_.assert(false); // => throw `MyCustomError`

Example

Instead of this:

function isUrl(url) {
    if (typeof url === "string") {
        throw new Error("URL should be a *string* value");
    }

    // ... URL validation
}

... you can that:

function isUrl(url) {
    _.assert(typeof url === "string", "URL should be a *string* value");
    // ... URL validation
}

Proposals

  • This is not for specs files in project...
  • This is not instead of Jasmine or any test framework...
  • This is inline code for testing production code!

License

The MIT License @ 2026

About

🔨 Plugin for Underscore.js: add "_.assert()", which simplify checking states

Topics

Resources

Stars

5 stars

Watchers

3 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages