JavaScript extract
Imports variables into symbol table from an array
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 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 | function extract (arr, type, prefix) { // Imports variables into symbol table from an array // // version: 909.322 // discuss at: http://phpjs.org/functions/extract // + original by: Brett Zamir (http://brett-zamir.me) // % note 1: Only works by extracting into global context (whether called in the global scope or // % note 1: within a function); also, the EXTR_REFS flag I believe can't be made to work // * example 1: size = 'large'; // * example 1: var_array = {'color' : 'blue', 'size' : 'medium', 'shape' : 'sphere'}; // * example 1: extract(var_array, 'EXTR_PREFIX_SAME', 'wddx'); // * example 1: color+'-'+size+'-'+shape+'-'+wddx_size; // * returns 1: 'blue-large-sphere-medium' if (arr instanceof Array && (type !== 'EXTR_PREFIX_ALL' && type !== 'EXTR_PREFIX_INVALID')) { return 0; } var targetObj = this.window; if (this.php_js && this.php_js.ini && this.php_js.ini['phpjs.extractTargetObj'] && this.php_js.ini['phpjs.extractTargetObj'].local_value) { // Allow designated object to be used instead of window targetObj = this.php_js.ini['phpjs.extractTargetObj'].local_value; } var chng = 0; for (var i in arr) { var validIdent = /^[_a-zA-Z$][\w|$]*$/; // TODO: Refine regexp to allow JS 1.5+ Unicode identifiers var prefixed = prefix+'_'+i; try { switch (type) { case 'EXTR_PREFIX_SAME' || 2: if (targetObj[i] !== undefined) { if (prefixed.match(validIdent) !== null) { targetObj[prefixed] = arr[i]; ++chng; } } else { targetObj[i] = arr[i]; ++chng; } break; case 'EXTR_SKIP' || 1: if (targetObj[i] === undefined) { targetObj[i] = arr[i]; ++chng; } break; case 'EXTR_PREFIX_ALL' || 3: if (prefixed.match(validIdent) !== null) { targetObj[prefixed] = arr[i]; ++chng; } break; case 'EXTR_PREFIX_INVALID' || 4: if (i.match(validIdent) !== null) { if (prefixed.match(validIdent) !== null) { targetObj[prefixed] = arr[i]; ++chng; } } else { targetObj[i] = arr[i]; ++chng; } break; case 'EXTR_IF_EXISTS' || 6: if (targetObj[i] !== undefined) { targetObj[i] = arr[i]; ++chng; } break; case 'EXTR_PREFIX_IF_EXISTS' || 5: if (targetObj[i] !== undefined && prefixed.match(validIdent) !== null) { targetObj[prefixed] = arr[i]; ++chng; } break; case 'EXTR_REFS' || 256: throw 'The EXTR_REFS type will not work in JavaScript'; case 'EXTR_OVERWRITE' || 0: // Fall-through default: targetObj[i] = arr[i]; ++chng; break; } } catch (e) { // Just won't increment for problem assignments } } return chng; } |
Examples
Running
1 2 3 4 | size = 'large'; var_array = {'color' : 'blue', 'size' : 'medium', 'shape' : 'sphere'}; extract(var_array, 'EXTR_PREFIX_SAME', 'wddx'); color+'-'+size+'-'+shape+'-'+wddx_size; |
Should return
1 | 'blue-large-sphere-medium' |
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 extract goodness in JavaScript.
Just to keep the relevant items together, I'm resubmitting list() here in case anyone looking at extract() is interested (I fully understand not porting it to the main project given its not following PHP-style (getting the array in by the last argument rather than by assignment)):
1
2
3
4
56
7
8
9
| // Only works in global context and deviates (by necessity) from PHP version list('drink', 'color', 'power', ['coffee', 'brown', 'caffeine']); function list () { var arr = arguments[arguments.length-1] for (var i=0; i < arr.length; i++) { this[arguments[i]] = arr[i]; } } alert(drink +' is '+color+' and '+power +' makes it special.\n'); // Example from PHP manual |


Kevin van Zonneveld
8 Jan '09