casing-kit
Version:
A lightweight, zero-dependency utility library for case transformations in JavaScript and TypeScript
120 lines (88 loc) • 4.48 kB
Markdown
A lightweight, zero-dependency utility library for case transformations in JavaScript and TypeScript.
[](https://www.npmjs.com/package/casing-kit)
[](https://github.com/username/casing-kit/blob/main/LICENSE)
- :arrows_counterclockwise: Convert between 15+ different case formats
- :dart: Precise control over transformation behavior
- :mag: Intelligent handling of special characters and edge cases
- :package: Zero dependencies
- :globe_with_meridians: Works in Node.js and browsers
- :memo: Written in TypeScript with full type definitions
```bash
npm install casing-kit
yarn add casing-kit
pnpm add casing-kit
```
```javascript
import { toCamelCase, toSnakeCase, toKebabCase } from "casing-kit";
toCamelCase("hello world"); // 'helloWorld'
toSnakeCase("helloWorld"); // 'hello_world'
toKebabCase("hello-world"); // 'hello-world'
```
| Function | Description | Example |
| ------------------ | -------------------------------------- | --------------------------------- |
| `toCamelCase()` | First word lowercase, rest capitalized | `"hello world"` → `"helloWorld"` |
| `toCobolCase()` | Uppercase with hyphens | `"helloWorld"` → `"HELLO-WORLD"` |
| `toConstantCase()` | Uppercase with underscores | `"helloWorld"` → `"HELLO_WORLD"` |
| `toDotCase()` | Lowercase with dots | `"helloWorld"` → `"hello.world"` |
| `toFlatCase()` | All lowercase, no separators | `"Hello-World"` → `"helloworld"` |
| `toKebabCase()` | Lowercase with hyphens | `"helloWorld"` → `"hello-world"` |
| `toLowerCase()` | All lowercase | `"Hello World"` → `"hello world"` |
| `toPascalCase()` | All words capitalized | `"hello world"` → `"HelloWorld"` |
| `toPathCase()` | Lowercase with slashes | `"helloWorld"` → `"hello/world"` |
| `toSentenceCase()` | First letter capitalized | `"helloWorld"` → `"Hello world"` |
| `toSnakeCase()` | Lowercase with underscores | `"helloWorld"` → `"hello_world"` |
| `toSpongeCase()` | Alternating case | `"helloWorld"` → `"hElLoWoRlD"` |
| `toSwapCase()` | Inverted case | `"Hello World"` → `"hELLO wORLD"` |
| `toTitleCase()` | First letter of each word capitalized | `"hello world"` → `"Hello World"` |
| `toTrainCase()` | Capitalized with hyphens | `"helloWorld"` → `"Hello-World"` |
| `toUpperCase()` | All uppercase | `"hello world"` → `"HELLO WORLD"` |
All case functions accept these options:
```javascript
// Preserve whitespace
toCamelCase(" hello world ", { preserveWhitespace: true }); // ' helloWorld '
// Preserve special characters
toConstantCase("hello@world", { preserveSpecialCharacters: true }); // 'HELLO@WORLD'
```
```javascript
// Custom transformation with options
customTransform(
"hello-world",
(word, index) => (index === 0 ? word : word.toUpperCase()),
{ separator: "-", outputSeparator: "." }
); // 'hello.WORLD'
```
```javascript
// Transform object keys to camelCase
transformObjectCasing({ user_id: 123, first_name: "John" }, toCamelCase);
// { userId: 123, firstName: "John" }
// Transform string values to snake_case
transformObjectCasing({ userId: "johnDoe" }, toSnakeCase, {
transformKeys: false,
transformValues: true,
});
// { userId: "john_doe" }
```
Full TypeScript definitions included:
```typescript
import { toCamelCase, type TransformOptions } from "casing-kit";
const options: TransformOptions = {
preserveWhitespace: true,
};
const result: string = toCamelCase("hello_world", options);
```
casing-kit targets ES2018 and works in all modern browsers and Node.js environments that support this standard. For older environments that don't support ES2018, you'll need to use a transpiler like Babel with appropriate polyfills.
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.