Wednesday, January 26, 2011

Coldbox Plugin - FormatUtils - useful UDF's by me and others (some inspired by RoR)

Over the years, I have used and created many UDF's in my applications. So when I decided to start using ColdBox, I wanted to start grouping them into plugins. One of the first ones, is my FormatUtils Plugin that has several functions that allow you to work with strings. Below is a list of functions included, credits and what they return.

Before we begin let's assume we assigned the Plugin to rc.FormatUtils in our handler as follows:

  • brFormat(theString)
    Converts every carriage return and/or line feed into a compliant line break <br />

    Will return:
    Hello World<br />How Are You?
  • pFormat(theString)
    Creates compliant paragraphs and line breaks from carriage returns and/or line feeds, I created this because ColdFusion's built-in paragraphFormat() does not close out paragraphs.

    Will return:
    <p>Hello World</p></p>How Are You?<br />I am good</p>
  • formatPhone(phone,mask)
    Original Author: Derrick Rapley
    This I grabbed from and it has come pretty handy. Basically you can pass a string and a mask and it will return it back in the format you requested.

    Will return:
    Line 1: (305) 555-1234
    Line 2: 3055551234
  • abbreviate(string,len,append)
    Original Author: Gyrus
    Also from and it has come pretty handy. Basically you can pass a string a length and it will truncated to your specified lenght without breaking a word.

    Will return:
    Line 1: A string like this can go ...
    Line 2: A string like this can go fo
  • urlCrypt(mode,string,closer,key)
    This one I actually have to thank my local CFUG for. In one of our little quizes we use to do before the meeting, a question was asked how to encrypt a string that can be safely passed as a URL and the answer was 2 undocummented functions cfusion_encrypt and cfusion_decrypt. I also saw another way using a mixture of toBase64() and toBinary() in case these functions would no longer work (the alternate method is commented out).

    Will return:
    Line 1: 1911204E0D130415540F001C6B081C4D10364E241B081E17081D0224453E02142014CB3
    Line 2: the name for me is Giancarlo Gomez
  • pluralize() - singularize() and $singularizeOrPluralize
    The credit to this one goes to 2 other frameworks. The first is of course Ruby on Rails. I went to a presentation to learn more about this language and I fell in love. A simple function called pluralize was introduced to me and I though it was great. Basically pass it a word and a number and get back the plural version of it. So at first I wanted to create it from scratch but I decided to do a little bith of research and found out CF Wheels already had almost the equivalent, so I started from there. The only thing I added which they did not have is for an alternate plural or singular word if one was not found in the predefined rules.

    Will return:
    20 houses

To close out you should know that it is not necessary to have Coldbox to use this, simply use the CFC as any other and you can take advantage of all these functions.

I created a new git repo where I will begin to add all my code to share. Click on the following link to see this CFC on gitHub:
FormatUtils.cfc on gitHub

No comments:

Post a Comment