Home > Free Stuff > Extending Coldfusion with UDFs

Extending Coldfusion with UDFs

December 3rd, 2008

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>

Related posts:

  1. Using a Captcha with Cold Fusion 8 Anyone who has spent time web surfing in the last...
  2. FREE CODE: Paging record list. When listing records from a database you will often need...

Free Stuff , , ,

  1. | #1

    Very good information. I would like to add that I like to store my commonly used functions in the server scope. That way you don’t have to declare them for every page, and they are always available.

    I use a custom tag to include all my functions and set the server scope. There is also a global error handler that if it detects an error because any function has been called and does not exist, it calls the tag to include all the functions into the server scope. Very useful for global persistent functions.

  2. | #2

    @Shawn Bailly

    Is your library of UDFs available to download? and if so could you post it here on the site?

  3. Heath Provost
    | #3

    I wrote this one a while back. Has come in handy on a few occasions.

    /*
    FUNCTION:   formatSize
    PURPOSE:    Converts a numeric size value to an appropriate
                string representation using bits, bytes, kilobytes,
                megabytes, gigabytes, terabytes, or petabytes as
                needed
    PARAMETERS: 1. sz:     numeric  -  Value to convert.
                2. unit:   string   -  Unit used for sz.
                                           B|KB|MB|GB|TB|PB.
                                           Defaults to B.
                3. format: string   -  A format specifier as used by
                                            the built-in NumberFormat
                                            function.
                                            Defaults to ",.00"
    RETURNS:    string
    */
    
    function formatSize(sz) {
      var result = "";
      var validUnits = "B,KB,MB,GB,TB,PB";
      var unitIdx = 1;
      var unit = "B";
      var format = ",.00";
      if (arrayLen(arguments) GT 1) {
        unit = arguments[2];
        unitIdx = listFindNoCase(validUnits, unit);
        if (arrayLen(arguments) GT 2) {
          format = arguments[3];
        }
      }
      if (unitIdx GT 0) {
        while ((sz GTE 1024) AND (unitIdx LT listLen(validUnits))) {
          sz = (sz / 1024);
          unitIdx = unitIdx + 1;
        }
        result = numberFormat(sz, format) & " "
                                 & listGetAt(validUnits, unitIdx);
      }
      else {
        result = "INVALID UNIT";
      }
      return result;
    }
    
  4. | #4

    Thanks Heath. Once we gather enough UDFs I will post a library here on programmers.org so everyone can browse and download functions they may need.

  5. Heath Provost
    | #5

    @Heath Provost

    Actaully, doesnt do bits… Messed up description. Just bytes on up. For example:

    formatSize(5000, “GB”, “_”) = “5 TB” (full rounding, no formatting)
    formatSize(1023, “B”, “,”) = “1,023 B” (full rounding, use commas)
    formatSize(1048576, “GB”) = “1.00 PB” (default - commas and round to 2 decimals)
    formatSize(1E15) = “909.49 TB” (unit defaults to bytes, default format. Can use scientific notation too)

  6. | #6

    @Heath Provost
    Heath is in my BRAIN!!!!

    /*#depends:preciseRound#*/
    function fileSize(size){
    	var units = "kb,mb,gb,tb,pb,eb,zb,yb";
    	var unit = "";
    	var pos = 0;
    	var value = "";
    	var counter = 0;
    	var precision = 2;
    	if (arrayLen(arguments) gte 2)precision = arguments[2];
    	if (arrayLen(arguments) gte 3)unit = arguments[3];
    	if (len(unit)){
    		if (isNumeric(unit))pos = unit;
    		else{
    			if (listFindNoCase(units,unit))pos = listFindNoCase(units,unit);
    			else unit = "";
    		};
    	}
    	if (not pos){
    		while (size/(1024^counter) gt 1)counter = incrementValue(counter);
    		pos = counter-1;
    	}
    	if (pos){
    		size = server.preciseRound(size/(1024^pos),precision);
    		value = "#numberFormat(size,",#iif(findNoCase(".",size),de(".#repeatString("9",precision)#"),de(""))#")# #uCase(listGetAt(units,pos))#";
    	}
    	else value = size;
    	return value;
    }
    
  7. Heath Provost
    | #7

    @Shawn Bailly

    Hah. Yours does exabytes, petabytes, and yottabytes though - Ill have to update my code to match now because that somehow makes me feel small and insignificant :(

  1. No trackbacks yet.