Nodejs String Title Case toTitleCase()

Here you can find the source of toTitleCase()

Method Source Code

String.prototype.toTitleCase = function() {

  // Certain minor words should be left lowercase unless
  // they are the first or last words in the string
  var lowers = ['a', 'an', 'and', 'as', 'at', 'but', 'by', 'for', 'from', 'if',
            'in', 'into', 'near', 'nor', 'of', 'on', 'onto', 'or', 'that',
            'the', 'to', 'with', 'via', 'vs', 'vs.', 'per',
            'um', 'uma', 'e', 'como', 'em', 'no', 'na', 'mas', 'por',
            'para', 'pelo', 'pela', 'de', 'do', 'da', 'se', 'perto', 'nem',
            'ou', 'que', 'o', 'a', 'com'];

  // Certain words such as initialisms or acronyms should be left uppercase
  var uppers = ["ID", "CEO", "CEOs", "CFO", "CFOs", "CNC", "COO", "COOs", "CPU", "HVAC", "GDP", "GINI", "IDHM", "R&D", "P&D", "PIB", "IT", "TI", "TV", "UI"];
  var smalls = uppers.map(function(u){ return u.toLowerCase(); });

  var str = this.replace(/([^\s:\-:\/:\(])([^\s:\-:\/:\(]*)/g, function(txt) {
    var low = txt.toLowerCase();
    if (lowers.indexOf(low) >= 0) return low
    else if (smalls.indexOf(low) >= 0) return uppers[smalls.indexOf(low)];
    else return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
  });/*from ww w.  j a  va2  s  .  c  o m*/

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

Related

  1. toTitleCase()
    String.prototype.toTitleCase = function(){
      return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
    
  2. toTitleCase()
    String.prototype.toTitleCase = function () {
        return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
    };
    
  3. toTitleCase()
    String.prototype.toTitleCase = function () {
      var str = this;
      var newStr = '';
      var words = str.split(' ');
      for (var i = 0; i < words.length; i++) {
        newStr += ' ' + words[i].slice(0, 1).toUpperCase() + words[i].slice(1).toLowerCase();
      return newStr.trim();
    };
    ...
    
  4. toTitleCase()
    String.prototype.toTitleCase = function() {
      var str = this;
      return str.toLowerCase().split(' ').map(function(word) {
        return (word.charAt(0).toUpperCase() + word.slice(1));
      }).join(' ');
    
  5. toTitleCase()
    'use strict';
    var _ = require('underscore');
    String.prototype.toTitleCase = function() {
      var exceptions = ['a', 'an', 'the', 'at', 'by', 'for', 'in', 'of', 'on', 'to', 'up', 'and', 'as', 'but', 'or', 'nor'];
      return _.map(this.split(' '), function(word) {
        return _.find(exceptions, function(compare) {
          return compare === word.toLowerCase();
        }) ? word : word.charAt(0).toUpperCase() + word.slice(1);
      }).join(' ');
    ...
    
  6. 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();}
        );
    };
    
  7. toTitleCase()
    String.prototype.toTitleCase = function () {
      return this[0].toUpperCase() + this.substring(1).toLowerCase();
    };
    
  8. 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;
    };
    ...
    
  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.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);
      });
    };