Gathering detailed insights and metrics for @danielhaim/titlecaser
Gathering detailed insights and metrics for @danielhaim/titlecaser
Gathering detailed insights and metrics for @danielhaim/titlecaser
Gathering detailed insights and metrics for @danielhaim/titlecaser
A powerful utility for transforming text to title case with support for multiple style guides and extensive customization options.
npm install @danielhaim/titlecaser
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
10 Stars
355 Commits
1 Forks
1 Watchers
3 Branches
2 Contributors
Updated on Jun 25, 2025
Latest Version
1.7.6
Package Id
@danielhaim/titlecaser@1.7.6
Unpacked Size
190.06 kB
Size
55.33 kB
File Count
19
NPM Version
10.8.2
Node Version
20.17.0
Published on
Apr 09, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
22
A powerful utility for converting text to title case with support for multiple style guides and extensive customization options.
TitleCaser is a comprehensive solution for converting text to title case according to various style guides (AP, APA, Chicago, NYT, Wikipedia, British). It handles special cases like hyphens, apostrophes, Roman numerals, and acronyms, and provides extensive customization options.
The comprehensive Language Conventions and Style Library (LCSL) is specifically designed to assist web content developers in adhering to the latest style guides and English language conventions. This all-inclusive library has various features, including support for numerous style guides such as AP, APA, Chicago, NY Times, Wikipedia, and British styles and customizable preferences to tailor to individual needs. TitleCaser is a component of this library, and LCSL is set to be open-sourced by the end of 2023.
To streamline workflow, modules are available in both browser and node environment versions and include a command-line interface for building, testing, and minimizing the module. Additionally, it features a filter ability that allows users to ignore certain phrases containing short words, preventing the module from mistakenly flagging instances where short words are used as part of a larger term or phrase.
The module has been designed to handle various capitalization scenarios, including:
This ensures that your content meets the appropriate style and formatting guidelines, regardless of the context. It also offers word replacement capabilities, as well as ignored phrases to create consistency in cases where certain terms may be capitalized differently depending on the context.
Whether you're developing web content for a major news organization or simply looking to improve your writing skills, this module is an essential tool that can help ensure your work is accurate, consistent, and conforms to the latest style guidelines.
1npm install @danielhaim/titlecaser
The package can be imported and used in both Node.js and browser environments:
1import { TitleCaser } from '@danielhaim/titlecaser'; 2 3// Basic usage with Chicago style 4const titleCaser = new TitleCaser({ 5 style: 'chicago' 6}); 7const result = titleCaser.toTitleCase('the book of life'); 8console.log(result); // "The Book of Life" 9 10// With custom options 11const customTitleCaser = new TitleCaser({ 12 style: 'ap', 13 smartQuotes: true, 14 ignoredWords: ['a', 'an', 'the'], 15 acronyms: ['API', 'JSON', 'XML'] 16}); 17 18const customResult = customTitleCaser.toTitleCase('the api and json data'); 19console.log(customResult); // "The API and JSON Data" 20 21// Add custom word replacements 22titleCaser.addReplaceTerm('js', 'JavaScript'); 23const jsResult = titleCaser.toTitleCase('js development'); 24console.log(jsResult); // "JavaScript Development" 25 26// Add exact phrase replacements 27titleCaser.addExactPhraseReplacements([ 28 { 'the correct phrase': 'The Correct Phrase' } 29]); 30const phraseResult = titleCaser.toTitleCase('this is the correct phrase'); 31console.log(phraseResult); // "This Is The Correct Phrase"
The function can also be used in a browser environment by including the TitleCaser.amd.js
script in your HTML file:
1<script src="./path/to/TitleCaser.amd.js"></script>
After that, the toTitleCase()
function can be accessed in your JavaScript code like this:
1const options = { 2 style: 'apa' 3}; 4const input = 'the future of devops: the next era'; 5const output = input.toTitleCase(options); 6 7console.log(output); // The Future of DevOps: The Next Era
1<h2>nodejs development on aws: an in-depth tutorial on server-side javascript deployment</h2> 2<h2>the iphone's impact on modern communication: a sociolinguistic analysis</h2> 3<h2>back-end and front-end</h2>
1function applyTitleCaseToH2Elements(options = { style: "apa" }) { 2 try { 3 const h2Elements = document.querySelectorAll("h2"); 4 h2Elements.forEach((h2) => { 5 const innerHTML = h2.innerHTML; 6 const modifiedContent = innerHTML.toTitleCase(options); 7 h2.innerHTML = modifiedContent; 8 }); 9 } catch (error) { 10 console.error( 11 "An error occurred while applying title case transformation:", 12 error 13 ); 14 } 15} 16 17applyTitleCaseToH2Elements();
The {options}
parameter is an object that contains the settings for the conversion process.
style
: determines the specific title case style to be applied. Permissible values include: ['ap', 'apa', 'british', 'chicago', 'nyt', 'wikipedia']
articlesList
refers to the words that should be treated as articles in title case.shortConjunctionsList
pertains to the words that should be treated as short conjunctions in title case.shortPrepositionsList
relates to the words that should be treated as short prepositions in title case.neverCapitalizedList
contains the words that should never be capitalized in title case.wordReplacementsList
is a map of terms that will be replaced during the title case conversion process.smartQuotes
boolean value that determines whether quotes should be replaced with smart quotes.setReplaceTerms(terms)
: Updates the wordReplacementsList
with new term-replacement pairs. It accepts an array of objects, each containing a single key-value pair representing the term and its replacement.removeReplaceTerm(term)
: Removes a replaced term from the wordReplacementsList
array in the option object of the TitleCaser
instance. Throws an error if the term is not found in the array, otherwise removes it from the array and updates the option object.addReplaceTerm(term, replacement)
: Adds a single term-replacement pair to the wordReplacementsList
. If the term already exists, it updates the replacement value.addExactPhraseReplacements(newPhrases)
- This method allows adding an array of exact phrase replacements to the TitleCaser
class. Each item in the array should be an object with a single key-value pair, where the key is the phrase to be replaced and the value is the desired replacement.setStyle(style: string)
: Sets the style option in the object of the TitleCaser instance. The method takes a string argument style that specifies the style to use for the title casing. If the argument is not a string, the method throws a TypeError. Otherwise, it updates the style option in the object.smartQuotes(smartQuotes: boolean)
: Specifies whether to replace straight quotes with smart quotes during title casing. Provide a boolean argument smartQuotes to enable or disable this feature.1import { TitleCaser } from '@danielhaim/titlecaser'; 2 3const titleCaser = new TitleCaser(); 4const result = titleCaser.toTitleCase('hello world'); 5console.log(result); // "Hello World"
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.
1import { TitleCaser } from '@danielhaim/titlecaser'; 2 3const titleCaser = new TitleCaser({ 4 style: 'apa' 5}); 6 7// Set multiple replacement terms using two separate calls to setReplaceTerms() 8titleCaser.setReplaceTerms({ 9 'hello world': 'Hello World', 10 'replace me': 'Replace Me' 11}); 12titleCaser.setReplaceTerms({ 13 'apa': 'APA' 14}); 15 16// Use descriptive variable names for the input and expected output 17const inputString = "hello world, replace me!"; 18const expectedOutput = "Hello World, Replace Me!"; 19 20// Call toTitleCase() to convert the input string to title case 21const outputString = titleCaser.toTitleCase(inputString);
The example below demonstrates how to use the TitleCaser class to convert a string to a title case with specific settings.
1import { TitleCaser } from '@danielhaim/titlecaser'; 2 3// Set the options object 4const options = { 5 style: "nyt", 6 wordReplacementsList: { 7 "nodejs": "Node.js", 8 "javascript": "JavaScript", 9 "mongodb": "MongoDB" 10 } 11}; 12 13// Instantiate a new TitleCaser object with the options 14const titleCaser = new TitleCaser(options); 15 16// Set the input string to be tested 17const input = "the basics of nodejs development with mongodb"; 18 19// Set the expected output 20const expectedOutput = "The Basics of Node.js Development with MongoDB"; 21 22// Call the toTitleCase method and store the result in actualOutput 23const actualOutput = titleCaser.toTitleCase(input);
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.
1import { TitleCaser } from '@danielhaim/titlecaser'; 2 3// Instantiate a new TitleCaser object with AP style formatting 4const titleCaser = new TitleCaser({ style: 'ap' }); 5 6// Set the input string to be tested 7const input = 'nodejs development on aws: an in-depth tutorial on server-side javascript deployment'; 8 9// Set the expected output 10const expectedOutput = 'Node.js Development on AWS: An In-depth Tutorial on Server-side JavaScript Deployment'; 11 12// Call the toTitleCase method and store the result in actualOutput 13const actualOutput = titleCaser.toTitleCase(input);
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.
1import { TitleCaser } from '@danielhaim/titlecaser'; 2 3// Instantiate a new TitleCaser object with AP style formatting 4const titleCaser = new TitleCaser({ style: "ap" }); 5 6// Set the input string to be tested 7const input = "the iphone's impact on modern communication: a sociolinguistic analysis"; 8 9// Set the expected output 10const expectedOutput = "The iPhone's Impact on Modern Communication: A Sociolinguistic Analysis"; 11 12// Call the toTitleCase method and store the result in actualOutput 13const actualOutput = titleCaser.toTitleCase(input);
The example below demonstrates how to use the TitleCaser with smart quotes.
1import { TitleCaser } from '@danielhaim/titlecaser'; 2 3// Instantiate a new TitleCaser object with AP style formatting and smart quotes enabled 4const titleCaser = new TitleCaser({ 5 style: 'ap', 6 smartQuotes: true 7}); 8 9// Set the input string to be tested 10const input = '"Never underestimate the power O\' persistence,"'; 11 12// Set the expected output 13const expectedOutput = '"Never Underestimate the Power O' Persistence,"'; 14 15// Call the toTitleCase method and store the result in actualOutput 16const actualOutput = titleCaser.toTitleCase(input);
TitleCaser is structured into three main components:
The package uses several JSON files to store specialized terms:
1new TitleCaser(options)
style
(string): Title case style ('ap', 'apa', 'chicago', 'nyt', 'wikipedia', 'british')smartQuotes
(boolean): Convert straight quotes to curly quotesignoredWords
(array): Words to ignore in title casingacronyms
(array): Words to treat as acronymsConverts text to title case according to the selected style.
1const result = titleCaser.toTitleCase('hello world');
Sets the word replacement list.
1titleCaser.setReplaceTerms([ 2 { 'js': 'JavaScript' }, 3 { 'api': 'API' } 4]);
Adds a single term replacement.
1titleCaser.addReplaceTerm('js', 'JavaScript');
Removes a term from the replacement list.
1titleCaser.removeReplaceTerm('js');
Adds exact phrase replacements.
1titleCaser.addExactPhraseReplacements([ 2 { 'the correct phrase': 'The Correct Phrase' } 3]);
Sets the title case style.
1titleCaser.setStyle('chicago');
1npm run build-package 2npm run build-docs 3npm run copy-package-to-docs 4npm run test
1npm run test
Useful materials for improving your knowledge of writing and language style guides. These resources include various books and manuals, such as the Publication Manual of the American Psychological Association, the Chicago Manual of Style, and the AP Stylebook, which are widely recognized as authoritative sources on grammar, punctuation, and capitalization rules.
If you encounter any bugs or issues while using the library or the demo page, please report them by opening a new issue in the repository's issue tracker.
When reporting a bug, please provide as much detail as possible, including the steps to reproduce the issue and any error messages that you see. I appreciate any contribution to improving this library.
We welcome contributions! Please see our Contributing Guidelines for details.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
See CHANGELOG.md for a list of changes and version history.
1TitleCaser – Combined Words Ending with Symbol 2 ✓ should preserve punctuation in "Championing Self-Acceptance: Landmark Initiative" (4 ms) 3TitleCaser – Disambiguation of Acronym vs. Pronoun (AP Style) 4 ✓ should capitalize "US" when preceded by "the" (1 ms) 5 ✓ should capitalize "US" in geopolitical context (with comma) (1 ms) 6 ✓ should capitalize "US" and "UK" together 7 ✓ should capitalize "US" after "from the" (1 ms) 8 ✓ should capitalize "US" after "via" (1 ms) 9 ✓ should capitalize "US" before "Military" 10 ✓ should capitalize "US" in geopolitical context (repeated case) (1 ms) 11 ✓ should capitalize pronoun "us" (case #1) (1 ms) 12 ✓ should capitalize pronoun "us" (case #2) 13 ✓ should capitalize pronoun "us" (case #3) (1 ms) 14 ✓ should capitalize "UK" in geopolitical context (with comma) 15 ✓ should handle multiple country codes in same sentence (1 ms) 16 ✓ should capitalize pronoun "us" in a parenthetical phrase (1 ms) 17 ✓ should capitalize "USA" in formal context 18 ✓ should capitalize "US" before "government" (AP style) (1 ms) 19 ✓ should capitalize "US" before "military" (AP style) 20 ✓ should capitalize "US" in geopolitical context (policy mention) 21 ✓ should capitalize "US" before "Military" (repeat case) (1 ms) 22 ✓ should capitalize "US" at the end of a sentence 23 ✓ should capitalize pronoun "us" in casual speech 24 ✓ should capitalize pronoun "us" in emotional context (1 ms) 25 ✓ should capitalize pronoun "us" in passive voice 26 ✓ should capitalize pronoun "us" with a compound verb 27 ✓ should capitalize pronoun "us" in an inverted clause (1 ms) 28 ✓ should capitalize pronoun "us" (repeat case #1) 29 ✓ should capitalize pronoun "us" (repeat case #2) (1 ms) 30 ✓ should capitalize pronoun "us" before "US Military" 31 ✓ should capitalize pronoun "us" before "military" (1 ms) 32 ✓ should capitalize pronoun "us" in a comma-separated clause 33 ✓ should capitalize "UK" at end of sentence (1 ms) 34 ✓ should capitalize "UK" in geopolitical context (repeat with comma) 35 ✓ should capitalize "UK" before government mention (1 ms) 36 ✓ should capitalize "UK" before territory mention 37 ✓ should handle multiple country codes and pronouns in one sentence (2 ms) 38 ✓ should handle multiple codes and pronouns with mention of military 39 ✓ should handle multiple codes and pronouns with mention of talks (1 ms) 40 ✓ should handle multiple codes and pronouns with mention of a bill (1 ms) 41 ✓ should capitalize "USA" in formal context (repeat case) 42 ✓ should capitalize "USA" before a bill mention (1 ms) 43 ✓ should handle AP-style acronym "U.S." in uppercase context 44 ✓ should handle "On & Off" phrases #1 (1 ms) 45 ✓ should handle "On & Off" phrases #2 46TitleCaser – Hyphenated & Apostrophized Words 47 ✓ should capitalize both parts of "t-mobile" 48 ✓ should capitalize "coca-cola" 49 ✓ should capitalize general hyphenated term "e-commerce" (1 ms) 50 ✓ should capitalize apostrophe word "o'connor" 51TitleCaser – Basic Title Casing Options (Default Style) 52 ✓ should convert a basic lowercase phrase to title case 53 ✓ should handle excessive spacing and lowercase articles 54 ✓ should handle brand casing with AP-style logic (1 ms) 55 ✓ should preserve correct casing in hyphenated names like "louis-iv" 56 ✓ should properly capitalize prepositions beyond 3 letters (1 ms) 57TitleCaser – Class Methods (setReplaceTerms, addExactPhraseReplacements, etc.) 58 ✓ should apply Wikipedia style to an entire sentence (1 ms) 59 ✓ should apply multiple term replacements via setReplaceTerms() 60 ✓ should apply exact phrase replacements (1 ms) 61 ✓ should remove a single replacement rule via removeReplaceTerm() 62TitleCaser – Variation Stability Tests (AP, Chicago, APA, NYT, Wikipedia) 63 ✓ should correctly handle brand names with "ap" style 64 ✓ should handle brand name "NERDs Candy" with AP style 65 ✓ should handle possessives and colons (AP style) 66 ✓ should handle hyphenated "BACK-end" with AP style 67 ✓ should handle acronym with colon (AP style) (1 ms) 68 ✓ should handle colon + comparison phrase (Chicago) 69 ✓ should capitalize hyphenated terms (Chicago) (1 ms) 70 ✓ should apply custom replacements for brand names (Chicago) (1 ms) 71 ✓ Smart quotes enabled 72 ✓ should keep acronyms in uppercase (Chicago) 73 ✓ should capitalize after colon (APA) (1 ms) 74 ✓ should handle colon and apostrophes (APA) 75 ✓ should handle short conjunctions and brand normalization (APA) (1 ms) 76 ✓ should handle acronym + colon usage (NYT) (1 ms) 77 ✓ should preserve sentence case for Wikipedia style (DevOps example) (1 ms) 78 ✓ should handle Wikipedia style with colon usage (1 ms) 79TitleCaser – Reserved Words & Special Handling 80 ✓ should transform a single reserved word correctly 81 ✓ should transform sentence with reserved word + colon 82 ✓ should handle possessive form of reserved word 83 ✓ should apply brand replacements (e.g., "mcdonalds" → "McDonald's") (1 ms) 84 ✓ should handle HTML <br> with colon (spaced) 85 ✓ should handle HTML <br> with full sentence split (1 ms) 86 ✓ should handle <br> with no space after colon 87 ✓ should handle ampersand "&" symbol (1 ms) 88 ✓ should handle excessive whitespace 89TitleCaser – addReplaceTerm Method 90 ✓ should add a new replacement term 91 ✓ should update an existing replacement term
No vulnerabilities found.
No security vulnerabilities found.