UNPKG

859 BJavaScriptView Raw
1/**
2 * Convert string into sentence case.
3 * First letter capped and each punctuations is capitalcase and joined with space.
4 * @memberof module:stringcase/lib
5 * @function titlecase
6 * @param {string} str - String to convert.
7 * @returns {string} Title cased string.
8 */
9
10'use strict'
11
12const snakecase = require('./snakecase')
13const lowercase = require('./lowercase')
14const trimcase = require('./trimcase')
15const capitalcase = require('./capitalcase')
16
17const LOWERCASE_WORDS = 'a,the,and,or,not,but,for,of'.split(',')
18
19/** @lends titlecase*/
20function titlecase (str) {
21 return snakecase(str).split(/_/g)
22 .map(trimcase)
23 .map(function (word) {
24 var lower = !!~LOWERCASE_WORDS.indexOf(word)
25 if (lower) {
26 return lowercase(word)
27 } else {
28 return capitalcase(word)
29 }
30 }).join(' ')
31}
32
33module.exports = titlecase;
34
35