-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChicago Title Case.js
60 lines (49 loc) · 1.84 KB
/
Chicago Title Case.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
{
"api":1,
"name":"Title Case (Chicago 15th Edition)",
"description":"Converts a string to title case according to the Chicago Manual of Style (15th edition).",
"author":"UFervor",
"icon":"textformat",
"tags":"title, case, Chicago, text"
}
**/
function titleCase(str) {
const lowerWords = ['and', 'but', 'for', 'or', 'nor', 'the', 'a', 'an', 'to', 'as'];
const prepositions = [
'about', 'above', 'across', 'after', 'against', 'along', 'among', 'around', 'at',
'before', 'behind', 'below', 'beneath', 'beside', 'between', 'beyond', 'by',
'down', 'during', 'except', 'for', 'from', 'in', 'inside', 'into', 'like', 'near',
'of', 'off', 'on', 'onto', 'out', 'outside', 'over', 'past', 'since', 'through',
'throughout', 'to', 'under', 'until', 'up', 'upon', 'with', 'within', 'without'
];
const words = str.split(' ');
return words.map((word, index, arr) => {
if (index === 0 || index === arr.length - 1) {
return capitalize(word);
}
if (lowerWords.includes(word.toLowerCase())) {
return word.toLowerCase();
}
if (prepositions.includes(word.toLowerCase()) && !isStressedPreposition(word, index, arr)) {
return word.toLowerCase();
}
if (word.includes('-')) {
return handleHyphenatedWord(word);
}
return capitalize(word);
}).join(' ');
}
function isStressedPreposition(word, index, arr) {
return index > 0 && arr[index - 1].toLowerCase() === 'from';
}
function handleHyphenatedWord(word) {
const [first, second] = word.split('-');
return `${capitalize(first)}-${second.toLowerCase()}`;
}
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
function main(input) {
input.text = titleCase(input.text);
}