@danielhaim/titlecaser 1.2.48 → 1.2.53
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/README.md +35 -6
- package/package.json +1 -1
- package/src/TitleCaser.js +4 -1
- package/src/TitleCaserConsts.js +39 -2
package/README.md
CHANGED
|
@@ -13,7 +13,9 @@ Transform any text to proper title case format using popular style guides such a
|
|
|
13
13
|
## Table of Contents
|
|
14
14
|
|
|
15
15
|
- [TitleCaser](#titlecaser)
|
|
16
|
-
* [Demo](
|
|
16
|
+
* [Demo](https://danielhaim1.github.io/TitleCaser/)
|
|
17
|
+
* [CodePen Demo 1](https://codepen.io/danielhaim/pen/oNQgjBv)
|
|
18
|
+
* [CodePen Demo 2](https://codepen.io/danielhaim/pen/oNPGzKw)
|
|
17
19
|
* [Table of Contents](#table-of-contents)
|
|
18
20
|
* [Introduction](#introduction)
|
|
19
21
|
* [Key Features:](#key-features)
|
|
@@ -65,7 +67,7 @@ $ npm i @danielhaim/titlecaser
|
|
|
65
67
|
The package can be imported and used in both Node.js and browser environments using the following syntax:
|
|
66
68
|
|
|
67
69
|
```js
|
|
68
|
-
import
|
|
70
|
+
import "./path/to/@danielhaim/titlecaser";
|
|
69
71
|
|
|
70
72
|
const options = {
|
|
71
73
|
style: 'chicago'
|
|
@@ -101,6 +103,33 @@ const output = input.toTitleCase(options);
|
|
|
101
103
|
console.log(output); // The Future of DevOps: The Next Era
|
|
102
104
|
```
|
|
103
105
|
|
|
106
|
+
### Example 2
|
|
107
|
+
|
|
108
|
+
```html
|
|
109
|
+
<h2>nodejs development on aws: an in-depth tutorial on server-side javascript deployment</h2>
|
|
110
|
+
<h2>the iphone's impact on modern communication: a sociolinguistic analysis</h2>
|
|
111
|
+
<h2>back-end and front-end</h2>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
```js
|
|
115
|
+
function applyTitleCaseToH2Elements(options = { style: "apa" }) {
|
|
116
|
+
try {
|
|
117
|
+
const h2Elements = document.querySelectorAll("h2");
|
|
118
|
+
h2Elements.forEach((h2) => {
|
|
119
|
+
const innerHTML = h2.innerHTML;
|
|
120
|
+
const modifiedContent = innerHTML.toTitleCase(options);
|
|
121
|
+
h2.innerHTML = modifiedContent;
|
|
122
|
+
});
|
|
123
|
+
} catch (error) {
|
|
124
|
+
console.error(
|
|
125
|
+
"An error occurred while applying title case transformation:",
|
|
126
|
+
error
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
applyTitleCaseToH2Elements();
|
|
132
|
+
```
|
|
104
133
|
## Options
|
|
105
134
|
|
|
106
135
|
The `{options}` parameter is an object that contains the settings for the conversion process.
|
|
@@ -128,7 +157,7 @@ The example below demonstrates how to use the TitleCaser class to convert a stri
|
|
|
128
157
|
In the example below, we create a new instance of the `TitleCaser` class with the `APA` style option. We then set multiple replacement terms using two separate calls to the `setReplaceTerms()` method. Descriptive variable names are used for the input string and expected output. We call `toTitleCase()` to convert the input string to a title case.
|
|
129
158
|
|
|
130
159
|
```js
|
|
131
|
-
import
|
|
160
|
+
import "./path/to/@danielhaim/titlecaser";
|
|
132
161
|
|
|
133
162
|
const titleCaser = new TitleCaser({
|
|
134
163
|
style: 'apa'
|
|
@@ -156,7 +185,7 @@ const outputString = titleCaser.toTitleCase(inputString);
|
|
|
156
185
|
The example below demonstrates how to use the TitleCaser class to convert a string to a title case with specific settings.
|
|
157
186
|
|
|
158
187
|
```js
|
|
159
|
-
import
|
|
188
|
+
import "./path/to/@danielhaim/titlecaser";
|
|
160
189
|
|
|
161
190
|
// Set the options object
|
|
162
191
|
const options = {
|
|
@@ -186,7 +215,7 @@ const actualOutput = titleCaser.toTitleCase(input);
|
|
|
186
215
|
The example below demonstrates how to use the TitleCaser class to convert a string to a title case with AP style formatting, including hyphenated words and word/brand replacement.
|
|
187
216
|
|
|
188
217
|
```js
|
|
189
|
-
import
|
|
218
|
+
import "./path/to/@danielhaim/titlecaser";
|
|
190
219
|
|
|
191
220
|
// Instantiate a new TitleCaser object with AP style formatting
|
|
192
221
|
const titleCaser = new TitleCaser({ style: 'ap' });
|
|
@@ -206,7 +235,7 @@ const actualOutput = titleCaser.toTitleCase(input);
|
|
|
206
235
|
The example below demonstrates how to use the TitleCaser class to convert a string to title case with AP style formatting, including a possessive noun and a colon.
|
|
207
236
|
|
|
208
237
|
```js
|
|
209
|
-
import
|
|
238
|
+
import "./path/to/@danielhaim/titlecaser";
|
|
210
239
|
|
|
211
240
|
// Instantiate a new TitleCaser object with AP style formatting
|
|
212
241
|
const titleCaser = new TitleCaser({ style: "ap" });
|
package/package.json
CHANGED
package/src/TitleCaser.js
CHANGED
|
@@ -122,7 +122,7 @@ export class TitleCaser {
|
|
|
122
122
|
case TitleCaserUtils.hasNumbers ( word ):
|
|
123
123
|
// If the word has numbers, return the correct casing.
|
|
124
124
|
return word;
|
|
125
|
-
|
|
125
|
+
default:
|
|
126
126
|
// Default to returning the word with the correct casing.
|
|
127
127
|
return word.charAt ( 0 )
|
|
128
128
|
.toUpperCase () + word.slice ( 1 )
|
|
@@ -144,6 +144,9 @@ export class TitleCaser {
|
|
|
144
144
|
// Replace the nl2br placeholder with <br> tags.
|
|
145
145
|
inputString = inputString.replace(/nl2br/gi, "<br />");
|
|
146
146
|
|
|
147
|
+
// BEFORE WE RETURN THE STRING
|
|
148
|
+
// CHECK THE LAST WORD AND IF IT IS INTENTIONALLY UPPERCASED, IF IT IS, RETURN THE STRING.
|
|
149
|
+
|
|
147
150
|
// Return the string.
|
|
148
151
|
return inputString;
|
|
149
152
|
} catch ( error ) {
|
package/src/TitleCaserConsts.js
CHANGED
|
@@ -9,7 +9,40 @@ export const correctTitleCasingList = [
|
|
|
9
9
|
'MobX', 'SCSS', 'TypeScript', 'Vue.js', '.NET', 'ASP', 'ASPX',
|
|
10
10
|
'MySQL', 'PHP', 'PostgresQL', 'Python', 'SQL', 'GraphQL',
|
|
11
11
|
'HTML5',
|
|
12
|
-
|
|
12
|
+
|
|
13
|
+
// Countries
|
|
14
|
+
'Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola', 'Antigua and Barbuda',
|
|
15
|
+
'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'Bahamas',
|
|
16
|
+
'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin',
|
|
17
|
+
'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Brazil', 'Brunei',
|
|
18
|
+
'Bulgaria', 'Burkina Faso', 'Burundi', 'Cabo Verde', 'Cambodia', 'Cameroon',
|
|
19
|
+
'Canada', 'Central African Republic', 'Chad', 'Chile', 'China', 'Colombia',
|
|
20
|
+
'Comoros', 'Congo', 'Costa Rica', 'Cote d\'Ivoire', 'Croatia', 'Cuba',
|
|
21
|
+
'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 'Dominica',
|
|
22
|
+
'Dominican Republic', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea',
|
|
23
|
+
'Eritrea', 'Estonia', 'Eswatini', 'Ethiopia', 'Fiji', 'Finland', 'France',
|
|
24
|
+
'Gabon', 'Gambia', 'Georgia', 'Germany', 'Ghana', 'Greece', 'Grenada',
|
|
25
|
+
'Guatemala', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Honduras', 'Hungary',
|
|
26
|
+
'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Ireland', 'Israel', 'Italy',
|
|
27
|
+
'Jamaica', 'Japan', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'Korea',
|
|
28
|
+
'Kosovo', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia', 'Lebanon', 'Lesotho',
|
|
29
|
+
'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Madagascar',
|
|
30
|
+
'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands',
|
|
31
|
+
'Mauritania', 'Mauritius', 'Mexico', 'Micronesia', 'Moldova', 'Monaco',
|
|
32
|
+
'Mongolia', 'Montenegro', 'Morocco', 'Mozambique', 'Myanmar', 'Namibia',
|
|
33
|
+
'Nauru', 'Nepal', 'Netherlands', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria',
|
|
34
|
+
'North Macedonia', 'Norway', 'Oman', 'Pakistan', 'Palau', 'Panama', 'Papua New Guinea',
|
|
35
|
+
'Paraguay', 'Peru', 'Philippines', 'Poland', 'Portugal', 'Qatar', 'Romania',
|
|
36
|
+
'Russia', 'Rwanda', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Vincent and the Grenadines',
|
|
37
|
+
'Samoa', 'San Marino', 'Sao Tome and Principe', 'Saudi Arabia', 'Senegal', 'Serbia',
|
|
38
|
+
'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Solomon Islands',
|
|
39
|
+
'Somalia', 'South Africa', 'South Korea', 'South Sudan', 'Spain', 'Sri Lanka', 'Sudan',
|
|
40
|
+
'Suriname', 'Sweden', 'Switzerland', 'Syria', 'Taiwan', 'Tajikistan', 'Tanzania',
|
|
41
|
+
'Thailand', 'Timor-Leste', 'Togo', 'Tonga', 'Trinidad and Tobago', 'Tunisia',
|
|
42
|
+
'Turkey', 'Turkmenistan', 'Tuvalu', 'Uganda', 'Ukraine', 'United Arab Emirates',
|
|
43
|
+
'United Kingdom', 'United States', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Vatican City',
|
|
44
|
+
'Venezuela', 'Vietnam', 'Yemen', 'Zambia', 'Zimbabwe',
|
|
45
|
+
|
|
13
46
|
// Acronyms/Abbreviations
|
|
14
47
|
'API', 'APIs', 'ASCII', 'CI', 'CircleCI', 'CLI', 'DLL', 'DNS',
|
|
15
48
|
'EC2', 'FTP', 'HTTP', 'HTTPs', 'ICMP', 'IDE', 'IP', 'ISP',
|
|
@@ -78,7 +111,7 @@ export const correctTitleCasingList = [
|
|
|
78
111
|
'ProTools', 'QuickTime', 'AdWords', 'AdSense', 'TikTok', 'Slack', 'Trello',
|
|
79
112
|
'Zoom', 'Twitch', 'Snapchat', 'WhatsApp', 'Telegram', 'Discord', 'Reddit',
|
|
80
113
|
'Quora', 'StackOverflow', 'StackExchange', 'Coca-Cola',
|
|
81
|
-
'AWS', 'GCP', 'VMware', 'CVS', 'ESL', 'EE', 'CW', 'EE',
|
|
114
|
+
'AWS', 'GCP', 'VMware', 'CVS', 'ESL', 'EE', 'CW', 'EE', 'ITV',
|
|
82
115
|
|
|
83
116
|
// Sports
|
|
84
117
|
'NBA', 'NCAA', 'NFL', 'WWE', 'WWF', 'FIFA',
|
|
@@ -100,6 +133,9 @@ export const correctTitleCasingList = [
|
|
|
100
133
|
|
|
101
134
|
// Commercial
|
|
102
135
|
'Ltd.', 'Co.', 'Inc.', 'St.', 'Ave.', 'Bldg.', 'No.',
|
|
136
|
+
|
|
137
|
+
// Other
|
|
138
|
+
'×', 'x', 'TV', 'IRL', 'UK'
|
|
103
139
|
];
|
|
104
140
|
|
|
105
141
|
export const wordReplacementsList = [
|
|
@@ -126,6 +162,7 @@ export const wordReplacementsList = [
|
|
|
126
162
|
{ 't.b.d': 'TBD' },
|
|
127
163
|
{ 'vuejs': 'Vue.js' },
|
|
128
164
|
{ 'phd': 'ph.d.' },
|
|
165
|
+
{ 'x': '×' },
|
|
129
166
|
];
|
|
130
167
|
|
|
131
168
|
// ! TODO
|