Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to enable/disable assert statements in node.js

I am using the assert module in node.js https://www.npmjs.com/package/assert

In C/C++, one can easily enable/disable assert statements with macros. Can the same thing be done for node.js and javascript in general?

like image 604
guagay_wk Avatar asked Sep 27 '22 02:09

guagay_wk


1 Answers

Note that the package you are using is adding asserts as a module and are therefor polyfills.

You can simply overwrite -part- of the Object to disable the polyfill.

Example:
Let's say that you want to disable the deepEqual assert that currently looks like this:

assert.deepEqual = function deepEqual(actual, expected, message) {
  if (!_deepEqual(actual, expected)) {
    fail(actual, expected, message, 'deepEqual', assert.deepEqual);
  }
};

You can simply overwrite it by doing something like:

assert.deepEqual = function deepEqual(actual, expected, message) {
  // disable
};

FYI:
Assert statements are in progress: http://wiki.ecmascript.org/doku.php?id=strawman:assert

like image 169
Bob van Luijt Avatar answered Oct 05 '22 23:10

Bob van Luijt