Comparing version 0.1.2 to 0.2.0
{ | ||
"name": "lazy-ass", | ||
"main": "index.js", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/bahmutov/lazy-ass", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
54
index.js
(function initLazyAss() { | ||
var lazyAss = function (condition) { | ||
var args = [].slice.call(arguments, 1); | ||
function formMessage(args) { | ||
var msg = args.reduce(function (total, arg, k) { | ||
if (k) { | ||
total += ' '; | ||
} | ||
if (typeof arg === 'string') { | ||
return total + arg; | ||
} | ||
if (typeof arg === 'function') { | ||
return total + arg(); | ||
} | ||
if (Array.isArray(arg)) { | ||
return total + JSON.stringify(arg); | ||
} | ||
return total + JSON.stringify(arg, null, 2); | ||
}, ''); | ||
return msg; | ||
} | ||
var lazyAss = function lazyAss(condition) { | ||
if (!condition) { | ||
var msg = args.reduce(function (total, arg, k) { | ||
if (k) { | ||
total += ' '; | ||
} | ||
if (typeof arg === 'string') { | ||
return total + arg; | ||
} | ||
if (typeof arg === 'function') { | ||
return total + arg(); | ||
} | ||
if (Array.isArray(arg)) { | ||
return total + JSON.stringify(arg); | ||
} | ||
return total + JSON.stringify(arg, null, 2); | ||
}, ''); | ||
throw new Error(msg); | ||
var args = [].slice.call(arguments, 1); | ||
throw new Error(formMessage(args)); | ||
} | ||
}; | ||
var lazyAssync = function lazyAssync(condition) { | ||
if (!condition) { | ||
var args = [].slice.call(arguments, 1); | ||
setTimeout(function () { | ||
throw new Error(formMessage(args)); | ||
}, 0); | ||
} | ||
}; | ||
function register(value, name) { | ||
@@ -29,4 +42,4 @@ if (typeof window === 'object') { | ||
window[name] = value; | ||
} else if (typeof module === 'object') { | ||
module.exports = value; | ||
} else if (typeof global === 'object') { | ||
global[name] = value; | ||
} else { | ||
@@ -38,3 +51,4 @@ throw new Error('Do not know how to register ' + name); | ||
register(lazyAss, 'lazyAss'); | ||
register(lazyAssync, 'lazyAssync'); | ||
}()); |
{ | ||
"name": "lazy-ass", | ||
"description": "Lazy assertions without performance penalty", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -22,4 +22,4 @@ # lazy-ass | ||
Lazy assertion function only evaluates its arguments and forms | ||
a message if condition is false | ||
Lazy assertion function evaluates its arguments and forms | ||
a message ONLY IF the condition is false | ||
@@ -30,2 +30,11 @@ ```js | ||
## Why? | ||
* Passing an object reference to a function is about | ||
[2000-3000 times faster](http://jsperf.com/object-json-stringify) | ||
than serializing an object and passing it as a string. | ||
* Concatenating 2 strings before passing to a function is about | ||
[30% slower](http://jsperf.com/string-concat-vs-pass-string-reference) | ||
than passing 2 separate strings. | ||
## Install | ||
@@ -49,2 +58,31 @@ | ||
## Lazy async assertions | ||
Sometimes you do not want to throw an error synchronously, breaking the entire | ||
execution stack. Instead you can throw an error asynchronously using `lazyAssync`, | ||
which internally implements logic like this: | ||
```js | ||
if (!condition) { | ||
setTimeout(function () { | ||
throw new Error('Conditions is false!'); | ||
}, 0); | ||
} | ||
``` | ||
This allows the execution to continue, while your global error handler (like | ||
my favorite [Sentry](http://bahmutov.calepin.co/know-unknown-unknowns-with-sentry.html)) | ||
can still forward the error with all specified information to your server. | ||
```js | ||
lazyAssync(false, 'foo'); | ||
console.log('after assync'); | ||
// output | ||
after assync | ||
Uncaught Error: foo | ||
``` | ||
In this case, there is no meaningful error stack, so use good message | ||
arguments - there is no performance penalty! | ||
### Small print | ||
@@ -51,0 +89,0 @@ |
@@ -0,3 +1,7 @@ | ||
/* global lazyAss */ | ||
if (typeof window === 'undefined') { | ||
require('..'); // Node | ||
} | ||
if (typeof lazyAss === 'undefined') { | ||
var lazyAss = require('..'); | ||
throw new Error('Cannot find lazyAss global varible'); | ||
} | ||
@@ -4,0 +8,0 @@ if (typeof expect === 'undefined') { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17875
13
297
135