/* Copyright (c) 2008 Mike Petty: BigFatStogie. See: http://blog.bigfatstogie.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package com.bfsAPI.data.utility { //import com.bfsAPI.managers.SubVersion; /** * TypeConversion * * Contains class members to convert most strings into their possible primatives. * One method in particular: toPrimative returns an untyped object will attempt * to do this conversion for you. * Nearly all other methods have a useWeakReference parameter which uses a method * for conversion with less stringent rules. For instance, in toBoolean, will use the * Boolean() global method which would return true for 'true' or 'false'. Without * the reference, it will evaluate if the parameter is 'true' or 'false', and if * neither returns null. * * * @author Mike Petty * @author-email mike@bigfatstogie.com * @date 04.10.2007 * @version 1.00 * @last-revision-date 01.31.2007 * @svn-revision $Revision: 3434 $ * @svn-revision-date $Date: 2007-01-31 16:56:55 -0500 (Wed, 31 Jan 2007) $ * @history * 1.02 04.10.2007 * Migration to AS3 / Flex environment * * 1.01 01.31.2007 * Added revised SubVersion management. It is now possible to use version validation at runtime. * This allows classes to check that the proper version or dated file has been included, and not * overwritten with an older version when dynamically including items. * * 1.00 08.01.2006 * Initial development * * @notes * * @usage * var tc:TypeConversion =TypeConversion.getInstance(); * var isPlaying:Boolean =tc.toBoolean(new XML(data).@playing); * */ public class TypeConversion { private static const classRev:String ='$Revision$'; private static const classDte:String ='$Date$'; private static const classAthr:String ='$Author$'; private static var instance:TypeConversion =new TypeConversion(); public function TypeConversion() { if (instance) { throw new Error('Singleton Pattern: use TypeConversion.getInstance() instead.'); } } //@:: Singleton Constructor Access //must access constructor through getter: var myvar =typeConversion.gettypeConversion(); public static function getInstance():TypeConversion { if (instance ==null) { instance =new TypeConversion(); } return instance; } //@:: Public Class Methods /** * Converts a string to a Boolean value. * * @param str String * @param useWeakConversion Boolean Uses the global Boolean() function to determine the boolean output, * when false, uses strict string 'true' / 'false' to determine if it can be converted. * @return Boolean. */ public static function toBoolean(str:String, useWeakConversion:Boolean =false):Boolean { return useWeakConversion ? Boolean(str) : str.toLowerCase() =='true' ? true : str.toLowerCase() =='false' ? false : null; } public static function toNumber(str:String):Number { return Number(str); } /** * Converts a string to a signed integer. * * @param str String The string-based number representation to convert * @param useWeakConversion Boolean Allows conversion to use a weaker policy via parseInt. * @return int */ public static function toInt(str:String, useWeakConversion:Boolean =false):int { return useWeakConversion ? int(parseInt(str)) : !isNaN(Number(str)) ? int(str) : null; } /** * Converts a string to an unsigned integer * * @param str String The string-based number representation to convert * @param useWeakConversion Boolean Allows conversion to use a weaker policy via parseInt. * @return uint */ public static function toUint(str:String, useWeakConversion:Boolean =false):uint { return useWeakConversion ? uint(parseInt(str)) : !isNaN(Number(str)) ? uint(str) : null; } public static function toArray(str:String, delimiter:String =','):Array { return str.split(delimiter); } /** * An auto dataType conversion from String. * * @description This method automatically converts the string parameter to a known primative dataType. Numbers are first * checked if it can be typed as either an int or uint before resorting to Number. * @param str String * @throws ArgumentError str cannot be null or empty * @return [null, Boolean, Number, int, uint] */ public static function toPrimative(str:String):* { if (!str.length) { throw new ArgumentError('TypeConversion.toPrimative: The argument str cannot be null or empty'); } else if (str.toLowerCase() =="null" || str.toLowerCase() =="undefined") { return null; } if (toBoolean(str)) { return toBoolean(str); } else if (!isNaN(Number(str))) { var num:Number =Number(str); return num <0 || num