Nodejs String Title Case titleize()

Here you can find the source of titleize()

Method Source Code

String.prototype.titleize = function() {
  var res = [];//from   w w  w .  j  av a 2  s.  com
  var parts = this.split(/_| /);
  $.each(parts, function(index, part) {
    res.push(part.capitalize());
  });
  return res.join(" ");
};

Related

  1. 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(' ');
    ...
    
  2. 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);
    };
    
  3. titlecase()
    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()
      })
      newWordArray[0] = newWordArray[0].capitalize()
    ...
    
  4. titleize()
    String.prototype.titleize = function(){
      if(this.length == 0) return this;
      return this[0].toUpperCase() + this.substr(1).toLowerCase();
    
  5. 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(' ')
    
  6. titleize()
    String.prototype.titleize = function() {
        var string_array = this.split(/[_-]+/);
        string_array = string_array.map(function(str) {
            return str.capitalize();
        });
        return string_array.join(' ');
    
  7. titleize()
    String.prototype.titleize = function() {
      var out = this;
      out = out.replace(/^\s*/, "");  
      out = out.replace(/_/g, ' ');
      out = out.replace(/^[a-z]|[^\s][A-Z]/g, function(str, offset) {
        if (offset === 0) {
          return(str.toUpperCase());
        } else {
          return(str.substr(0,1) + " " + str.substr(1).toUpperCase());
    ...
    
  8. titleize()
    String.prototype.titleize = function () {
      return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1);});
    };
    
  9. titleize()
    String.prototype.titleize = function() {
      strings = this.split(' ')
      string  = ''
      for (var i = 0, len = strings.length; i < len; ++i) {
        string += strings[i].charAt(0).toUpperCase() + strings[i].toLowerCase().slice(1)
        if (i != (len-1)) { string += ' ' }
      return string
    function switchPage(page) {
        var links = ['home', 'add', 'import'];
        for (var i=0;i<links.length;i++) {
            var link = document.getElementById(links[i] + '-bar');
            link.className = "";
            if (links[i] == page)
                link.className = "active";