UNPKG

@bigfishtv/cockpit

Version:

62 lines (51 loc) 1.54 kB
/* * Title Caps * * Ported to JavaScript By John Resig - http://ejohn.org/ - 21 May 2008 * Original by John Gruber - http://daringfireball.net/ - 10 May 2008 * License: http://www.opensource.org/licenses/mit-license.php */ const small = '(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)' const punct = '([!"#$%&\'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)' /** * Takes string and returns a title-cased version of it, e.g. "cats and dogs" -> "Cats and Dogs" * @param {String} title * @return {string} */ export default function titleCase(title) { var parts = [], split = /[:.;?!] |(?: |^)["Ò]/g, index = 0 title = title.toLowerCase() while (true) { var m = split.exec(title) parts.push( title .substring(index, m ? m.index : title.length) .replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all) { return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all) }) .replace(RegExp('\\b' + small + '\\b', 'ig'), lower) .replace(RegExp('^' + punct + small + '\\b', 'ig'), function(all, punct, word) { return punct + upper(word) }) .replace(RegExp('\\b' + small + punct + '$', 'ig'), upper) ) index = split.lastIndex if (m) parts.push(m[0]) else break } return parts .join('') .replace(/ V(s?)\. /gi, ' v$1. ') .replace(/(['Õ])S\b/gi, '$1s') .replace(/\b(AT&T|Q&A)\b/gi, function(all) { return all.toUpperCase() }) } function lower(word) { return word.toLowerCase() } function upper(word) { return word.substr(0, 1).toUpperCase() + word.substr(1) }