UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

91 lines (78 loc) 2.79 kB
--- layout: page title: "JavaScript assert_options function" comments: true sharing: true footer: true alias: - /functions/view/assert_options:832 - /functions/view/assert_options - /functions/view/832 - /functions/assert_options:832 - /functions/832 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's assert_options {% codeblock info/assert_options.js lang:js https://raw.github.com/kvz/phpjs/master/functions/info/assert_options.js raw on github %} function assert_options (what, value) { // From: http://phpjs.org/functions // + original by: Brett Zamir (http://brett-zamir.me) // * example 1: assert_options('ASSERT_CALLBACK'); // * returns 1: null // 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 ini, dflt; switch (what) { case 'ASSERT_ACTIVE': ini = 'assert.active'; dflt = 1; break; case 'ASSERT_WARNING': ini = 'assert.warning'; dflt = 1; throw 'We have not yet implemented warnings for us to throw in JavaScript (assert_options())'; case 'ASSERT_BAIL': ini = 'assert.bail'; dflt = 0; break; case 'ASSERT_QUIET_EVAL': ini = 'assert.quiet_eval'; dflt = 0; break; case 'ASSERT_CALLBACK': ini = 'assert.callback'; dflt = null; break; default: throw 'Improper type for assert_options()'; } // I presume this is to be the most recent value, instead of the default value var originalValue = this.php_js.assert_values[ini] || (this.php_js.ini[ini] && this.php_js.ini[ini].local_value) || dflt; if (value) { this.php_js.assert_values[ini] = value; // We use 'ini' instead of 'what' as key to be more convenient for assert() to test for current value } return originalValue; } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/info/assert_options.js) Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are the closest match to this hashtable-like data structure. Please also note that php.js offers community built functions and goes by the [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just: - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/info/assert_options.js) ### Example 1 This code {% codeblock lang:js example %} assert_options('ASSERT_CALLBACK'); {% endcodeblock %} Should return {% codeblock lang:js returns %} null {% endcodeblock %} ### Other PHP functions in the info extension {% render_partial _includes/custom/info.html %}