Use PHP functions in JavaScript

JavaScript compact

Creates a hash containing variables and their values

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
function compact ( ) {
    // Creates a hash containing variables and their values  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/compact    // +   original by: Waldo Malqui Silva
    // +    tweaked by: Jack
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: var1 = 'Kevin'; var2 = 'van'; var3 = 'Zonneveld';      // *     example 1: compact('var1', 'var2', 'var3');
    // *     returns 1: {'var1': 'Kevin', 'var2': 'van', 'var3': 'Zonneveld'}    
    
    var matrix = {}, that = this;
     var process = function ( value ) {
        var i = 0, l = value.length, key_value = '';
        for (i = 0; i < l; i++) {
            key_value = value [ i ];
            if (key_value instanceof Array) {                process( key_value );
            } else {
                if (typeof that.window[key_value] !== 'undefined') {
                    matrix[key_value] = that.window[key_value];
                }            }
        }
        return true;
    };
        process(arguments);
    return matrix;
}
external links: original PHP docs | raw js source

Examples

Running

1
2
var1 = 'Kevin'; var2 = 'van'; var3 = 'Zonneveld';
compact('var1', 'var2', 'var3');

Should return

1
{'var1': 'Kevin', 'var2': 'van', 'var3': 'Zonneveld'}

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 compact 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

Gravatar
Brett Zamir
3 Dec '08 Permalink

q  Again, adapting the PHP manual's examples, here is an equivalent for extract():

[CODE=&quot;Javascript&quot;]var size = &quot;large&quot;;
var var_array = {&quot;color&quot; : &quot;blue&quot;,
&quot;size&quot; : &quot;medium&quot;,
&quot;shape&quot; : &quot;sphere&quot;};
extract(var_array, 'EXTR_PREFIX_SAME', &quot;wddx&quot;);

alert([color, size, shape, wddx_size].join()); // blue,large,sphere,medium[/CODE]


[CODE=&quot;Javascript&quot;]// Only works on global variables

function extract (arr, type, prefix) {
for (var i in arr) {
switch (type) {
case 'EXTR_PREFIX_SAME' || 2:
if (window[i] !== undefined) {
window[prefix+'_'+i] = arr[i];
}
// Fall-through
case 'EXTR_SKIP' || 1:
if (window[i] === undefined) {
window[i] = arr[i];
}
break;
case 'EXTR_PREFIX_ALL' || 3:
window[prefix+'_'+i] = arr[i];
break;
case 'EXTR_PREFIX_INVALID' || 4:
if(i.match(/^[_a-zA-Z$][\w|$]*$/) != null) { // Refine regexp to allow JS 1.5+ Unicode identifiers
window[i] = arr[i];
}
else {
window[prefix+'_'+i] = arr[i];
}
break;
case 'EXTR_IF_EXISTS' || 6:
if (window[i] !== undefined) {
window[i] = arr[i];
}
break;
case 'EXTR_PREFIX_IF_EXISTS' || 5:
if (window[i] !== undefined) {
window[prefix+'_'+i] = arr[i];
}
break;
case 'EXTR_REFS' || 256:
throw 'The EXTR_REFS type will not work in JavaScript';
break;
case 'EXTR_OVERWRITE' || 0:
// Fall-through
default:
window[i] = arr[i];
break;
}
}
}[/CODE]

Gravatar
Karol Kowalski
6 Feb '08 Permalink

q  This version is not recursive as it should be acc to the php manual. Try run following example (taken from the manual, too):

[CODE=&quot;Javascript&quot;]
var city = &quot;San Francisco&quot;;
var state = &quot;CA&quot;;
var event = &quot;SIGGRAPH&quot;;

var location_vars = new Array(&quot;city&quot;, &quot;state&quot;);

var result = compact(&quot;event&quot;, &quot;nothing_here&quot;, &quot;location_vars&quot;);

console.log(result)
[/CODE]

What you get is an object with event property and location_vars property which is an Array. What should be returned is a 'flat' object with event, city and state properties (because location_vars) should be searched inside.


Contribute a New function