TypeScript library to convert strings between different case formats.
npm install easy-string-case> TypeScript library to convert strings between different case formats.
---
- 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.
---
```
npm install easy-string-case
---
`ts
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'
`
---
All methods are static and take a single argument: the string to convert.
#### StringCase.toCamelCase(str: string): string"hello world"
Converts to camelCase. Example: → "helloWorld"
#### StringCase.toPascalCase(str: string): string"foo_bar-baz"
Converts to PascalCase (UpperCamelCase). Example: → "FooBarBaz"
#### StringCase.toKebabCase(str: string): string"HelloWorld"
Converts to kebab-case. Example: → "hello-world"
#### StringCase.toSnakeCase(str: string): string"foo-bar baz"
Converts to snake_case. Example: → "foo_bar_baz"
#### StringCase.toDotCase(str: string): string"snake_case-example"
Converts to dot.case. Example: → "snake.case.example"
#### StringCase.toPathCase(str: string): string"FooBarBaz"
Converts to path/case. Example: → "foo/bar/baz"
#### StringCase.toScreamingSnakeCase(str: string): string"helloWorld"
Converts to SCREAMING_SNAKE_CASE. Example: → "HELLO_WORLD"
#### StringCase.toCobolCase(str: string): string"snake_case"
Converts to COBOL-CASE. Example: → "SNAKE-CASE"
#### StringCase.toTitleCase(str: string): string"mixedCASE input"
Converts to Title Case. Example: → "MixedCASE Input"
#### StringCase.toSentenceCase(str: string): string"THIS_IS_A_TEST"
Converts to Sentence case. Example: → "This is a test"
#### StringCase.toUpperCamelCase(str: string): string"hello world"
Converts to UpperCamelCase (PascalCase). Example: → "HelloWorld"
---
`ts`
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'
---
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.
---
The library includes unit tests with Jest for all methods and relevant cases. You can run them with:
```
npm run test
---
MIT