Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add String.prototype.titleCase() to Node.js #57247

Closed
tanmoydutta114 opened this issue Mar 1, 2025 · 1 comment
Closed

Add String.prototype.titleCase() to Node.js #57247

tanmoydutta114 opened this issue Mar 1, 2025 · 1 comment
Labels
feature request Issues that request new features to be added to Node.js.

Comments

@tanmoydutta114
Copy link

What is the problem this feature will solve?

Currently, JavaScript lacks a built-in method to title-case a string, which means developers must either:

  1. Manually implement it using regex or loops every time.
  2. Use third-party libraries (e.g., lodash, change-case), which is unnecessary for such a basic function.
  3. Use CSS text-transform: capitalize;, which only works in the UI and doesn't handle logic-based transformations.

By adding String.prototype.titleCase(), JavaScript will have a native, consistent, and efficient way to format text, making it easier for developers to handle:

  • Names: "john doe".titleCase() // "John Doe"
  • Headlines: "breaking news: stock market rises".titleCase() // "Breaking News: Stock Market Rises"

This function aligns with other built-in string methods like .toUpperCase() and .toLowerCase(), enhancing the developer experience without unnecessary dependencies.

What is the feature you are proposing to solve the problem?

I propose adding a built-in String.prototype.titleCase() method to JavaScript, similar to Python’s string.title(). This method will capitalize the first letter of each word in a string while keeping the rest of the characters unchanged.

Proposed Implementation:

String.prototype.titleCase = function() {
    return this.replace(/\b\w/g, char => char.toUpperCase());
};

Example Usage:

console.log("hello world".titleCase());  // "Hello World"
console.log("this is an example".titleCase()); // "This Is An Example"

This function would provide a native, efficient, and consistent way to apply title case formatting without requiring third-party libraries or manual implementations.

What alternatives have you considered?

  1. Using Third-Party Libraries

    • Libraries like lodash (_.startCase()) or change-case provide similar functionality.
    • However, installing a full package for such a simple operation adds unnecessary dependencies.
  2. Manually Implementing It in Every Project

    • Developers currently write custom regex or loop-based solutions whenever title casing is needed.
    • This leads to inconsistent implementations across projects.
  3. Using CSS text-transform: capitalize;

    • While CSS can capitalize words in UI elements, it does not modify the actual string value in JavaScript, making it unsuitable for text processing.

Adding String.prototype.titleCase() would eliminate the need for these workarounds and provide a native, standardized solution for JavaScript developers.

@tanmoydutta114 tanmoydutta114 added the feature request Issues that request new features to be added to Node.js. label Mar 1, 2025
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Mar 1, 2025
@bnoordhuis
Copy link
Member

Language and standard library changes are not within node's remit. You'd have to take this up with TC39, they're the language stewards.

@bnoordhuis bnoordhuis closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js.
Projects
Archived in project
Development

No branches or pull requests

2 participants