index.js 694 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * Module dependencies.
  3. */
  4. var AssertionError = require('assert').AssertionError
  5. , callsite = require('callsite')
  6. , fs = require('fs')
  7. /**
  8. * Expose `assert`.
  9. */
  10. module.exports = process.env.NO_ASSERT
  11. ? function(){}
  12. : assert;
  13. /**
  14. * Assert the given `expr`.
  15. */
  16. function assert(expr) {
  17. if (expr) return;
  18. var stack = callsite();
  19. var call = stack[1];
  20. var file = call.getFileName();
  21. var lineno = call.getLineNumber();
  22. var src = fs.readFileSync(file, 'utf8');
  23. var line = src.split('\n')[lineno-1];
  24. var src = line.match(/assert\((.*)\)/)[1];
  25. var err = new AssertionError({
  26. message: src,
  27. stackStartFunction: stack[0].getFunction()
  28. });
  29. throw err;
  30. }