CFLib.org – Common Function Library Project

toBoolean([arg])

Last updated August 26, 2012

author

Steve Withington

Version: 1 | Requires: CF9 | Library: StrLib

Description:
Pass me a value, expression, or anything you want and I'll give you back its boolean value (true || false). I'm especially useful when mixing CFML with JavaScript and you need an actual 'true' or 'false' value to pass in as an argument to a js call.

Return Values:
Returns a boolean: true if arg was a boolean and true, otherwise false

Example:

test = {a=[[],[]],b='1,2,3'};
toBoolean(test); // returns false

test = 'php is great';
toBoolean(test); // returns false

test = 'cfml is awesome' == 'cfml is awesome';
toBoolean(test); // returns true

Parameters:

Name Description Required
arg Any value No

Full UDF Source:

/**
 * I convert anything to boolean
 * version 0.1 by Steve Withington
 * version 1.0 by Adam Cameron: simplifying logic
 * 
 * @param arg 	 Any value (Optional)
 * @return Returns a boolean: true if arg was a boolean and true, otherwise false 
 * @author Steve Withington (stephenwithington@gmail.com) 
 * @version 1, August 26, 2012 
 */
public boolean function toBoolean(any arg='') {
	return isBoolean(arg) && arg ? true : false;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson