function to make string title case

April 8, 2010

Here’s a simple coldfusion function to make a string title case(every word of the string starting with capital letters). You can pass in exceptions in a list.

<cffunction name=”makeTitleCase” access=”public” returntype=”string” output=”no”>
<cfargument name=”str” type=”string” required=”yes”/>
<cfargument name=”excludeList” type=”string” required=”no” default=”an,the,at,by,for,of,in,up,on,to,and,as,but,if,or,nor,a,is”/>
<cfset var i = 1>
<cfset var wordArray = “”>
<cfscript>
if(arguments.str.trim().length() EQ 0) {
return arguments.str;
}
wordArray = listToArray(arguments.str,” “);
for(i=1;i<=arrayLen(wordArray);i++) {
if(not listfindnocase(arguments.excludeList,wordArray[i])) {
wordArray[i] = wordArray[i].substring(0, 1).toUpperCase() &  wordArray[i].substring(1);
}
}
</cfscript>
<cfreturn arrayToList(wordArray,” “)/>
</cffunction>

it uses some underlying JAVA functionality to manipulate the string.

Enjoy!


ColdFusion : Use arrays, lists are slower!

March 18, 2010

If you have to handle a list of data in coldfusion, you could end up using a list or an array. Well choose and array working with the data in the array is much faster compared to lists. lets do a little test for that theory:

lets define our list and array

<cfset list = “”>

<cfset array = arraynew(1)>

now lets do a loop and insert data in the list and the array

<cftimer label=”myTimerString” type=”debug”>
<cfloop from=”1″ to=”5000″ step=”1″ index=”j”>
<cfset list = listprepend(list,j)>
</cfloop>
<cfset listsort(list,”numeric”,”asc”)>
</cftimer>
<cftimer label=”myTimerArray” type=”debug”>
<cfloop from=”1″ to=”5000″ step=”1″ index=”j”>
<cfset arrayappend(array,j)>
</cfloop>
<cfset arraysort(array,”numeric”,”asc”)>
</cftimer>

so we loop from 1 to 5000 and insert the data in the list and the array surrounding with cftimer tags to see the execution times in our debug info. the results are not surprising at all. Arrays being the clear winner. Here are the results for three executions

CFTimer Times

[172ms] myTimerString

[0ms] myTimerArray

CFTimer Times

[187ms] myTimerString

[0ms] myTimerArray

CFTimer Times

[172ms] myTimerString

[0ms] myTimerArray

So when handling list data’s use arrays for your data storing and sorting. If u need to have a list at the end you can always use the arrayToList() function to get it in a list.

Interestingly in JAVA there is very little difference doing the same test. Thats a bit weird considering that coldfusion itself runs on JAVA.

peace out!


Coldfusion autosuggest for text box!

March 12, 2010

Coldfusion makes it really easy to add autosuggest to a textbox (CF 8 and above only). the cfinput tag has a new attribute (autosuggest) that takes a list as an input and matches the entered text with this list.

so we have our list

<cfset myList = “jack,corey,david,sam”>

making the autosuggest is as easy as just adding the list to the cfinput tag

<cfform>

<cfinput name=”myInput” type=”text” autoSuggest=”#myList#”/>

</cfform>

You can also use data from a query to populate the textbox.

<cfquery name=”myQuery” datasource=”myDSN”>

SELECT firstName FROM Customer

</cfquery>

the input then changes to

<cfform>

<cfinput name=”myInput” type=”text” autoSuggest=”#valueList(myQuery.firstName)#”/>

</cfform>

One thing to keep in mind is that this way you append the whole list to the inline HTML of the page, so if you have a small list then its not a problem but if you have a massive list to check with, the page size could increase heaps. In those cases it is wiser to bind the input field to a CFC else use jQuery to make the autosuggest getting data via ajax from another page(i will post a jQuery alternative soon).

so here’s the final cfinput bound to a CFC

<cfform>

<cfinput name=”myInput” type=”text” autoSuggest=”cfc:myCFC.myFunction({value})”/>

</cfform>

Of course the function will have access set to remote and watch out for the onRequest function in the Application.cfc in case you use it.


Use underlying JAVA function to create UUID instead of createUUID()!

March 7, 2010

Coldfusion createUUID() function is slow. I find using the underlying JAVA class UUID in the java.util package is much faster. Here’s how we do it in coldfusion -

<cfset myUUID = createobject(“java”, “java.util.UUID”) .randomUUID().toString()/>

The createObject function allows us to access the underlying JAVA functions, so we access the java.util.UUID class and then create the UUID using the randomUUID() function and finally converting to string using toString() function. If you will be creating UUID more than once on the page i would recommend creating the object once at the top if the page and then using the functions to create the UUID.

Just another example of the using the awesome power of JAVA in coldfusion.


Write queries keeping plan cache in mind!

March 7, 2010

We all know SQL Server has a plan cache for queries being executed. The query plans get cached and reused as the same query gets executed again. We can look after the plan cache by making sure we follow a few basics when writing queries.

The demo is for coldfusion using the cfquery tag and the same applies for any development environment passing queries to SQL server.

Here’s our first query

<cfquery datasource=”myDB”>

SELECT * FROM myTable WHERE foo=1;

</cfquery>

Here’s our second query

<cfquery datasource=”myDB”>

SELECT * FROM  myTable WHERE foo=1 ;

</cfquery>

Although they appear to be the same, SQL server will store them as two separate query plans because of the extra space between the foo=1 and the query end symbol ‘;’. You can check the plans are stored separately in the cache using the DMV sys.dm_exec_cached_plans (2005 and upward only).

Just something to keep in mind while writing queries!


jQuery : textarea maxlength indicator!

March 7, 2010

jquery is super cool! Here’s how easy it is to add a max length indicator using jQuery.

Firstly we have our textare inside a div layer

<div id=”commentDiv”>
<label for=”comment-text”>Comment:</label>
<textarea rows=”7″ cols=”30″ name=”comment-text” id=”comment-text”></textarea>
</div>

Then we have our jQuery code that displays and updates the indicator.

$(document).ready(function(){
var maxLength = 100;
$(‘#commentDiv’).append(‘<span id=”length”>’+maxLength+’</span>’);
$(‘.maxlength’).keyup(function(e) {
$(‘#length’).text(maxLength-$(this).val().length);
if ($(this).val().length >= maxLength) {
e.preventDefault();
}
});
});

The append function adds the span where we display the maxlength value to start with. You can add it in the HTML code on the page if you like. The keyup function then binds to the textarea. Then we change the value in the span tags using the .text function. The if condition stops the textarea from going over the maximum length set by us.

There you go , done and dusted in about 10 lines of code.


Coldfusion function with returntype string has a leading space!

February 21, 2010

When dealing with coldfusion functions, i noticed my function which returns a string always came back with a leading space for some reason.

<cffunction name=”myFunction” access=”public” returntype=”string”>

<cfreturn “myString”/>

</cffunction>

When the function is called and you output the string – (i wont bother with creating the component and creating the object, you get the idea!)

<cfoutput>’#myFunction()#’</cfoutput>

outputs:-

‘ myString’

To remove this leading space i added the output attribute to the function and voila, it’s gone.

<cffunction name=”myFunction” access=”public” returntype=”string” output=”false”>

<cfreturn “myString”/>

</cffunction>

<cfoutput>’#myFunction()#’</cfoutput>

outputs:-

‘myString’

I have no idea why this happens. If someone knows why this happens and is expected behaviour, please post your comments.


Follow

Get every new post delivered to your Inbox.