Skip to content

Commit c58f202

Browse files
authored
Capitalize Title Words (#2240)
* Create CapitalizeTitleWords.js * Create README.md
1 parent c312504 commit c58f202

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* According to MLA Format, the first and last words will always be capitalized even if normally excluded.
3+
* Otherwise, you can add to the list of excluded words via the "excludedWords" array in the "processTitle" function.
4+
* Set the "title" variable to the text you want to format. Often this will be a Short Description or similar field. Or you can call "processTitle" and pass in the string
5+
* You can use a Business Rule (on insert) to make the modifications and provide more consistent formatting even when you have users who love to exclusively use lower case for everything.
6+
*/
7+
8+
var title = "the ultimate short description of the 21st century";
9+
10+
gs.info(processTitle(title));
11+
12+
function processTitle(title) {
13+
var excludedWords = ["a", "and", "as", "at", "but", "by", "down", "for", "from", "if", "in", "into", "like", "near", "nor", "of", "off", "on", "once", "onto", "or", "over", "past", "so", "than", "that", "the", "to", "upon", "when", "with", "yet"];
14+
15+
var indexList = [0];
16+
var startIndex = -1;
17+
var currentToken = [];
18+
for (var i = 0; i < title.length; i++) {
19+
var c = title[i];
20+
var cNum = c.charCodeAt(0);
21+
if ((cNum >= 65 && cNum <= 90) || (cNum >= 97 && cNum <= 122) || (cNum >= 48 && cNum <= 57) || (cNum == 39)) {
22+
if (currentToken.length == 0)
23+
startIndex = i;
24+
currentToken.push(c);
25+
} else {
26+
if (excludedWords.indexOf(currentToken.join("")) == -1) {
27+
indexList.push(startIndex);
28+
}
29+
currentToken = [];
30+
}
31+
}
32+
33+
indexList.push(startIndex);
34+
35+
var titleArray = [];
36+
for (var i2 = 0; i2 < title.length; i2++) {
37+
if (indexList.indexOf(i2) != -1) {
38+
titleArray.push(title[i2].toUpperCase());
39+
} else {
40+
titleArray.push(title[i2]);
41+
}
42+
}
43+
44+
return titleArray.join("");
45+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
This script will take a string and capitalize words that are not part of an exclusion list.
2+
The first and last words will always be capitalized even if normally excluded, according to MLA Format.
3+
4+
This is great for providing more consistently formatted Titles and Short Descriptions, especially when you have those users who just LOVE to exclusively use lowercase for everything.
5+
6+
You can use this code in a Business Rule (on insert) to make the modifications and provide more consistent formatting where applicable.
7+
8+
You can add to or remove from the list of excluded words via the "excludedWords" array in the "processTitle" function.
9+
10+
Set the "title" variable to the text you want to format. Often this will be a Short Description or similar field. Or you can call the "processTitle" function and pass in the string
11+
12+
*** An example run ***
13+
14+
Before String:
15+
the ultimate short description of the 21st century
16+
17+
After String:
18+
The Ultimate Short Description of the 21st Century

0 commit comments

Comments
 (0)