Tuesday, April 27, 2010

Javascript Functions for Prototypers

I have been working on a a web-based project for sometime now. Because of the diverse requirements on the system, I was required to have as many interactions with the users' data from a client-side point of view. I could not find inbuilt functions that could solve my problems. So I was compelled to extend the String object so as to suit my needs.

I should admit that they may not be all that robust to take pride in, but they are able to do what I wanted any way! I believe that there exists someone like me, looking for same (if not similar) functions and probably they are tired with googling. There might also be some who are working on some standardized library and may have been thinking of functions likes these. I guess this will be a starting point for both.

I have tried to make them very simple (simple in all senses) and straightforward, but if you have questions, keep them flowing. We are here to help each other. Constructive criticisms and suggestions are surely welcome!!

Programming hint: In each of these functions, You can improve the split functions to make comparisons using regular expression matching

  1. contains() function
  2. /* checks for the presence of a substring in a given string of
    * semi-colon separated substrings.
    * it returns 'true' if found, otherwise it returns 'false'
    *
    * for example :
    * 1. ["programming;in;javascript;is;cool"].contains("javascript") => true
    * 2. ["programming;in;javascript;is;cool"].contains("java") => false
    *
    * TO DO: ADD HANDLING OF 'SPACE' SEPARATED SUBSTRINGS
    *
    */
    String.prototype.contains = function (substring) {

    var array_of_strings = this.split(';');

    if (jQuery.inArray(substring, array_of_strings)>= 0) {
    return true;
    }
    else {
    return false;
    }
    }
  3. capitalize() function
  4. /* capitalizes a given string
    * Author: Edmond Kachale
    * for example :
    * "ProGraMming Is CooL".capitalize() => "Programming is cool"
    */
    String
    .prototype.capitalize = function(){
    var capitalized_string = new Array();

    if((this.length> 0)){
    capitalized_string.push(this[0].toUpperCase());
    capitalized_string.push(this.substring(1,this.length).toLowerCase());

    return
    capitalized_string.join("");
    }
    else{
    return
    this;
    }
    }

  5. titleize() function

  6. This function depends on capitalize() function above

    /* "titleizes" a given string
    * Author: Edmond Kachale
    * for example :
    * "Programming is cool".titleize() => "Programming Is Cool"
    */

    String.prototype.titleize = function(){
    var
    titleized_string = new Array();
    var
    sub_strings = this.split(" ");

    for(i = 0; i <this.length; i++)
    titleized_string.push(sub_strings[i].capitalize());

    return titleized_string.join(" ");
    }

Enjoy your programming!

NB: Forgive me for poor formatting. I didn't have enough time to create a custom css file!