読者です 読者をやめる 読者になる 読者になる

Shut the fuck up and write some code

グダグダ言わずにコードを書きたいブログ

AS3版ObjectDumper

年末より超久々にAS3を書いていて詰まりまくりで死にそうです。そもそもFlashLiteばかりやっていたので、ツール作ったり遊びで書いたりしていたけれど、業務でまともに書いたことが無い。

デバッグ用にObjectDumper使いたいな、と思ったらAS3版は無いんですな。AS2版を適当に書き換えたので貼っておきます。誤動作の責任は一切とりません。

package {
	import flash.xml.XMLNode;
	/**
	 * ObjectDumperクラス
	 */
	public class ObjectDumper{
		
		public static function toString(obj:Object, showFunctions:Boolean=false, showUndefined:Boolean=false, showXMLstructures:Boolean=false, maxLineLength:Number=100, indent:Number=0):String{
			var od = new util.ObjectDumper();
			return od.realToString(obj, showFunctions, showUndefined, showXMLstructures, maxLineLength, indent);
		}
		
		public function ObjectDumper(){
			this.inProgress = new Array();
		}
		
		private var inProgress:Array;
		
		private function realToString(obj:Object, showFunctions:Boolean = false, showUndefined:Boolean = false, showXMLstructures:Boolean = false, maxLineLength:Number= 0, indent:Number= 0):String {
			for (var x = 0; x < this.inProgress.length; x++){
				if (this.inProgress[x] == obj) return "***";
			}
			this.inProgress.push(obj);
			
			indent ++;
			var t = typeof obj;
			var result;
		
			if ((obj is XMLNode) && (showXMLstructures != true)){
				result = obj.toString();
			}else if (obj is Date){
				result = obj.toString();
			}else if (t == "object"){
				var nameList = new Array();
				var i ;
				if (obj is Array){
					result = "["; // "Array" + ":";
					for (i = 0; i < obj.length; i++){
						nameList.push(i);
					}
				}else{
					result = "{"; // "Object" + ":";
					for (i in obj){
						nameList.push(i);
					}
					nameList.sort();
				}
				
				var sep = "";
				for (var j = 0; j < nameList.length; j++){
					var val = obj[nameList[j]];
				
					var show = true;
					if (typeof val == "function") show = (showFunctions == true);
					if (typeof val == "undefined") show = (showUndefined == true);
				
					
					if (show){
						result += sep;
						if (!(obj is Array))
						result += nameList[j] + ": ";
						result +=
						realToString(val, showFunctions, showUndefined, showXMLstructures, maxLineLength, indent);
						sep = ", `";
					}
				}
				if (obj is Array)
					result += "]";
				else
					result += "}";
			}else if (t == "function"){
				result = "function";
			}else if (t == "string"){
				result = "\"" + obj + "\"";
			}else{
				result = String(obj);
			}
		
			if (result == "undefined") result = "-";
				this.inProgress.pop();
			return util.ObjectDumper.replaceAll(result, "`", (result.length < maxLineLength) ? "" : ("\n" + doIndent(indent)));
			
		}

		public static function replaceAll (str : String, from: String, to: String){
			var chunks = str.split(from);
			var result = "";
			var sep = "";
			for (var i = 0; i < chunks.length; i++){
				result += sep + chunks[i];
				sep = to;
			}
			return result;
		}
	
		private function doIndent(indent){
			var result = "";
			for (var i = 0; i < indent; i ++){
				result += "     ";
			}
			return result;
		}		
	}
}
広告を非表示にする