all-in-one-text-case-converter
v1.0.0
Published
A comprehensive text case conversion library with TypeScript support and regex capabilities
Maintainers
Readme
Text Case Converter
A comprehensive TypeScript/JavaScript library for text case conversion with regex-based transformation capabilities.
Features
- 🔄 Multiple Case Types: Convert between camelCase, PascalCase, snake_case, kebab-case, CONSTANT_CASE, Title Case, Sentence case, UPPERCASE, and lowercase
- 🎯 Regex Support: Advanced text transformations using custom regex patterns
- 🔍 Case Detection: Automatically detect the case type of input text
- 📦 TypeScript Support: Full TypeScript definitions included
- 🧪 Well Tested: Comprehensive test suite with 100% coverage
- 🚀 Zero Dependencies: Lightweight with no external dependencies
- 🛠️ Flexible Options: Customizable separators, whitespace preservation, and word boundary patterns
Installation
npm install text-case-converterQuick Start
import { TextCaseConverter } from 'text-case-converter';
// Basic case conversion
TextCaseConverter.convert('hello world', 'camel'); // 'helloWorld'
TextCaseConverter.convert('hello world', 'pascal'); // 'HelloWorld'
TextCaseConverter.convert('hello world', 'snake'); // 'hello_world'
TextCaseConverter.convert('hello world', 'kebab'); // 'hello-world'
// Convert to all case types at once
const allCases = TextCaseConverter.convertToAll('hello world');
console.log(allCases);
// {
// camel: 'helloWorld',
// pascal: 'HelloWorld',
// snake: 'hello_world',
// kebab: 'hello-world',
// constant: 'HELLO_WORLD',
// title: 'Hello World',
// sentence: 'Hello world',
// upper: 'HELLO WORLD',
// lower: 'hello world'
// }
// Detect case type
TextCaseConverter.detectCase('helloWorld'); // 'camel'
TextCaseConverter.detectCase('HelloWorld'); // 'pascal'
TextCaseConverter.detectCase('hello_world'); // 'snake'API Reference
TextCaseConverter
convert(text: string, caseType: CaseType, options?: ConversionOptions): string
Convert text to the specified case type.
Parameters:
text- The input text to convertcaseType- Target case type ('camel','pascal','snake','kebab','constant','title','sentence','upper','lower')options- Optional conversion options
Example:
TextCaseConverter.convert('hello world', 'camel', {
preserveWhitespace: true,
separator: '_'
});convertBatch(texts: string[], caseType: CaseType, options?: ConversionOptions): string[]
Convert multiple texts to the same case type.
const texts = ['hello world', 'foo bar', 'test case'];
TextCaseConverter.convertBatch(texts, 'camel');
// ['helloWorld', 'fooBar', 'testCase']convertToAll(text: string, options?: ConversionOptions): Record<CaseType, string>
Convert text to all supported case types.
detectCase(text: string): CaseType | 'mixed' | 'unknown'
Detect the case type of input text.
regexTransform(text: string, options: RegexTransformOptions): string
Apply regex-based transformation to text.
TextCaseConverter.regexTransform('hello world', {
pattern: /world/g,
replacement: 'universe'
});
// 'hello universe'RegexTransformer
Advanced regex-based text transformations.
Predefined Transformations
import { RegexTransformer } from 'text-case-converter';
// Normalize whitespace
RegexTransformer.transformations.normalizeWhitespace('hello world');
// 'hello world'
// Remove special characters
RegexTransformer.transformations.removeSpecialChars('hello@#$world!');
// 'helloworld'
// Extract words
RegexTransformer.transformations.extractWords('hello, world! 123');
// ['hello', 'world', '123']
// Mask emails
RegexTransformer.transformations.maskEmails('Contact [email protected]');
// 'Contact [EMAIL]'
// Format phone numbers
RegexTransformer.transformations.formatPhoneNumbers('Call 1234567890');
// 'Call (123) 456-7890'
// Split camelCase
RegexTransformer.transformations.splitCamelCase('helloWorldTest');
// 'hello World Test'Custom Patterns
// Use predefined patterns
RegexTransformer.patterns.email; // Email regex pattern
RegexTransformer.patterns.url; // URL regex pattern
RegexTransformer.patterns.phone; // Phone number pattern
RegexTransformer.patterns.camelCaseBoundaries; // camelCase boundaries
// Custom transformation
RegexTransformer.transform('ABC123DEF456', {
pattern: /([A-Z]+)(\d+)/g,
replacement: '$1-$2'
});
// 'ABC-123DEF-456'Individual Converters
You can also import and use individual converter classes:
import { CaseConverter } from 'text-case-converter';
CaseConverter.toCamelCase('hello world'); // 'helloWorld'
CaseConverter.toPascalCase('hello world'); // 'HelloWorld'
CaseConverter.toSnakeCase('hello world'); // 'hello_world'
CaseConverter.toKebabCase('hello world'); // 'hello-world'
CaseConverter.toConstantCase('hello world'); // 'HELLO_WORLD'
CaseConverter.toTitleCase('hello world'); // 'Hello World'
CaseConverter.toSentenceCase('hello world'); // 'Hello world'
CaseConverter.toUpperCase('hello world'); // 'HELLO WORLD'
CaseConverter.toLowerCase('HELLO WORLD'); // 'hello world'Configuration Options
ConversionOptions
interface ConversionOptions {
separator?: string; // Custom separator for words
preserveWhitespace?: boolean; // Preserve leading/trailing whitespace
wordBoundaryPattern?: RegExp; // Custom word boundary pattern
}RegexTransformOptions
interface RegexTransformOptions {
pattern: RegExp; // Regex pattern to match
replacement: string | ((match: string, ...args: any[]) => string);
global?: boolean; // Apply transformation globally
}Advanced Examples
Custom Word Boundaries
TextCaseConverter.convert('hello123world', 'camel', {
wordBoundaryPattern: /\d+/ // Split on numbers
});
// Result depends on custom patternPreserve Whitespace
TextCaseConverter.convert(' hello world ', 'camel', {
preserveWhitespace: true
});
// ' helloWorld 'Complex Regex Transformations
// Convert currency format
TextCaseConverter.regexTransform('price: $10.50', {
pattern: /\$(\d+)\.(\d+)/g,
replacement: (match, dollars, cents) => {
const total = parseInt(dollars) * 100 + parseInt(cents);
return `${total} cents`;
}
});
// 'price: 1050 cents'Chaining Transformations
let text = '[email protected] world';
// First mask emails
text = RegexTransformer.transformations.maskEmails(text);
// 'hello [EMAIL] world'
// Then convert to title case
text = TextCaseConverter.convert(text, 'title');
// 'Hello [EMAIL] World'Browser Support
This library works in all modern browsers and Node.js environments. It's written in TypeScript and compiled to ES5 for maximum compatibility.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite:
npm test - Submit a pull request
License
MIT License - see LICENSE file for details.
Changelog
1.0.0
- Initial release
- Support for 9 case types
- Regex transformation capabilities
- TypeScript support
- Comprehensive test suite
