Last active
March 21, 2020 11:05
What If ColdFusion Recognized More Truthy / Falsey Values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction | |
name="truthy" | |
access="public" | |
returntype="boolean" | |
output="false" | |
hint="I determine if the given argument can be converted / shoe-horned into a boolean value. By default, we will be trying to create a FALSE; everything else will be TRUE."> | |
<!--- Define arguments. ---> | |
<cfargument | |
name="value" | |
type="any" | |
required="false" | |
hint="I am the truthy / falsey value being converted into a Boolean value." | |
/> | |
<!--- | |
Check to see if the given argument can be converted to a | |
False value. The following will be considered falseys: | |
-- NULL / undefined value | |
-- False (boolean) | |
-- Empty queries | |
-- Empty arrays | |
-- Empty structs | |
-- Zero (numeric) [same as Boolean already] | |
-- Empty strings | |
---> | |
<cfif ( | |
!structKeyExists( arguments, "value" ) | |
|| | |
( | |
isBoolean( arguments.value ) && | |
!arguments.value | |
) | |
|| | |
( | |
isQuery( arguments.value ) && | |
!arguments.value.recordCount | |
) | |
|| | |
( | |
isArray( arguments.value ) && | |
!arrayLen( arguments.value ) | |
) | |
|| | |
( | |
isStruct( arguments.value ) && | |
!structCount( arguments.value ) | |
) | |
|| | |
( | |
isStruct( arguments.value ) && | |
structKeyExists( arguments.value, "toBoolean" ) && | |
!arguments.value.toBoolean() | |
) | |
|| | |
( | |
isSimpleValue( arguments.value ) && | |
!len( arguments.value ) | |
) | |
)> | |
<!--- This can be considered a false. ---> | |
<cfreturn false /> | |
</cfif> | |
<!--- | |
If we made it this far, then the given value could not be | |
converted to a False; as such, it must be True (that which | |
is not false must be true - there is no room for anything | |
in between). | |
---> | |
<cfreturn true /> | |
</cffunction> | |
<!--- ----------------------------------------------------- ---> | |
<!--- ----------------------------------------------------- ---> | |
<!--- ----------------------------------------------------- ---> | |
<!--- ----------------------------------------------------- ---> | |
<!--- Build up an empty query. ---> | |
<cfset myQuery = queryNew( "id, name" ) /> | |
<!--- Build up an empty array. ---> | |
<cfset myArray = [] /> | |
<!--- Build up an empty struct. ---> | |
<cfset myStruct = {} /> | |
<!--- Build up an empty string. ---> | |
<cfset myString = "" /> | |
<!--- Now, let's test each of these "empty" data types. ---> | |
<cfoutput> | |
<!--- Null value. ---> | |
<cfif truthy( javaCast( "null", "" ) )> | |
Null Value: True | |
<cfelse> | |
Null Value: False | |
</cfif> | |
<br /> | |
<!--- Empty query. ---> | |
<cfif truthy( myQuery )> | |
Empty Query: True | |
<cfelse> | |
Empty Query: False | |
</cfif> | |
<br /> | |
<!--- Empty array. ---> | |
<cfif truthy( myArray )> | |
Empty Array: True | |
<cfelse> | |
Empty Array: False | |
</cfif> | |
<br /> | |
<!--- Empty struct. ---> | |
<cfif truthy( myStruct )> | |
Empty Struct: True | |
<cfelse> | |
Empty Struct: False | |
</cfif> | |
<br /> | |
<!--- Empty string. ---> | |
<cfif truthy( myString )> | |
Empty String: True | |
<cfelse> | |
Empty String: False | |
</cfif> | |
</cfoutput> | |
<!--- ----------------------------------------------------- ---> | |
<!--- ----------------------------------------------------- ---> | |
<!--- ----------------------------------------------------- ---> | |
<!--- ----------------------------------------------------- ---> | |
<!--- | |
To make sure we aren't getting false Falses, let's run the | |
same tests with non-empty values. | |
---> | |
<!--- Build up a non-empty query. ---> | |
<cfset queryAddRow( myQuery, 1 ) /> | |
<!--- Build up a non-empty array. ---> | |
<cfset myArray = [ "Joanna" ] /> | |
<!--- Build up a non-empty struct. ---> | |
<cfset myStruct = { tricia = "my BFF" } /> | |
<!--- Build up a non-empty string. ---> | |
<cfset myString = "Outstanding" /> | |
<!--- Now, let's test each of these "full" data types. ---> | |
<cfoutput> | |
<br /> | |
<br /> | |
<!--- Non-empty query. ---> | |
<cfif truthy( myQuery )> | |
Non-Empty Query: True | |
<cfelse> | |
Non-Empty Query: False | |
</cfif> | |
<br /> | |
<!--- Non-empty array. ---> | |
<cfif truthy( myArray )> | |
Non-Empty Array: True | |
<cfelse> | |
Non-Empty Array: False | |
</cfif> | |
<br /> | |
<!--- Non-empty struct. ---> | |
<cfif truthy( myStruct )> | |
Non-Empty Struct: True | |
<cfelse> | |
Non-Empty Struct: False | |
</cfif> | |
<br /> | |
<!--- Non-empty string. ---> | |
<cfif truthy( myString )> | |
Non-Empty String: True | |
<cfelse> | |
Non-Empty String: False | |
</cfif> | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment