Utility Function: GetInstanceName()

I can't claim the core of this function as my own, it comes from a presentation by Adam Lehman on ColdFusion server administration. I wrapped his code snippet into a function for reuse.

<cffunction name="GetInstanceName" access="public" output="false" returntype="string">        
    <cfreturn createObject("java", "jrunx.kernel.JRun").getServerName() />
</cffunction>

 

My first Webkit and FireFox 3 standards let down

So today I finally found a part of the HTML 4.01 standard that Internet Explorer 6 (and 7) follows better then FireFox 3 and Webkit: <colgroup> and lt;col> tags. Apparently, neither FF3 or Webkit (and thus Chrome) bother to implement these tags. Normally, FF3 and Webkit are on the right side of standards, so its kind of a disappointment, especially when I need the functionality of those tags for a project.

If you aren't familiar with them, browse on over to W3School's XHTML reference and get acquainted. Basically, with column groups you can implement nice things like setting a column's background color without adding classes to each lt;td> in that column.

I hope that FF3 and Webkit see there way to implementing this feature.

 

Utility Functions: ArrayRandomize()

A simple array randomization option. Note that although I use "CFMX_COMPAT" as the default randomization algorithm, you should probably always use "SHA1PRNG" to get better results.

<cffunction name="ArrayRandomize" access="public" output="true" returntype="array">
    <cfargument name="array" type="array" required="true" />
    <cfargument name="algorithm" type="string" required="false" default="CFMX_COMPAT" />
    
    <!---
        Fisher-Yates shuffle
        http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
     --->

    <cfset var i = ArrayLen(arguments.array) />
    <cfset var k = 0 />
    <cfset var temp = "" />
    
    <cfif i gt 0>
        <cfloop condition="i gt 0">
            <cfset k = RandRange(1, i, arguments.algorithm) />
            
            <cfset temp = arguments.array[i] />
            <cfset arguments.array[i] = arguments.array[k] />
            <cfset arguments.array[k] = temp />
            <cfset i = i - 1 />
        </cfloop>
    </cfif>
    
    <cfreturn arguments.array />
</cffunction>

 

Utility Function: RequireScript()

I'm not too happy with with this implementation, but I think it works out fairly well. Note that it uses the two functions in the related posts.

<cffunction name="RequireScript" access="public" output="false" returntype="void">
<cfargument name="script" type="string" required="true" />

<cfif NOT StructKeyExists(request, "__requirescript")>
        <cfset request["__requirescript"] = ArrayNew(1) />
    </cfif>
    <cfif NOT ArrayFind(request["__requirescript"], arguments.script)>
        <cfset ArrayAppend(request["__requirescript"], arguments.script) />
        <cfset IncludeScript(arguments.script) />
    </cfif>
    
<cfreturn />
</cffunction>

 

Utility Function: ListIsEmpty()

While this is a simple bit of code to write, I personally like the syntax of checking "something is empty" over writing len(something) gt 0.

<cffunction name="ListIsEmpty" access="public" output="false" returntype="boolean">
    <cfargument name="list" type="string" required="true" />
    <cfargument name="delimiters" type="string" required="false" default="," />
    
    <cfreturn (ListLen(arguments.list, arguments.delimiters) gt 0) />
</cffunction>

 

Utility Function: ArrayFind()

I sometimes need to search an array of strings for a specific value, so I came up with this function.

<cffunction name="ArrayFind" access="public" output="false" returntype="numeric">
    <cfargument name="array" type="array" required="true" />
    <cfargument name="find" type="any" required="true" />
    
    <cfset var position = 0 />
    <cfset var x = 0 />
    
    <cfloop from="1" to="#ArrayLen(arguments.array)#" index="x">
        <cfif arguments.array[x] eq arguments.find>
            <cfset position = x />
            <cfbreak />
        </cfif>
    </cfloop>
    
    <cfreturn position />
</cffunction>

 

More Entries

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.