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!


10 comments:

  1. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....

    Selenium training in Chennai

    Selenium training in Bangalore

    ReplyDelete

  2. This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb.
    This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here 


    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Really very nice blog information for this one and more technical skills are improving, I like that kind of post.

    eTechno Soft Solutions is a leading training institute for all kind of the Oracle Training in Bangalore with real-time experienced trainers with 100% Placement Assistance.

    ReplyDelete
  5. its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    python training in chennai

    python online training in chennai

    python training in bangalore

    python training in hyderabad

    python online training

    python flask training

    python flask online training

    python training in coimbatore

    ReplyDelete