Utility Function: ListRemoveDuplicates()

I often use lists behind the scenes, and if I'm looping through and processing stuff, I had to reprocess something if its been duplicated in the list.


<cffunction name="ListRemoveDuplicates" access="public" returntype="string" output="false">
    <cfargument name="lsList" type="string" required="false" default="">
    <cfargument name="delimiters" type="string" required="false" default=",">

    <cfset var lsNewList = "">

    <cfloop list="#arguments.lsList#" index="listItem" delimiters="#arguments.delimiters#">
        <cfif NOT listFind(lsNewList, listItem, arguments.delimiters)>
            <cfset lsNewList = listAppend(lsNewList, listItem, arguments.delimiters)>
        </cfif>
    </cfloop>

    <cfreturn lsNewList>
</cffunction>

This should probably be ListRemoveDups() as is more fitting for CF, but I prefer the longer name

 

Utility Function: GetCurrentDirectory()

This one is really short... I use code like this all over the place, and its always cumbersome to do the nested function call every time.


<cffunction name="GetCurrentDirectory" output="false" access="public" returntype="string">
    <cfreturn GetDirectoryFromPath(GetBaseTemplatePath()) />
</cffunction>

 

Utility Function: DateBetween()

I get so tired of doing date comparisons to see if a date is between two other dates. Its not hard, but I always feel like it should be a method.


<cffunction name="DateBetween" output="false" access="public" returntype="boolean">
    <cfargument name="testDate" type="date" required="true" />
    <cfargument name="lowDate" type="date" required="true" />
    <cfargument name="highDate" type="date" required="true" />
    <cfargument name="inclusive" type="boolean" required="false" default="true" />
    
    <cfif arguments.inclusive>
        <cfreturn (lowDate lte testDate AND testDate lte highDate) />
    <cfelse>
        <cfreturn (lowDate lt testDate AND testDate lt highDate) />
    </cfif>
</cffunction>

 

IsDate() and SQL Server

I don't know about you, but my company uses the IsDate() function for a lot of simple validation of date inputs, and it works fairly well for the most part. The only problem I have with it is that when paired with SQL Server (2000 or 2005), it still can't capture all invalid dates.

The date 'January 5, 753' is a valid date, but attempting to insert it into a SQL Server datetime object throws an error because valid dates for SQL Server are between January 1, 1753 and December 31, 9999. Dates above the year 9999 are caught by IsDate(), but the lower bound is not.


<cffunction name="IsSQLServerDate" returntype="boolean" output="false">
    <cfargument name="date" type="date" required="true"/>
    <cfargument name="type" type="string" required="false" default="datetime"/>

    <cfswitch expression="#arguments.type#">
        <cfcase value="datetime">
            <cfreturn IsDate(arguments.date) AND Year(arguments.date) gte 1753 />
        </cfcase>
        <cfcase value="smalldatetime">
            <cfreturn IsDate(arguments.date) AND DateCompare(arguments.date, '1/1/1900') gte 0 AND DateCompare(arguments.date, '6/2/2079') lte 0 />
        </cfcase>
    </cfswitch>
</cffunction>

Nothing that someone else couldn't write, but useful. to save headaches.

 
Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me on LinkedIn.