|
| 1 | +# komoji ✨ |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <img src="https://raw.githubusercontent.com/hyperweb-io/dev-utils/refs/heads/main/docs/img/logo.svg" width="80"> |
| 5 | + <br /> |
| 6 | + <strong>the tiny case transformer</strong> |
| 7 | + <br /> |
| 8 | + <br /> |
| 9 | + Effortlessly transform strings between naming conventions |
| 10 | + <br /> |
| 11 | + <br /> |
| 12 | + <a href="https://github.com/hyperweb-io/dev-utils/actions/workflows/ci.yml"> |
| 13 | + <img height="20" src="https://github.com/hyperweb-io/dev-utils/actions/workflows/ci.yml/badge.svg" /> |
| 14 | + </a> |
| 15 | + <a href="https://github.com/hyperweb-io/dev-utils/blob/main/LICENSE"> |
| 16 | + <img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/> |
| 17 | + </a> |
| 18 | +</p> |
| 19 | + |
| 20 | +## Why komoji? |
| 21 | + |
| 22 | +komoji is your friendly companion for working with naming conventions. It's tiny, focused, and does one thing exceptionally well: transforming strings between different cases with zero dependencies. |
| 23 | + |
| 24 | +Perfect for: |
| 25 | +- 🔄 Converting API responses to JavaScript conventions |
| 26 | +- 🎨 Generating code from schemas and templates |
| 27 | +- 🛠️ Building developer tools and CLI utilities |
| 28 | +- 📦 Processing configuration files across formats |
| 29 | + |
| 30 | +## Install |
| 31 | + |
| 32 | +```sh |
| 33 | +npm install komoji |
| 34 | +``` |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +### Transform to PascalCase |
| 39 | + |
| 40 | +```typescript |
| 41 | +import { toPascalCase } from 'komoji'; |
| 42 | + |
| 43 | +toPascalCase('hello-world'); // HelloWorld |
| 44 | +toPascalCase('user_name'); // UserName |
| 45 | +toPascalCase('api response'); // ApiResponse |
| 46 | +toPascalCase('my-component_v2'); // MyComponentV2 |
| 47 | +``` |
| 48 | + |
| 49 | +### Transform to camelCase |
| 50 | + |
| 51 | +```typescript |
| 52 | +import { toCamelCase } from 'komoji'; |
| 53 | + |
| 54 | +toCamelCase('hello-world'); // helloWorld |
| 55 | +toCamelCase('user_name'); // userName |
| 56 | +toCamelCase('api-response-data'); // apiResponseData |
| 57 | + |
| 58 | +// Strip leading non-alphabetic characters |
| 59 | +toCamelCase('__private_field', true); // privateField |
| 60 | +toCamelCase('123-invalid', true); // invalid |
| 61 | +``` |
| 62 | + |
| 63 | +### Validate Identifiers |
| 64 | + |
| 65 | +```typescript |
| 66 | +import { isValidIdentifier, isValidIdentifierCamelized } from 'komoji'; |
| 67 | + |
| 68 | +// Check if string is a valid JavaScript identifier |
| 69 | +isValidIdentifier('myVar'); // true |
| 70 | +isValidIdentifier('my-var'); // false |
| 71 | +isValidIdentifier('123abc'); // false |
| 72 | +isValidIdentifier('_private'); // true |
| 73 | + |
| 74 | +// Check if string can be camelized into valid identifier |
| 75 | +isValidIdentifierCamelized('my-var'); // true (can become myVar) |
| 76 | +isValidIdentifierCamelized('valid_id'); // true |
| 77 | +isValidIdentifierCamelized('-invalid'); // false (starts with hyphen) |
| 78 | +``` |
| 79 | + |
| 80 | +## API |
| 81 | + |
| 82 | +### `toPascalCase(str: string): string` |
| 83 | + |
| 84 | +Converts a string to PascalCase by capitalizing the first letter of each word and removing separators. |
| 85 | + |
| 86 | +**Supported separators:** hyphens (`-`), underscores (`_`), spaces (` `) |
| 87 | + |
| 88 | +### `toCamelCase(key: string, stripLeadingNonAlphabetChars?: boolean): string` |
| 89 | + |
| 90 | +Converts a string to camelCase with an optional flag to strip leading non-alphabetic characters. |
| 91 | + |
| 92 | +**Parameters:** |
| 93 | +- `key` - The string to transform |
| 94 | +- `stripLeadingNonAlphabetChars` - Remove leading non-alphabetic characters (default: `false`) |
| 95 | + |
| 96 | +### `isValidIdentifier(key: string): boolean` |
| 97 | + |
| 98 | +Checks if a string is a valid JavaScript identifier (follows standard naming rules). |
| 99 | + |
| 100 | +### `isValidIdentifierCamelized(key: string): boolean` |
| 101 | + |
| 102 | +Checks if a string can be transformed into a valid JavaScript identifier (allows internal hyphens that will be removed during camelization). |
| 103 | + |
| 104 | +## Design Philosophy |
| 105 | + |
| 106 | +komoji embraces simplicity: |
| 107 | +- 🎯 Zero dependencies |
| 108 | +- 🪶 Tiny footprint |
| 109 | +- 🚀 Fast and predictable |
| 110 | +- 💎 Pure functions |
| 111 | +- 📖 Clear, focused API |
| 112 | + |
| 113 | +## Developing |
| 114 | + |
| 115 | +When first cloning the repo: |
| 116 | + |
| 117 | +```sh |
| 118 | +pnpm install |
| 119 | +pnpm build |
| 120 | +``` |
0 commit comments