1.2.1 • Published 8 years ago

underscore.assert v1.2.1

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

underscore.assert (npm)

npm version npm.io Travis Coverage Status

:hammer: Plugin for Underscore.js: add method "_.assert", which simplify checking states

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

Warning!

Probably, better option foy you is use assert package from npm. But if you insist I will be happy to hear that you use my package

Please give me any feedback @piecioshka.

Install

$ npm install underscore.assert

Usage

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'); // thows 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
}

There is a shorter, nicer and better experience for developer :smile:

Proposals

  • This is not for tests 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 @ 2013

1.2.1

8 years ago

1.2.0

8 years ago

1.1.1

8 years ago

1.1.0

9 years ago