Nodejs String Title Case toTitleCase()

Here you can find the source of toTitleCase()

Method Source Code

/*/*from w  w w .  java 2s.com*/
 * To Title Case 2.0.1 ? http://individed.com/code/to-title-case/
 * Copyright ? 2008?2012 David Gouch. Licensed under the MIT License.
 *
 * usage: String.toTitleCase() method on the string you want converted.
 */

String.prototype.toTitleCase = function () {
    var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i;

    return this.replace(/([^\W_]+[^\s-]*) */g, function (match, p1, index, title) {
        if (index > 0 && index + p1.length !== title.length &&
            p1.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
            title.charAt(index - 1).search(/[^\s-]/) < 0) {
            return match.toLowerCase();
        }

        if (p1.substr(1).search(/[A-Z]|\../) > -1) {
            return match;
        }

        return match.charAt(0).toUpperCase() + match.substr(1);
    });
};

Related

  1. toTitleCase()
    var smallword=["bin","binti","to","a","for","in"];
    function is_in_small(text){
      var i=0;
      while(i<smallword.length){
        if(smallword[i]==text.toLowerCase()){
          return true;
        i=i+1;
      return false;
    String.prototype.toTitleCase = function () {
        return this.replace(/\w\S*/g, function(txt){
          if(is_in_small(txt)){
      return txt.toLowerCase();
          return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}
        );
    };
    
  2. toTitleCase()
    String.prototype.toTitleCase = function () {
      return this[0].toUpperCase() + this.substring(1).toLowerCase();
    };
    
  3. toTitleCase()
    String.prototype.toTitleCase = function() {
      var words = this.split(' ');
      var compiledString = '';
      for (var i = 0; i < words.length; i++) {
        compiledString += (' ' + words[i].capitalize());
      compiledString = compiledString.slice(1);
      return compiledString;
    };
    ...
    
  4. toTitleCase()
    String.prototype.toTitleCase = function(){
      var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
      return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
        if (index > 0 && index + match.length !== title.length &&
          match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
          (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
          title.charAt(index - 1).search(/[^\s-]/) < 0) {
          return match.toLowerCase();
        if (match.substr(1).search(/[A-Z]|\../) > -1) {
          return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
      });
    };
    
  5. toTitleCase()
    String.prototype.toTitleCase = function () {
      var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|vs?\.?|via)$/i;
      return this.replace(/([^\W_]+[^\s-]*) */g, function (match, p1, index, title) {
        if (index > 0 && index + p1.length !== title.length &&
          p1.search(smallWords) > -1 && title.charAt(index - 2) !== ":" && 
          title.charAt(index - 1).search(/[^\s-]/) < 0) {
          return match.toLowerCase();
        if (p1.substr(1).search(/[A-Z]|\../) > -1) {
    ...
    
  6. toTitleCase()
    String.prototype.toTitleCase = function () {
        return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
    };
    
  7. toTitleCase()
    String.prototype.toTitleCase = function(){
      var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
      return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
        if (index > 0 && index + match.length !== title.length &&
          match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
          (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
          title.charAt(index - 1).search(/[^\s-]/) < 0) {
          return match.toLowerCase();
        if (match.substr(1).search(/[A-Z]|\../) > -1) {
          return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
      });
    };
    
  8. toTitleCase()
    String.prototype.toTitleCase = function () {
      if (!this) throw new Error('Null refference extension.');
      if (!this.length) return this;
      var words = this.split(' ');
      if (words.length === 1) 
        return this[0].toUpperCase() + this.substring(1).toLowerCase();
      for (var i = 0; i < words.length; i++) {
        words[i] = words[i].toTitleCase();
      };
    ...
    
  9. toTitleCase()
    String.prototype.toTitleCase = function(){
      var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
      return this.toLowerCase().replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
        if (index > 0 && index + match.length !== title.length &&
          match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
          (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
          title.charAt(index - 1).search(/[^\s-]/) < 0) {
          return match.toLowerCase();
        if (match.substr(1).search(/[A-Z]|\../) > -1) {
          return match;
        return match.charAt(0).toUpperCase() + match.substr(1);
      });
    };