Created
March 8, 2012 01:58
-
-
Save danilowm/1998003 to your computer and use it in GitHub Desktop.
print_r() do PHP no Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JavaScript print_r | |
* | |
* PHP print JavaScript version | |
* | |
* @package print_r | |
* @author Bruno Augusto | |
* @version 0.1 | |
* | |
* @params string Input | |
* @return string Output | |
*/ | |
function print_r( input, _indent ) { | |
// Recuo | |
var indent = ( typeof( _indent ) == 'string' ) ? _indent + ' ' : ' ' | |
var parent_indent = ( typeof( _indent ) == 'string' ) ? _indent : ''; | |
var output = ''; | |
// Tipo de Elemento do Array | |
switch( typeof( input ) ) { | |
case 'string': | |
output = "'" + input + "'n"; | |
break; | |
case 'number': | |
output = input + "n"; | |
break; | |
case 'boolean': | |
output = ( input ? 'true' : 'false' ) + "n"; | |
break; | |
case 'object': | |
output = ( ( input.reverse ) ? 'Array' : 'Object' ) + "n"; | |
output += parent_indent + "(n"; | |
for( var i in input ) { | |
output += indent + '[' + i + '] => ' + print_r( input[ i ], indent ); | |
} | |
output += parent_indent + ")n" | |
break; | |
} | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment