1  /**
     2   * com.sekati.validate.TypeValidation
     3   * @version 1.0.5
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   * 
     8   * Sourced/adapted from nectere fw for dependencies only
     9   */
    10   
    11  /**
    12   * Simple Type Validation class. 
    13   * @see {@link com.sekati.validate.StringValidation}
    14   */
    15  class com.sekati.validate.TypeValidation {	
    16  
    17  	/**
    18  	 * get an objects type and return an info object containing properties:
    19  	 * {@code
    20  	 * 	.inspectable:Boolean, 
    21  	 * 	.name:String;
    22  	 * 	.stringify:Boolean;
    23  	 * }
    24  	 * @param o (Object)
    25  	 * @return Object
    26  	 */
    27  	public static function getType(o:Object):Object {
    28  		var typeOf:String = typeof (o);
    29  		var type:Object = new Object();
    30  		type.inspectable = true;
    31  		type.name = typeOf;
    32  		if (TypeValidation.isString(o) || TypeValidation.isBoolean(o) || TypeValidation.isNumber(o) || typeOf == "undefined" || typeOf == "null") {
    33  			type.inspectable = false;
    34  		} else if (o instanceof Date) {
    35  			type.inspectable = false;
    36  			type.name = "date";
    37  		} else if (o instanceof Array) {
    38  			type.name = "array";
    39  		} else if (o instanceof Button) {
    40  			type.name = "button";
    41  		} else if (o instanceof MovieClip) {
    42  			type.name = "movieclip";
    43  		} else if (o instanceof XML) {
    44  			type.name = "xml";
    45  			type.stringify = true;
    46  		} else if (o instanceof XMLNode) {
    47  			type.name = "xmlnode";
    48  			type.stringify = true;
    49  		} else if (o instanceof Color) {
    50  			type.name = "color";
    51  		}
    52  		return type;
    53  	}
    54  		
    55  	public static function isNumber(val:Object):Boolean {
    56  		return compare(val, "number", Number);
    57  	}
    58  	
    59  	public static function isString(val:Object):Boolean {
    60  		return compare(val, "string", String);
    61  	}
    62  	
    63  	public static function isFunction(val:Object):Boolean {
    64  		return compare(val, "function", Function);
    65  	}
    66  	
    67  	public static function isBoolean(val:Object):Boolean {
    68  		return compare(val, "boolean", Boolean);
    69  	}
    70  	
    71  	public static function isArray(val:Object):Boolean {
    72  		return compare(val, "array", Array);
    73  	}
    74  	
    75  	public static function isDate(val:Object):Boolean {
    76  		return compare(val, "date", Date);
    77  	}
    78  	
    79  	public static function isSound(val:Object):Boolean {
    80  		return compare(val, "sound", Sound);
    81  	}
    82  	
    83  	public static function isMovieClip(val:Object):Boolean {
    84  		return compare(val, "movieclip", MovieClip);
    85  	}
    86  	
    87  	public static function isTextField(val:Object):Boolean {
    88  		return compare(val, "textfield", TextField);
    89  	}
    90  
    91  	public static function isXML(val:Object):Boolean {
    92  		return compare(val, "xml", XML);
    93  	}
    94  	
    95  	public static function isXMLNode(val:Object):Boolean {
    96  		return compare(val, "xmlnode", XMLNode);
    97  	}
    98  	
    99  	public static function isObject(val:Object):Boolean {
   100  		return compare(val, "object", Object);
   101  	}
   102  	
   103  	public static function isInstanceOf(val:Object, classPath:Function):Boolean {
   104  		return (val instanceof classPath);
   105  	}
   106  	
   107  	public static function isNull(val:Object):Boolean {
   108  		return (val == null || val == undefined || val == "");
   109  	}
   110  	
   111  	private static function compare(val:Object, as1:String, as2:Function):Boolean {
   112  		return (typeof(val) == as1 || val instanceof as2);
   113  	}
   114  	
   115  	private function TypeValidation(){
   116  	}
   117  }