package com.natejc.utils { // ********************************************************************************** // ********************************************************************************** /** * The static CustomCaster class is used to cast a value, passed as a String, into * the appropriate primitive datatype. * * @author Nate Chatellier */ public class CustomCaster { // ********************************************************************************** /** * CustomCaster is a static class and should not be instantiated. */ public function CustomCaster() { trace("CustomCaster is a static class and should not be instantiated."); } // END CONSTRUCTOR // ********************************************************************************** /** * Returns value casted as an object of type sDataType. * * @param sDataType The datatype of value as a String. * @param value The value that should be returned casted as the appropriate type. * @return value as the appropriate datatype. */ public static function customCast(sDataType:String, value:*):* { switch(sDataType.toLowerCase()) { case "int": case "int_8": case "int_16": case "int_32": var intValue:int = value; return intValue; break; case "uint": case "int_8": case "uint_16": case "uint_32": var uintValue:uint = value; return uintValue; break; case "number": case "float": var numberValue:Number = value; return numberValue; break; case "boolean": case "bool": var booleanValue:Boolean = value; if (String(value).toLowerCase() == "false" || String(value) == "0") booleanValue = false; // handles odd XML casting bug if (String(value).toLowerCase() == "true" || String(value) == "1") booleanValue = true; // handles odd XML casting bug return booleanValue; break; case "string": case "char": var stringValue:String = value; return stringValue; break; case "array": var arrayValue:Array = String(value).split(","); return arrayValue; break; case "xml": var xmlValue:XML = XML(value); return xmlValue; break; } // END SWITCH } // END FUNCTION customCast // ********************************************************************************** /** * Returns the most restrictive valid datatype of dataObject written as a String. * For example, a variable of type int with the value of 5 will return "uint". However, a variable * of type int with the value of -5 will return "int". * * @param dataObject The datatype object that should have its datatype detected. * @return The datatype of dataObject written as a String, or * "undefined" fo the datatype cannot be determined. */ public static function customTypeOf(dataObject:*):String { if (dataObject is uint) return "uint"; if (dataObject is int) return "int"; if (dataObject is Number) return "Number"; if (dataObject is Boolean) return "Boolean"; if (dataObject is XML) return "XML"; if (dataObject is String) return "String"; if (dataObject is Array) return "Array"; if (dataObject is Sprite) return "Sprite"; if (dataObject is Object) return "Object"; return "undefined"; } // END FUNCTION customTypeOf // ********************************************************************************** // ********************************************************************************** } // END CLASS CustomCaster } // END PACKAGE