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

lazy-ass-helpful

v0.6.1

Published

lazy-ass plugin to automatically show helpful info for failed assertions

Downloads

102

Readme

lazy-ass-helpful

lazy-ass plugin to automatically show helpful info for failed assertions

Test page, test page BDD (mocha)

NPM

Build status dependencies devdependencies

Stop writing assertion messages, let the automatic on the fly code rewriting put the entire expression into the text message.

This package provides single function lazyAssHelpful that transforms any given function with lazy-ass assertions into another function. If any lazy assertion fails, it will help its expression in the error message.

Example:

Typical code with assertions requires lots of extra stuff to be helpful, if the condition is false.

function doSomething(a) {
  lazyAss(typeof a === 'number', 'a should be a number', a);
  ...
}
doSomething('foo'); // Error: a shoud be a number 'foo'

lazy-ass-helpful allows you to skip writing text explanations in assertions

function doSomething(a) {
  lazyAss(typeof a === 'number', a);
  ...
}
lazyAssHelpful(doSomething)('foo'); // Error: condition [typeof a === "number"] 'foo'

install and use

node:

npm install lazy-ass-helpful --save
require('lazy-ass-helpful');

browser:

bower install lazy-ass-helpful --save
<script src="bower_components/lazy-ass-helpful-browser.js"></script>

use:

function foo() {
  lazyAss(2 + 2 === 5);
}
var helpfulFoo = lazyAssHelpful(foo);
foo(); // Error: failed
helpfulFoo(); // Error: condition [2 + 2 === 5]

Auto variables

lazyAssHelpful not only places the expression into the message, it also parses the expression and puts all variables into the message (if they are defined and not functions)

function bar() {
  var foo = 'something';
  lazyAss(foo === 'nothing');
}
var barHelped = lazyAssHelpful(bar);
barHelped();
// throws
// Error: condition [foo === "nothing"] foo: something

Limitation

Because lazyAssHelpful rewrites and evals the given function, it no longer can access the closure variables directly. Only global variables or local variables are allowed

// local variables are ok
function foo() {
  var bar = 'bar';
  lazyAss(bar === 'something');
}
lazyAssHelpful(foo)();
// global variables are ok
window.bar = 'bar';
function foo() {
  lazyAss(window.bar === 'something');
}
lazyAssHelpful(foo)();
// closure variables are NOT ok
bar bar = 'bar';
function foo() {
  lazyAss(bar === 'something');
}
lazyAssHelpful(foo)(); // ReferenceError: bar is undefined

lazy-ass-helpful-bdd

I include lazy-ass-helpful-bdd.js that wraps the common BDD functions describe and it into helpDescribe and helpIt. These functions transform the unit test code using lazyAssHelpful method.

Typical setup:

<script src="node_modules/mocha/mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script src="node_modules/lazy-ass/index.js"></script>
<script src="lazy-ass-helpful-browser.js"></script>
<script src="lazy-ass-helpful-bdd.js"></script>
<script src="test/test-bdd.js"></script>
<script>
  mocha.run();
</script>

The write unit test wrapped in helpDescribe function using lazyAss assertions. If an assertion fails, there will be helpful context.

lazy-ass-helpful-bdd

I also have same project optimized for QUnit assertions rewriting, see qunit-helpful

How?

lazyAssHelpful rewrites the given function using falafel, and then returns a new function with every lazyAss(condition, ...) replaced with lazyAss(condition, 'condition source', ...).

Small print

Author: Gleb Bahmutov © 2014 @bahmutov

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github