What is lazy-ass?
The lazy-ass npm package is a utility for making assertions with lazy evaluation. It allows developers to write assertions that are only evaluated if the assertion condition is false. This can be useful for performance reasons, as the assertion message, which might include string concatenation or other computations, is only calculated if the assertion fails.
What are lazy-ass's main functionalities?
Lazy assertions
This feature allows you to make assertions that are only evaluated if the condition is false. In the code sample, `lazyAss` checks if 'foo' is a string, and if it's not, it will throw an error with the message 'expected a string'.
var lazyAss = require('lazy-ass');
var is = require('check-more-types');
lazyAss(is.string('foo'), 'expected a string');
Lazy assertions with callback
This feature allows you to provide a callback function for the assertion message. The callback is only called if the assertion fails, which can save performance for expensive computations that are only needed for the error message.
var lazyAss = require('lazy-ass');
var is = require('check-more-types');
function expensiveComputation() {
// some expensive computation here
return 'computed value';
}
lazyAss(is.number(expensiveComputation), function () {
return 'expected a number, got ' + expensiveComputation();
});
Other packages similar to lazy-ass
chai
Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. It offers a richer and more comprehensive set of assertions compared to lazy-ass, including property assertions, deep equality, and chainable language constructs.
assert
Assert is a simple assertion library that comes built-in with Node.js. It provides a simple set of assertion tests and is less feature-rich compared to lazy-ass. It does not support lazy evaluation of error messages.
expect.js
Expect.js is a minimalistic BDD assertion toolkit for Node.js and the browser. It provides a similar set of assertions to lazy-ass but does not support lazy evaluation. It has a fluent API and supports assertions like 'to.be', 'to.have', and 'to.contain'.
lazy-ass
Lazy assertions without performance penalty
Example
Regular assertions evaluate all arguments and concatenate message
EVERY time, even if the condition is true.
console.assert(typeof foo === 'object',
'expected ' + JSON.stringify(foo, null, 2) + ' to be an object');
Lazy assertion function only evaluates its arguments and forms
a message if condition is false
lazyAss(typeof foo === 'object', 'expected', foo, 'to be an object');
Install
Node: npm install lazy-ass --save
then var lazyAss = require('lazy-ass');
Browser: bower install lazy-ass --save
, makes function available as window.lazyAss
.
Notes
You can pass as many arguments to lazyAss after the condition. The condition
will be evaluated every time (this is required for any assertion). The rest of arguments
will be concatenated according to rules
- string will be left unchanged.
- function will be called and its output will be concatenated.
- any array or object will be JSON stringified.
There will be single space between the individual parts.
Small print
Author: Gleb Bahmutov © 2014
License: MIT - do anything with the code, but don't blame me if it does not work.
Spread the word: tweet, star on github, etc.
Support: if you find any problems with this module, email / tweet /
open issue on Github
MIT License
Copyright (c) 2014 Gleb Bahmutov
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.