Extending Coldfusion with UDFs
I cobbled together a few functions you may find useful this morning. As you can see they are simple, albeit useful, and so I wanted to share them here. Enjoy, and keep on coding!
/*
FUNCTION: ArrayFindString
PURPOSE: Searches inside the specified array (array_obj)
for a string matching string (str)
PARAMETERS: 1. array_obj: the array containing the strings you
want to try to match to str
2. str: the string you will be looking for in array_obj
RETURNS: boolean
NOTE: not case sensitive
*/
function ArrayFindString(array_obj, str){
var i = 1;
for (i = 1; i lte arrayLen(array_obj); i = i + 1)
if (array_obj[i] eq str) return true;
return false;
}
/*
FUNCTION: ArrayContainsString
PURPOSE: Searches inside the specified array (array_obj) for a
string that contains the string (str)
PARAMETERS: 1. array_obj: the array containing the strings you want
to try to match to str
2. str: the string you will be looking for in array_obj
RETURNS: boolean
NOTE: not case sensitive
*/
function ArrayContainsString(array_obj, str){
var i = 1;
for (i = 1; i lte arrayLen(array_obj); i = i + 1)
if (findNoCase(str, array_obj[i])) return true;
return false;
}
/*
FUNCTION: StringSearchArray
PURPOSE: Searches inside the specified string (str) for any
string matches contained in the array (array_obj)
PARAMETERS: 1. str: the string you will be searching inside of
2. array_obj: the array containing the strings you want
to look for in str
RETURNS: boolean
NOTE: not case sensitive
*/
function StringSearchArray(str, array_obj){
var i = 1;
for (i = 1; i lte arrayLen(array_obj); i = i + 1)
if (findNoCase(array_obj[i], str)) return true;
return false;
}
If your curious, I use StringSearchArray to control which areas of my application require user login:
...
foldersNoLogin = arrayNew(1);
foldersNoLogin[1] = "/login";
foldersNoLogin[2] = "/registration";
</cfscript>
<cfif not isDefined("session.loggedin") or session.loggedin eq false>
<cfif not StringSearchArray(cgi.path_info, foldersNoLogin)>
<cflocation addtoken="no" url="/login/">
</cfif>
</cfif>



