Skip to content

Instantly share code, notes, and snippets.

@ericraio
Created August 16, 2018 18:57
Show Gist options
  • Save ericraio/4d0c246b0e55070b4288ca95b7304ca8 to your computer and use it in GitHub Desktop.
Save ericraio/4d0c246b0e55070b4288ca95b7304ca8 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 _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) {
var title = s.replace(SPLIT_RE, _titlize);
return title.slice(0,title.length-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment