Convert into a space separated text with each word capitalized
npm install text-capital-case[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Bundle size][bundlephobia-image]][bundlephobia-url]


> Transform text into Capital Case format where each word is capitalized and separated by spaces.
- Lightweight - Only ~450B minified + gzipped
- Type-safe - Full TypeScript support with comprehensive type definitions
- Zero dependencies - No external dependencies
- Tree-shakeable - ES modules support
- Universal - Works in browsers, Node.js, and serverless environments
- Well-tested - Comprehensive test suite with edge cases
- Customizable - Flexible options for advanced use cases
``bashnpm
npm install text-capital-case
๐ฏ Quick Start
`javascript
import { capitalCase } from "text-capital-case";console.log(capitalCase("hello world")); // "Hello World"
console.log(capitalCase("user_profile_data")); // "User Profile Data"
console.log(capitalCase("backgroundColor")); // "Background Color"
`๐ Usage
$3
`javascript
import { capitalCase } from "text-capital-case";console.log(capitalCase("hello world")); // "Hello World"
`$3
`javascript
const { capitalCase } = require("text-capital-case");console.log(capitalCase("hello world")); // "Hello World"
`$3
`typescript
import { capitalCase, Options } from "text-capital-case";const result: string = capitalCase("hello world");
console.log(result); // "Hello World"
`๐ Transformation Examples
$3
`javascript
import { capitalCase } from "text-capital-case";// From different cases
capitalCase("hello world"); // "Hello World"
capitalCase("Hello World"); // "Hello World"
capitalCase("HELLO WORLD"); // "Hello World"
capitalCase("camelCase"); // "Camel Case"
capitalCase("PascalCase"); // "Pascal Case"
capitalCase("snake_case"); // "Snake Case"
capitalCase("kebab-case"); // "Kebab Case"
capitalCase("dot.case"); // "Dot Case"
// Complex examples
capitalCase("XMLHttpRequest"); // "Xml Http Request"
capitalCase("iPhone"); // "I Phone"
capitalCase("version 1.2.3"); // "Version 1 2 3"
capitalCase("userProfileData"); // "User Profile Data"
`$3
`javascript
import { capitalCase } from "text-capital-case";// Custom word splitting
capitalCase("XMLHttpRequest", {
splitRegexp: /([a-z])([A-Z])/g,
}); // "Xml Http Request"
// Custom character stripping
capitalCase("hello@world.com", {
stripRegexp: /[@.]/g,
}); // "Hello World Com"
// Custom transformation function
capitalCase("api-v2-endpoint", {
transform: (word, index) => {
if (word === "api") return "API";
if (word === "v2") return "V2";
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
},
}); // "API V2 Endpoint"
`๐ Real-World Examples
$3
`javascript
import { capitalCase } from "text-capital-case";// Job titles
capitalCase("chief executive officer"); // "Chief Executive Officer"
capitalCase("senior software engineer"); // "Senior Software Engineer"
capitalCase("product manager"); // "Product Manager"
capitalCase("data scientist"); // "Data Scientist"
capitalCase("marketing director"); // "Marketing Director"
`$3
`javascript
import { capitalCase } from "text-capital-case";// Document sections
capitalCase("table of contents"); // "Table Of Contents"
capitalCase("executive summary"); // "Executive Summary"
capitalCase("financial overview"); // "Financial Overview"
capitalCase("risk assessment"); // "Risk Assessment"
capitalCase("implementation plan"); // "Implementation Plan"
`$3
`javascript
import { capitalCase } from "text-capital-case";// Product names
capitalCase("premium subscription"); // "Premium Subscription"
capitalCase("enterprise solution"); // "Enterprise Solution"
capitalCase("mobile application"); // "Mobile Application"
capitalCase("cloud storage"); // "Cloud Storage"
capitalCase("customer support"); // "Customer Support"
`$3
`javascript
import { capitalCase } from "text-capital-case";// Form fields
capitalCase("billing information"); // "Billing Information"
capitalCase("shipping address"); // "Shipping Address"
capitalCase("payment method"); // "Payment Method"
capitalCase("contact details"); // "Contact Details"
capitalCase("account settings"); // "Account Settings"
`$3
`javascript
import { capitalCase } from "text-capital-case";// Transform content categories
const categories = [
"web_development",
"machine-learning",
"data.analysis",
"userExperience",
"projectManagement",
];
const formattedCategories = categories.map(capitalCase);
console.log(formattedCategories);
// [
// "Web Development",
// "Machine Learning",
// "Data Analysis",
// "User Experience",
// "Project Management"
// ]
`๐ API Reference
$3
Converts a string to Capital Case.
#### Parameters
-
input (string): The string to convert
- options (Options, optional): Configuration options#### Returns
-
string: The Capital Case formatted string#### Options
`typescript
interface Options {
// Custom transform function for word processing
transform?: (word: string, index: number, words: string[]) => string; // Regex to strip characters before processing
stripRegexp?: RegExp;
// Custom split function
split?: (value: string) => string[];
}
`๐ง Advanced Configuration
$3
`javascript
import { capitalCase } from "text-capital-case";// Split on specific patterns
capitalCase("XMLHttpRequest", {
splitRegexp: /([a-z])([A-Z])/g,
}); // "Xml Http Request"
// Split on numbers
capitalCase("user123data", {
splitRegexp: /([a-zA-Z])(\d)/g,
}); // "User 123 Data"
`$3
`javascript
import { capitalCase } from "text-capital-case";// Strip specific characters
capitalCase("hello@world.com", {
stripRegexp: /[@.]/g,
}); // "Hello World Com"
// Strip all non-alphanumeric
capitalCase("hello!@#world", {
stripRegexp: /[^a-zA-Z0-9]/g,
}); // "Hello World"
`$3
`javascript
import { capitalCase } from "text-capital-case";// Preserve acronyms
capitalCase("xml-http-request", {
transform: (word, index) => {
const acronyms = ["xml", "http", "api", "url", "html", "css", "js"];
if (acronyms.includes(word.toLowerCase())) {
return word.toUpperCase();
}
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
},
}); // "XML HTTP Request"
// Custom business logic
capitalCase("user-v2-api", {
transform: (word, index) => {
if (word === "v2") return "V2";
if (word === "api") return "API";
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
},
}); // "User V2 API"
`๐ Bundle Size
This package is optimized for minimal bundle size:
- Minified: ~450B
- Gzipped: ~250B
- Tree-shakeable: Yes
- Side effects: None
๐ Browser Support
- Modern browsers: ES2015+ (Chrome 51+, Firefox 54+, Safari 10+)
- Node.js: 12+
- TypeScript: 4.0+
- Bundle formats: UMD, ESM, CommonJS
๐งช Testing
`bash
Run tests
pnpm testRun tests in watch mode
pnpm test --watchRun tests with coverage
pnpm test --coverageType checking
pnpm typecheckLinting
pnpm lint
`๐ Related Packages
text-camel-case - Convert to camelCase
- text-constant-case - Convert to CONSTANT_CASE
- text-dot-case - Convert to dot.case
- text-header-case - Convert to Header-Case
- text-is-lower-case - Check if text is lower case
- text-case - All case transformations in one package๐ License
๐ค Contributing
1. Fork the repository
2. Create your feature branch (
git checkout -b feature/amazing-feature)
3. Commit your changes (git commit -m 'Add some amazing feature')
4. Push to the branch (git push origin feature/amazing-feature`)- ๐ง Email: selikhov.dmitrey@gmail.com
- ๐ Issues: GitHub Issues
- ๐ฌ Discussions: GitHub Discussions
- ๐ Documentation: Full Documentation
---
Made with โค๏ธ by Dmitry Selikhov
[npm-image]: https://img.shields.io/npm/v/text-capital-case.svg?style=flat
[npm-url]: https://npmjs.org/package/text-capital-case
[downloads-image]: https://img.shields.io/npm/dm/text-capital-case.svg?style=flat
[downloads-url]: https://npmjs.org/package/text-capital-case
[bundlephobia-image]: https://img.shields.io/bundlephobia/minzip/text-capital-case.svg
[bundlephobia-url]: https://bundlephobia.com/result?p=text-capital-case