@danielhaim/titlecaser 1.2.36 → 1.2.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/TitleCaser.js +21 -21
package/package.json
CHANGED
package/src/TitleCaser.js
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
}
|
|
7
7
|
from "./TitleCaserConsts.js";
|
|
8
8
|
|
|
9
|
-
import
|
|
9
|
+
import TitleCaserUtils from "./TitleCaserUtils.js";
|
|
10
10
|
|
|
11
11
|
export class TitleCaser {
|
|
12
12
|
constructor (options = {}) {
|
|
@@ -36,7 +36,7 @@ export class TitleCaser {
|
|
|
36
36
|
shortPrepositionsList,
|
|
37
37
|
neverCapitalizedList,
|
|
38
38
|
replaceTerms
|
|
39
|
-
} =
|
|
39
|
+
} = TitleCaserUtils.getTitleCaseOptions(this.options, commonAbbreviationList, wordReplacementsList);
|
|
40
40
|
|
|
41
41
|
// Prerocess the replaceTerms array to make it easier to search for.
|
|
42
42
|
const replaceTermsArray = replaceTermList.map(term => Object.keys(term)[0].toLowerCase());
|
|
@@ -67,54 +67,54 @@ export class TitleCaser {
|
|
|
67
67
|
|
|
68
68
|
const wordsInTitleCase = words.map((word, i) => {
|
|
69
69
|
switch (true) {
|
|
70
|
-
case
|
|
70
|
+
case TitleCaserUtils.isWordAmpersand(word):
|
|
71
71
|
// if the word is an ampersand, return it as is.
|
|
72
72
|
return word;
|
|
73
|
-
case
|
|
73
|
+
case TitleCaserUtils.hasHtmlBreak(word):
|
|
74
74
|
// If the word is a <br> tag, return it as is.
|
|
75
75
|
return word;
|
|
76
|
-
case
|
|
76
|
+
case TitleCaserUtils.isWordIgnored(word, ignoreList):
|
|
77
77
|
// If the word is in the ignore list, return it as is.
|
|
78
78
|
return word;
|
|
79
79
|
case replaceTermsArray.includes(word.toLowerCase()):
|
|
80
80
|
// If the word is in the replaceTerms array, return the replacement.
|
|
81
81
|
return replaceTermObj[word.toLowerCase()];
|
|
82
|
-
case
|
|
82
|
+
case TitleCaserUtils.isWordInArray(word, correctTitleCasingList):
|
|
83
83
|
// If the word is in the correctTitleCasingList array, return the correct casing.
|
|
84
|
-
return
|
|
85
|
-
case
|
|
84
|
+
return TitleCaserUtils.correctTerm(word, correctTitleCasingList);
|
|
85
|
+
case TitleCaserUtils.hasSuffix(word, style):
|
|
86
86
|
// If the word has a suffix, return the correct casing.
|
|
87
|
-
return
|
|
88
|
-
case
|
|
87
|
+
return TitleCaserUtils.correctSuffix(word, correctTitleCasingList);
|
|
88
|
+
case TitleCaserUtils.hasHyphen(word):
|
|
89
89
|
// If the word has a hyphen, return the correct casing.
|
|
90
|
-
return
|
|
91
|
-
case
|
|
90
|
+
return TitleCaserUtils.correctTermHyphenated(word, style);
|
|
91
|
+
case TitleCaserUtils.hasUppercaseIntentional(word):
|
|
92
92
|
// If the word has an intentional uppercase letter, return the correct casing.
|
|
93
93
|
return word;
|
|
94
|
-
case
|
|
94
|
+
case TitleCaserUtils.isShortWord(word, style) && i !== 0:
|
|
95
95
|
// If the word is a short word, return the correct casing.
|
|
96
|
-
return (i > 0 &&
|
|
96
|
+
return (i > 0 && TitleCaserUtils.endsWithSymbol(words[i - 1],
|
|
97
97
|
[':', '?', '!', '.'])) ? word.charAt(0).toUpperCase() + word.slice(1) : word.toLowerCase();
|
|
98
|
-
case
|
|
98
|
+
case TitleCaserUtils.endsWithSymbol(word):
|
|
99
99
|
// If the word ends with a symbol, return the correct casing.
|
|
100
100
|
const splitWord = word.split(/([.,\/#!$%\^&\*;:{}=\-_`~()])/g);
|
|
101
101
|
const processedWords = splitWord.map((splitWord, j) => {
|
|
102
102
|
// If the word is in the correctTitleCasingList array, return the correct casing.
|
|
103
|
-
if (
|
|
103
|
+
if (TitleCaserUtils.isWordInArray(splitWord, correctTitleCasingList)) return TitleCaserUtils.correctTerm(splitWord, correctTitleCasingList);
|
|
104
104
|
// Else return the word with the correct casing.
|
|
105
|
-
else return (j > 0 &&
|
|
105
|
+
else return (j > 0 && TitleCaserUtils.endsWithSymbol(splitWord)) ? splitWord.charAt(0)
|
|
106
106
|
.toUpperCase() + splitWord.slice(1) : splitWord.charAt(0)
|
|
107
107
|
.toUpperCase() + splitWord.slice(1);
|
|
108
108
|
});
|
|
109
109
|
// Join the processed words and return them.
|
|
110
110
|
return processedWords.join("");
|
|
111
|
-
case
|
|
111
|
+
case TitleCaserUtils.startsWithSymbol(word):
|
|
112
112
|
// If the word starts with a symbol, return the correct casing.
|
|
113
|
-
return !
|
|
114
|
-
case
|
|
113
|
+
return !TitleCaserUtils.isWordInArray(word, correctTitleCasingList) ? word : TitleCaserUtils.correctTerm(word);
|
|
114
|
+
case TitleCaserUtils.hasRomanNumeral(word):
|
|
115
115
|
// If the word has a roman numeral, return the correct casing.
|
|
116
116
|
return word.toUpperCase();
|
|
117
|
-
case
|
|
117
|
+
case TitleCaserUtils.hasNumbers(word):
|
|
118
118
|
// If the word has numbers, return the correct casing.
|
|
119
119
|
return word;
|
|
120
120
|
default:
|