UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

159 lines (142 loc) 4.81 kB
<!-- Generated by Rakefile:build --> <strong> ekim </strong> on 2011-04-20 03:09:54 <br /> Can it dump RA or curse defined as : <pre><code> RA=[]; RA[1]=RA; curse={re:RA}; curse.s=curse; </code></pre> ? If so, can the output be posted to see how it's represented? <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-03-10 14:30:54 <br /> Thanks, Zahlii! I've committed some changes to http://github.com/kvz/phpjs/raw/master/functions/var/var_dump.js . I removed a global, added support for RegExp, cleaned up the function appearance a little and added more support for other DOM node types, and cleaned up. If you want to get a string returned, use var_export() (as in PHP) with the second argument set to true. We don't currently support output buffering. <hr /> <strong> Zahlii </strong> on 2010-03-06 20:37:09 <br /> I changed that .js a bit. Now it is able to: * var_dump functions * var_dump Date(s) as String <pre><code> function var_dump () { var output = &quot;&quot;, pad_char = &quot; &quot;, pad_val = 4, lgth = 0, i = 0, d = this.window.document; var getFuncName = function (fn) { var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn); if (!name) { return '(Anonymous)'; } return name[1]; }; var repeat_char = function (len, pad_char) { var str = &quot;&quot;; for (var i=0; i &lt; len; i++) { str += pad_char; } return str; }; var getScalarVal = function (val) { var ret = ''; if (val === null) { ret = 'NULL'; } else if (typeof val === 'boolean') { ret = 'bool('+val+')'; } else if (typeof val === 'string') { ret = 'string('+val.length+') &quot;'+val+'&quot;'; } else if (typeof val === 'number') { if (parseFloat(val) == parseInt(val, 10)) { ret = 'int('+val+')'; } else { ret = 'float('+val+')'; } } else if (val === undefined) { ret = 'UNDEFINED'; // Not PHP behavior, but neither is undefined as value } else if (typeof val === 'function') { ret = 'FUNCTION'; // Not PHP behavior, but neither is function as value ret = val.toString().split(&quot;\n&quot;); txt = &quot;&quot;; for(var j in ret) { txt+= (j !=0 ? thick_pad : '')+ret[j]+&quot;\n&quot;; } ret = txt; } else if(val instanceof Date) { val = val.toString(); ret = 'string('+val.length+') &quot;'+val+'&quot;' } else if(val.nodeName) { ret = 'HTMLElement(&quot;'+val.nodeName.toLowerCase()+'&quot;)'; } return ret; }; var formatArray = function (obj, cur_depth, pad_val, pad_char) { var someProp = ''; if (cur_depth &gt; 0) { cur_depth++; } base_pad = repeat_char(pad_val*(cur_depth-1), pad_char); thick_pad = repeat_char(pad_val*(cur_depth+1), pad_char); var str = &quot;&quot;; var val=''; if (typeof obj === 'object' &amp;&amp; obj !== null) { if (obj.constructor &amp;&amp; getFuncName(obj.constructor) === 'PHPJS_Resource') { return obj.var_dump(); } lgth = 0; for (someProp in obj) { lgth++; } str += &quot;array(&quot;+lgth+&quot;) {\n&quot;; for (var key in obj) { if (typeof obj[key] === 'object' &amp;&amp; obj[key] !== null &amp;&amp; !(obj[key] instanceof Date) &amp;&amp; !obj[key].nodeName) { str += thick_pad + &quot;[&quot;+key+&quot;] =&gt;\n&quot;+thick_pad+formatArray(obj[key], cur_depth+1, pad_val, pad_char); } else { val = getScalarVal(obj[key]); str += thick_pad + &quot;[&quot;+key+&quot;] =&gt;\n&quot;+ thick_pad +val + &quot;\n&quot;; } } str += base_pad + &quot;}\n&quot;; } else { str = getScalarVal(obj); } return str; }; output = formatArray(arguments[0], 0, pad_val, pad_char); for (i=1; i &lt; arguments.length; i++) { output += '\n'+formatArray(arguments[i], 0, pad_val, pad_char); } return output; } </code></pre> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-02-24 00:25:46 <br /> @Ben: Can you give the example code you used to test? <hr /> <strong> Ben </strong> on 2010-02-23 22:38:46 <br /> Doesn't seem to work in Chrome, only worked in Firefox :( <hr /> <strong> <a href="joelwallis.net" rel="nofollow">joelwallis</a> </strong> on 2009-08-03 20:33:16 <br /> Very good idea! <hr />