Skip to content

Instantly share code, notes, and snippets.

@cfg
Created July 27, 2011 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfg/1110167 to your computer and use it in GitHub Desktop.
Save cfg/1110167 to your computer and use it in GitHub Desktop.
Mimic PHP's empty() function in ColdFusion
<!---
See: http://coreygilmore.com/blog/2008/05/01/mimic-phps-empty-function-in-coldfusion/
--->
<cfscript>
function empty(val) {
/**
* Return TRUE if one of the following conditions
* for a defined variable is met:
* - A *trimmed* string is empty
* - An array or struct is empty
* - A query has a recordcount of 0
* - A bool is false
* - A number is 0
*
* For all other values including IsDate(testVar) it returns false.
*
* Similar to the php empty() function, www.php.net/empty
*
* @param val Variable to test. (Required)
* @return Returns a boolean.
* @author Corey Gilmore (http://coreygilmore.com/)
*
*/
if( IsSimpleValue(val) ) {
if( IsDate(val) ) {
return false; // no validation here
} else if( IsNumeric(val) ) {
return YesNoFormat(val EQ 0);
} else if( IsBoolean(val) ) {
return NOT YesNoFormat(val);
} else {
// assume string
return NOT YesNoFormat( Len(Trim(val)) );
}
} else {
if( IsArray(val) ) {
return NOT YesNoFormat( ArrayLen(val) );
} else if( IsStruct(val) ) {
return StructIsEmpty(val);
} else if( IsQuery(val) ) {
return NOT val.recordcount;
}
}
return false;
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment