easy-string-case
v1.0.2
Published
TypeScript library to convert strings between different case formats.
Downloads
17
Maintainers
Readme
easy-string-case
TypeScript library to convert strings between different case formats.
Main Features
- Converts strings between the most common formats:
camelCase,PascalCase,snake_case,kebab-case,dot.case,path/case,SCREAMING_SNAKE_CASE,COBOL-CASE,Title Case,Sentence case,UpperCamelCase. - Does not use any external dependencies. All processing is native and portable.
- Supports input in any format: spaces, dashes, underscores, camelCase, PascalCase, etc.
- Unicode-aware: supports international and accented characters.
- Easy to use and test.
Installation
npm install easy-string-caseBasic Usage
import { StringCase } from 'easy-string-case';
StringCase.toCamelCase('hello world'); // 'helloWorld'
StringCase.toPascalCase('foo_bar-baz'); // 'FooBarBaz'
StringCase.toKebabCase('HelloWorld'); // 'hello-world'
StringCase.toSnakeCase('foo-bar baz'); // 'foo_bar_baz'
StringCase.toDotCase('snake_case-example'); // 'snake.case.example'
StringCase.toPathCase('FooBarBaz'); // 'foo/bar/baz'
StringCase.toScreamingSnakeCase('helloWorld'); // 'HELLO_WORLD'
StringCase.toCobolCase('snake_case'); // 'SNAKE-CASE'
StringCase.toTitleCase('mixedCASE input'); // 'MixedCASE Input'
StringCase.toSentenceCase('THIS_IS_A_TEST'); // 'This is a test'
StringCase.toUpperCamelCase('hello world'); // 'HelloWorld'API
Main Methods
All methods are static and take a single argument: the string to convert.
StringCase.toCamelCase(str: string): string
Converts to camelCase. Example: "hello world" → "helloWorld"
StringCase.toPascalCase(str: string): string
Converts to PascalCase (UpperCamelCase). Example: "foo_bar-baz" → "FooBarBaz"
StringCase.toKebabCase(str: string): string
Converts to kebab-case. Example: "HelloWorld" → "hello-world"
StringCase.toSnakeCase(str: string): string
Converts to snake_case. Example: "foo-bar baz" → "foo_bar_baz"
StringCase.toDotCase(str: string): string
Converts to dot.case. Example: "snake_case-example" → "snake.case.example"
StringCase.toPathCase(str: string): string
Converts to path/case. Example: "FooBarBaz" → "foo/bar/baz"
StringCase.toScreamingSnakeCase(str: string): string
Converts to SCREAMING_SNAKE_CASE. Example: "helloWorld" → "HELLO_WORLD"
StringCase.toCobolCase(str: string): string
Converts to COBOL-CASE. Example: "snake_case" → "SNAKE-CASE"
StringCase.toTitleCase(str: string): string
Converts to Title Case. Example: "mixedCASE input" → "MixedCASE Input"
StringCase.toSentenceCase(str: string): string
Converts to Sentence case. Example: "THIS_IS_A_TEST" → "This is a test"
StringCase.toUpperCamelCase(str: string): string
Converts to UpperCamelCase (PascalCase). Example: "hello world" → "HelloWorld"
Advanced Examples
StringCase.toCamelCase('XML_http-request'); // 'xmlHttpRequest'
StringCase.toPascalCase('foo-bar_baz qux'); // 'FooBarBazQux'
StringCase.toKebabCase('some Text@ with spêcial#char'); // 'some-text-with-special-char'
StringCase.toSnakeCase(' hello world '); // 'hello_world'
StringCase.toDotCase('foo_bar.baz-qux'); // 'foo.bar.baz.qux'
StringCase.toPathCase('foo_bar.baz-qux'); // 'foo/bar/baz/qux'
StringCase.toScreamingSnakeCase('some Text@ with spêcial#char'); // 'SOME_TEXT_WITH_SPECIAL_CHAR'
StringCase.toCobolCase('foo-bar.baz_qux'); // 'FOO-BAR-BAZ-QUX'
StringCase.toTitleCase('foo-bar.baz_qux'); // 'Foo Bar Baz Qux'
StringCase.toSentenceCase('foo-bar.baz_qux'); // 'Foo bar baz qux'
StringCase.toUpperCamelCase('foo-bar.baz_qux'); // 'FooBarBazQux'Implementation
The main logic is in two classes:
- CommonCase: Normalizes the input string into an array of "words" using Unicode rules, common separators, and camelCase/PascalCase detection. This allows all methods to work correctly regardless of the original format.
- StringCase: Exposes static methods to convert between the different formats, using CommonCase normalization.
No external dependencies are used. All code is pure and portable TypeScript.
Tests
The library includes unit tests with Jest for all methods and relevant cases. You can run them with:
npm run testLicense
MIT
