Skip to content

Commit

Permalink
added assert example
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Nov 30, 2011
1 parent aa98726 commit a83d33e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/assert.js
@@ -0,0 +1,31 @@

require('../');

var fs = require('fs');

function assert(expr) {
if (expr) return;

var call = __stack[1]
, file = call.getFileName()
, lineno = call.getLineNumber()
, src = fs.readFileSync(file, 'utf8')
, line = src.split('\n')[lineno-1]
, src = parse(line);

var fmt = ' \033[91massert: \033[31m%s\033[0m'
+ '\033[90m in %s:%d\033[0m';

console.error(fmt, src, file, lineno);
}

function parse(str) {
return str.match(/assert\((.*)\)/)[1];
}

assert('wahoo');

var user = { authenticated: false };
assert(user.authenticated);


0 comments on commit a83d33e

Please sign in to comment.