Created
September 24, 2011 14:56
-
-
Save jacksonkr/1239417 to your computer and use it in GitHub Desktop.
print_r For Actionscript3
This file contains 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
package com.jacksonkr.utils { | |
/** | |
* mimics the php print_r function | |
* jacksonkr.com <[email protected]> | |
*/ | |
public | |
function print_r(obj: * , | |
lvl: int = 0): String { | |
var str: String = ''; | |
var sp = space(lvl); | |
var i: * ; | |
if (obj is String) return obj.toString(); | |
if (obj is XML) return obj.toString(); | |
var op: String = '{}'; | |
// mark the type of object | |
if ((obj is Array) || obj.length) { | |
str = 'Array'; | |
op = '[]'; | |
} else if (obj is Object) { | |
str = 'Object'; | |
} | |
str += '\n' + sp + op.charAt(0); | |
for (i in obj) { | |
str += '\n' + space(lvl + 1) + '[' + i + '] => '; | |
try { | |
str += print_r(obj[i], lvl + 1); | |
} catch (e: Error) { | |
str += obj[i].toString(); | |
} | |
} | |
str += '\n' + sp + op.charAt(1); | |
if (lvl == 0) trace(str); | |
return str; | |
} | |
} | |
function space(lvl: int): String { | |
if (lvl < 0) lvl = 0; | |
var ret: String = ''; | |
for (var i: int = 0;i < lvl;++i) { | |
ret += ' '; | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment