瀏覽代碼

added toEnum and toClass.

Nicolas Cannasse 19 年之前
父節點
當前提交
bfd9d7a91b
共有 1 個文件被更改,包括 36 次插入0 次删除
  1. 36 0
      std/Type.hx

+ 36 - 0
std/Type.hx

@@ -35,6 +35,42 @@ enum ValueType {
 **/
 class Type {
 
+	/**
+		Converts a value to an Enum or returns [null] if the value is not an Enum.
+	**/
+	public static function toEnum( t : Dynamic ) : Enum untyped {
+		try {
+		#if flash9
+			if( !t.__isenum )
+				return null;
+		#else true
+			if( t.__ename__ == null )
+				return null;
+		#end
+			return t;
+		} catch( e : Dynamic ) {
+			return null;
+		}
+	}
+	
+	/**
+		Converts a value to a Class or returns [null] if the value is not a Class.
+	**/
+	public static function toClass( t : Dynamic ) : Class untyped {
+		try {
+		#if flash9
+			if( !t.hasOwnProperty("prototype") )
+				return null;
+		#else true
+			if( t.__name__ == null )
+				return null;
+		#end
+			return t;
+		} catch( e : Dynamic ) {
+			return null;
+		}
+	}
+	
 	/**
 		Returns the class of a value or [null] if this value is not a Class instance.
 	**/