Configuration files often contain Boolean parameters for enabling or disabling features in an application or website. This way non-programmers can easily customize complex systems. But what if someone uses another word than “true” (for example, “1″, “yes”, “y”, “on”)?
In order to make my Flash/Flex apps less error-prone, I usually run all Boolean values from external configuration files through a converter. It’s a simple class with a single static method:
-
package
-
{
-
public class BooleanConverter
-
{
-
-
public static const TRUE_VALUES:Array = [ "1", "true", "yes", "y", "on", "enabled" ];
-
-
public static function makeBoolean( val:* ):Boolean
-
{
-
var str:String = String( val ).toLowerCase();
-
-
for ( var i:int = 0; i < BooleanConverter.TRUE_VALUES.length; i++ )
-
{
-
if ( str == BooleanConverter.TRUE_VALUES[i] )
-
{
-
return true;
-
}
-
}
-
return false;
-
}
-
-
}
-
}
The method makeBoolean() accepts any type of data and converts it into a string. The string value is then compared to a list of words which represent the Boolean status true. If no match is found false is returned.
Tags: ActionScript, Flash, Flex
Great blog, subscribed to your rss feed. Thanks.
Great info – keep up the great work.