JavaScript assert
Checks if assertion is false
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 | function assert (assertion) { // Checks if assertion is false // // version: 1008.1718 // discuss at: http://phpjs.org/functions/assert // + original by: Brett Zamir (http://brett-zamir.me) // % note 1: Do not pass untrusted user input to assert() in string form (you can test it beforehand though) // % note 2: Does not provide perfect arguments to the assertion callback, as far as file location or line number // * example 1: assert('false === true'); // * returns 1: false var result = false, callback, retVal, err=undefined; // BEGIN REDUNDANT this.php_js = this.php_js || {}; this.php_js.ini = this.php_js.ini || {}; this.php_js.assert_values = this.php_js.assert_values || {}; // END REDUNDANT var getOption = function (value) { if (this.php_js.assert_values[value]) { return this.php_js.assert_values[value]; } if (this.php_js.ini[value]) { return this.php_js.ini[value].local_value; } switch (value) { case 'assert.active': return 1; case 'assert.warning': // Don't need this now //return 1; throw 'We have not yet implemented warnings in JavaScript (assert())'; case 'assert.bail': return 0; case 'assert.quiet_eval': return 0; case 'assert.callback': return null; default: throw 'There was some problem'; } }; if (!getOption('assert.active')) { return false; // is this correct? should callbacks still execute? Should still bail if on? } try { // Less overhead to use string when assertion checking is off, allows message of exact code to callback result = typeof assertion === 'string' ? eval(assertion) : assertion; } catch (e) { if (!getOption('assert.quiet_eval')) { throw e; } err = e; result = false; } retVal = result !== false; // return false if false, otherwise, return true if (retVal === false) { if (getOption('assert.bail')) { // Todo: Will the function bail before a callback or after? throw 'Assertion bailed'; // No way to bail without throwing an exception (and there are no "warnings" in JavaScript for us to throw) } callback = getOption('assert.callback'); if (typeof callback === 'string') { callback = this.window[callback]; } // Not perfect for file location (might also use __FILE__()) or line number callback(this.window.location.href, err && err.lineNumber, (typeof assertion === 'string' ? assertion : '')); // From the docs, what does this mean?: "the third argument will contain the expression that failed (if any - literal values such as 1 or "two" will not be passed via this argument)" } return retVal;} |
Examples
Running
1 | assert('false === true'); |
Should return
1 | false |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have assert goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>