Nodejs String Title Case titlecase()

Here you can find the source of titlecase()

Method Source Code

String.prototype.titlecase = function () {
  const wordArray = this.split(' ')
  const restrictedWords = ['a', 'the', 'an', 'and']
  const newWordArray = wordArray.map(word => {
    const downcaseWord = word.toLowerCase()
    if(restrictedWords.includes(downcaseWord)) return downcaseWord
    return word.capitalize()
  })//from  ww w.ja v  a 2  s. c om
  newWordArray[0] = newWordArray[0].capitalize()
  return newWordArray.join(' ')
}

Related

  1. titleCase(str)
    function titleCase(str) {
      var arr = str.split(" ");
      var combineArr = [];
      var resultArr = [];
      for(var i = 0; i<arr.length; i++){
        var upperArr = [];
        var lowerArr = [];  
        for(var j = 0; j < arr[i].length; j++){      
          if(j === 0){
    ...
    
  2. titleCase(str)
    function titleCase(str) {
      var arr = str.split(" ");
      var first;
      var ends = [];
      var temp;
      var arrFixed = [];
      for (i = 0; i < arr.length; i++){
        first = [];
        ends = [];
    ...
    
  3. titleCase(str)
    function titleCase(str) {
      var splitted = str.toLowerCase().split(' ');
      for (var i = 0; i < splitted.length; i++) {
        var holder = splitted[i];    
        var bigLetter = holder.charAt(0).toUpperCase();
        holder = holder.slice(1, holder.length);
        splitted[i] = bigLetter.concat(holder);
      str = splitted.join(' ');
    ...
    
  4. titleCase(string)
    function titleCase(str) {
        var newTitle = str.split(' ');
        var updateTitle = [];
        for (var st in newTitle) {
            updateTitle[st] = newTitle[st]
                .toLowerCase()
                .replaceAt(0, newTitle[st].charAt(0).toUpperCase());
        return updateTitle.join(' ');
    ...
    
  5. title_case()
    String.prototype.title_case = function () {
      var words = this.split(' ');
      var word = '';
      if (words.length > 1) {
        for (var i=0; i < words.length; i++) {
          if (words[i] != 'of' && words[i] != 'the' || i === 0){
            word = word.concat( words[i].charAt(0)
            .toUpperCase() + words[i].slice(1) + ' ');
          else {
            word = word.concat( words[i] + ' ');
        return word.trim();
      else {
        return this.charAt(0).toUpperCase() + this.slice(1);
    };
    
  6. titleize()
    String.prototype.titleize = function(){
      if(this.length == 0) return this;
      return this[0].toUpperCase() + this.substr(1).toLowerCase();
    
  7. titleize()
    String.prototype.titleize = function() {
      var words = this.split(' ')
      var array = []
      for (var i=0; i<words.length; ++i) {
        array.push(words[i].charAt(0).toUpperCase() + words[i].toLowerCase().slice(1))
      return array.join(' ')
    
  8. titleize()
    String.prototype.titleize = function() {
      var res = [];
      var parts = this.split(/_| /);
      $.each(parts, function(index, part) {
        res.push(part.capitalize());
      });
      return res.join(" ");
    };
    
  9. titleize()
    String.prototype.titleize = function() {
        var string_array = this.split(/[_-]+/);
        string_array = string_array.map(function(str) {
            return str.capitalize();
        });
        return string_array.join(' ');