ActionScript 3 版 print_r

Date
2007-07-25 (Wed)
Category
AIR / ActionScript

オールドスクール開発で、print debug はかかせません。そして php で育った僕には、print_r みたいな、データ構造ダンプ関数がかかせません。というわけで作りました。

使い方


import Utils;

trace( Utils.print_r( [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ) );

などとすると、

[trace] ---------------------------------------------------------------------
[trace] :Array =>
[trace]   [0]:int => 0
[trace]   [1]:int => 1
[trace]   [2]:int => 2
[trace]   [3]:int => 3
[trace]   [4]:int => 4
[trace]   [5]:int => 5
[trace]   [6]:int => 6
[trace]   [7]:int => 7
[trace]   [8]:int => 8
[trace]   [9]:int => 9
[trace] ---------------------------------------------------------------------

と、帰ってきます。他に toCamelCase / capitalize ができます。

既存の問題

コード

package
{
  import flash.utils.describeType;
public class Utils {
public static function print_r (o:*, name:String = "", recur:int = 0):String { var result:String = ""; var type:String = typeof(o); var desc:XML = describeType(o);
// meta data of object if (recur == 0) { result += "---------------------------------------------------------------------\n"; } else { for (var i:int = 0; i < recur; ++i) { result += " "; } } if (name) { result += '[' + name + ']'; } result += ':' + desc.@name + ' => ';
// content of object switch (type) { case "boolean": case "number": case "string": result += String(o); break; case "xml": result += o.toXMLString(); break;0 case "object": if (desc.@name == "Object" || desc.@name == "Array") { for (var key:String in o) { result += "\n"; result += print_r(o[key], key, (recur + 1)); } } else { var prop:XML; //trace("UTIL: " + typeof(o)); //trace("UTIL: " + desc.toXMLString()); // -- properties for each (prop in desc.variable) { result += "\n"; result += print_r(o[prop.@name], prop.@name, (recur + 1)); } // -- methods for each (prop in desc.method) { //trace("UTIL: " + prop.toXMLString()); result += "\n"; result += print_r(o[prop.@name], prop.@name, (recur + 1)); } // -- constructor //<constructor> // <parameter index="1" type="flash.net::URLRequest" optional="true"/> //</constructor> // -- inheritance and implements //<implementsInterface type="flash.events::IEventDispatcher"/> //<extendsClass type="flash.events::EventDispatcher"/> //<extendsClass type="Object"/> // -- events and others.. //<metadata name="Event"> // <arg key="name" value="httpStatus"/> // <arg key="type" value="flash.events.HTTPStatusEvent"/> //</metadata> } break; case "function": //trace("UTIL: " + typeof(o)); //trace("UTIL: " + desc.toXMLString()); result += String(o); break; }
if (recur == 0) { result += "\n---------------------------------------------------------------------"; } return result; }
public static function toCamelCase (str:String):String { var r:String = ""; var a:Array = str.split(' '); for (var s:String in a) { r += Utils.capitalize(a[s]); } return r; }
public static function capitalize (str:String):String { var r:String = ""; r += (str.slice(0, 1)).toUpperCase(); r += str.slice(1); return r; }
} }

Comment:0

Comment Form

Remember Me?


Trackback:0

TrackBack URL for this entry
http://blogs.grf-design.com/mt/mt-tb.cgi/227
Listed below are links to weblogs that reference
ActionScript 3 版 print_r from The Croton

Return to Page Top