Created
August 16, 2018 18:57
-
-
Save ericraio/4d0c246b0e55070b4288ca95b7304ca8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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