Now Available in YourJS

At times you may need to format a string so that it is more appealing. One function that does this is String#toProperCase() because it actually capitalizes the first character of each word. Even though I already had a simple version of this defined in jPaq v1 I thought it would be nice to add the option to lower-case the remaining characters. Here is the updated version of the function definition:

String.prototype.toProperCase = function(opt_lowerCaseTheRest) {
  return (opt_lowerCaseTheRest ? this.toLowerCase() : this)
    .replace(/(^|[\s\xA0])[^\s\xA0]/g, function(s){ return s.toUpperCase(); });

Examples of using this function are as follows:

var msg = "Where in the WORLD is Carmen Sandiego?";
alert(msg.toProperCase());  // Where In The WORLD Is Carmen Sandiego?
alert(msg.toProperCase(true));  // Where In The World Is Carmen Sandiego?

At this point this change is scheduled to appear in the next version of jPaq but seeing as how I still don’t know when I will have time to release it I figured it would be a good idea to publicly release it here for everyone to use. Have fun. 😎

Leave a Reply

Your email address will not be published. Required fields are marked *