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>

 

Utility Function: RenderTemplate()

This function is half-way to being a really nice function. I just wish that I could pass in arguments, or a structure to use as the variables available in the rendered template. I'd love to have that isolation...


<cffunction name="RenderTemplate" access="public" output="false" returnType="string">
    <cfargument name="path" type="string" required="false" />
    
    <cfset var local = StructNew() />
    
    <cfsavecontent variable="local.returnValue">
        <cfinclude template="#arguments.path#" />
    </cfsavecontent>
    
    <cfreturn local.returnValue />
</cffunction>

 

Utility Function: JSClean()

This one I use a lot when writing out JSON. It probably is missing something that will still break a JS string, but its all I've found so far.


<cffunction name="JSClean" access="public" returntype="string" output="false">
    <cfargument name="sString" type="string" required="true" />
        
    <cfset var sReturn = REReplaceNoCase(Trim(arguments.sString), "\s", " ", "ALL") />
        
    <cfloop list="\:':"":&" delimiters=":" index="x">
        <cfset sReturn = ReplaceNoCase(sReturn, x, "\#x#", "ALL") />
    </cfloop>
        
    <cfreturn sReturn />
</cffunction>

 

Utility Function: Redirect()

Another simple wrapper function, this one for <cflocation/>.


<cffunction name="Redirect" access="public" output="false" returntype="void">
    <cfargument name="url" type="string" required="true" />
    <cfargument name="addToken" type="boolean" required="false" default="false" />
        
    <cflocation url="#arguments.url#" addtoken="#arguments.addToken#" />
    <cfabort />
        
    <cfreturn />
</cffunction>

 

Utility Function: IncludeScript()

I use this one to dynamically include JS I need for a specific page. Its really just a specialized wrapper for <cfhtmlhead/> but I like it better.


<cffunction name="IncludeScript" access="public" output="false" returntype="void">
    <cfargument name="script" type="string" required="true" />
        
    <cfhtmlhead text="<script type=""text/javascript"" src=""#arguments.script#""></script>" />
        
    <cfreturn />
</cffunction>

 

Utility Function: IsColor()

I think that there are some valid colors that the REGEX is going to reject, but here is another one that I find useful:


<cffunction name="IsColor" access="public" output="false" returntype="boolean">
    <cfargument name="color" type="string" required="true" />
        
    <cfset var local = structNew() />
    <cfset local.colorNames = "aqua,black,blue,fuchsia,gray,green,lime,maroon,navy,olive,purple,red,silver,teal,white,yellow" />
    <cfset local.regex = "^(##([\dA-F]{3}|[\dA-F]{6})|([\dA-F]{3}|[\dA-F]{6}))$" />
    <cfset local.returnValue = false />
        
    <cfif listFind(local.colorNames, arguments.color) OR arrayLen(REMatchNoCase(local.regex, arguments.color)) gt 0>
        <cfset local.returnValue = true />
    </cfif>
    
    <cfreturn local.returnValue />
</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.