Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active December 20, 2015 21:29
Show Gist options
  • Save JamoCA/6198319 to your computer and use it in GitHub Desktop.
Save JamoCA/6198319 to your computer and use it in GitHub Desktop.
camelSpace is a ColdFusion UDF to convert any ColdFusion (scoped or non-scoped) variable name to a title formatted string. (NOTE: You can test this at CFLive.net)
<p>NOTE: 2 other UDFs exist that have similar functionality.</p>
<p><b>varNameToText()</b> - Incorrectly adds spaces between all uppercase words like "ZIP". Doesn't remove scope.<br>
http://cflib.org/udf/varNameToText</p>
<p><b>camelToSpace()</b> - If first letter is upper, adds a space. Doesn't remove underscores.<br>
http://cflib.org/udf/camelToSpace</p>
<cfscript>
/*
camelSpace: Converts variable name to text string w/title format.
Removes underscores & scope (if exists), uppercases first letter & all first letter of each word
*/
function camelSpace(text){
if (NOT IsSimpleValue(text) OR NOT LEN(TRIM(text))) {text='Undefined';}
text = listLast(text,".");
text = trim(rereplace(text, "_+", " ","all"));
text = trim(rereplace(text, " ([[:lower:]])", " \u\1","all"));
if (Compare(ucase(left(text,1)), left(text,1)) NEQ 0){
text = ucase(left(text,1)) & right(text, len(text)-1);
}
text = trim(Rereplace(text, "([[:upper:]])([[:lower:]])", " \1\2","all"));
return text;
}
</cfscript>
<h2>camelSpace</h2>
<CFOUTPUT>
<CFLOOP LIST="request.FirstName,last_name,FORM.address,URL.city,Attributes.State,ZIP,MyQuery.phone_number,email,whatHappened" INDEX="this">
"#this#" = <b>"#CamelSpace(this)#"</b><br>
</CFLOOP>
</CFOUTPUT>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment