Use PHP functions in JavaScript

JavaScript assert_options

Set/get the various assert flags

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
function assert_options (what, value) {
    // Set/get the various assert flags  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/assert_options    // +   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;
}
external links: original PHP docs | raw js source

Examples

Running

1
assert_options('ASSERT_CALLBACK');

Should return

1
null

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_options goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

No comments yet. Be the first!


Contribute a New function