Title Case
Title Case

Script key: Title Case

Install

Details

Label Title Case
Voice Over Title Case
Script
// Title Case
// based on http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript

function toTitleCase(str)
{
  var i, j, str, lowers, uppers;
  str = str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});

// Certain minor words should be left lowercase unless 
  // they are the first or last words in the string
  lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At', 
  'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
  for (i = 0, j = lowers.length; i < j; i++)
    str = str.replace(new RegExp('\\s' + lowers[i] + '\\s', 'g'), 
      function(txt) {
        return txt.toLowerCase();
      });

// Certain words such as initialisms or acronyms should be left uppercase
  uppers = ['Id', 'Tv', 'Ngo'];
  for (i = 0, j = uppers.length; i < j; i++)
    str = str.replace(new RegExp('\\b' + uppers[i] + '\\b', 'g'), 
      uppers[i].toUpperCase());


  return str;
}

var sel = getSelectedText();

if (!sel || sel.length == 0) {
  setText(toTitleCase(getText()));
}
else {
  setSelectedText(toTitleCase(sel));
}

Advanced

Keyboard shortcut None
Hide key Yes

Discussion

comments powered by Disqus