Nodejs String Title Case toTitleCase()

Here you can find the source of toTitleCase()

Method Source Code

'use strict';/*from   w w w.  j a  va 2s  . co  m*/

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(' ');
};

Related

  1. toTitleCase( selector )
    String.prototype.toTitleCase = function( selector ) {
        var splitArray;
        var finalString = "";
        if ( typeof ( selector ) === 'undefined' ) {
            selector = " ";
            splitArray = this.split ( " " ) ;
        } else {
            splitArray = this.split ( selector ) ;
        splitArray.forEach( function( element ) {
            finalString += element[0].toUpperCase() + element.substr(1) + selector;
        } );
        finalString = finalString.substr( 0, finalString.length-1);
        return finalString;
    
  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 () {
        return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
    };
    
  4. 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();
    };
    ...
    
  5. toTitleCase()
    String.prototype.toTitleCase = function() {
      var str = this;
      return str.toLowerCase().split(' ').map(function(word) {
        return (word.charAt(0).toUpperCase() + word.slice(1));
      }).join(' ');
    
  6. toTitleCase()
    String.prototype.toTitleCase = function() {
      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'];
      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(); });
    ...
    
  7. 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();}
        );
    };
    
  8. toTitleCase()
    String.prototype.toTitleCase = function () {
      return this[0].toUpperCase() + this.substring(1).toLowerCase();
    };
    
  9. 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;
    };
    ...