Skip to content

Instantly share code, notes, and snippets.

@ericraio
Created August 16, 2018 19:16
Show Gist options
  • Save ericraio/9d433d28ec744f11fb0652d2f84ce646 to your computer and use it in GitHub Desktop.
Save ericraio/9d433d28ec744f11fb0652d2f84ce646 to your computer and use it in GitHub Desktop.
import _ from 'lodash';
const SPLIT_RE = /(\S+)(\s+|\s*$)/g;
const SPACE = ' ';
const ARTICLES = 'a an the';
const CONJUNCTIONS = 'and but for nor or so yet';
const PREPOSITIONS = 'ago at by for from in into next of off on onto out over past till to';
const EXCEPTIONAL = SPACE + [ARTICLES, CONJUNCTIONS, PREPOSITIONS].join(SPACE) + SPACE;
function camelCase(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
function _titlize(match, word) {
if (!isExceptional(word)) {
word = camelCase(word);
}
return word + SPACE;
}
function isExceptional(s) {
return EXCEPTIONAL.indexOf(SPACE + s + SPACE) >= 0;
}
export default function titlize(s) {
if (s) {
var title = s.replace(SPLIT_RE, _titlize);
return title.slice(0,title.length-1);
} else {
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment