Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active December 2, 2022 20:05
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 JamoCA/56a6e1f32a353b23c13e71b028c7551f to your computer and use it in GitHub Desktop.
Save JamoCA/56a6e1f32a353b23c13e71b028c7551f to your computer and use it in GitHub Desktop.
ColdFusion functions with return type "boolean" returns Double, Integer, String or CFBoolean, but not "boolean" unless you explicitly & manually cast it.
<!--- 20221202 A ColdFusion function w/return type of "boolean" won't return boolean unless you explicitly & manually cast it.
- James Moberg - SunStar Media
Gist: https://gist.github.com/JamoCA/56a6e1f32a353b23c13e71b028c7551f
Tweet: https://twitter.com/gamesover/status/1598770171057168384
If not explicitly cast, Lucee & Adobe may return a "Double", "Integer", or "String" datatype.
Adobe 2018-2021 sometimes returns a "CFBoolean" type & CF2016 only returns "String".
I'm suprised that Adobe doesn't auto-trim values before evaluating. (The isValid("email") BIF auto-trims before validating.)
Probably "not a bug", but I'd prefer that functions with a return type of "boolean" return an typed boolean value and not
some value that can be coerced-to-boolean.
NOTE: Separate issue, when dumping boolean data types, Adobe displays YES/NO and Lucee returns true/false.
(I personally prefer Lucee's true/false.)
--->
<cfscript>
boolean function isTrue(any input=""){
return (isvalid("boolean", arguments.input)) ? arguments.input : false;
}
boolean function isTrue_casted(any input=""){
local.r = (isvalid("boolean", arguments.input)) ? arguments.input : false;
return javacast("boolean", local.r);
}
results = [];
tests = [1, 0, -1, javacast("string",""), "abc", " ", " 1 ", "yes", "no", javacast("boolean",1)];
for (test in tests){
result = [
"value": test
,"isBoolean": isBoolean(test)
,"isTrue": isTrue(test)
,"isTrue_type": getMetadata(isTrue(test)).getName()
,"isTrueCasted": isTrue_Casted(test)
,"isTrueCasted_type": getMetadata(isTrue_Casted(test)).getName()
];
arrayappend(results, result);
}
writedump(var=results, label="Boolean Results");
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment