JSFiddle - React, Tailwind, and code Playground
by Andrew Gerst
HTML
<b>JavaScript Assertions</b><br/><br/>
http://blog.kowalczyk.info/articles/jsref.html
JavaScript
// Number
// 2 1.5 2.5e3 0xFF 010
assert("2+2 == 4"); // numbers are 64-bit floating point
assert("1.5 == 3/2"); // (no separate integer type)
assert("2.5e3 == 2500"); // 2.5 x 10^3 exponential notation
assert("0xFF == 255"); // hexadecimal
assert("010 == 8"); // octal
// Simple Math
assert("2 + 2 == 4"); // addition
assert("");
function createDiv(text) {
var div = document.createElement("div")
div.innerHTML = text;
document.body.appendChild(div);
}
function assert(assertion) {
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 =...