Skip to content

Commit a624224

Browse files
authored
Create CapitalizeTitleWords.js
1 parent 79f51b7 commit a624224

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-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+
}

0 commit comments

Comments
 (0)