Micro Hyphenization Utility
uhyphen is a compact, single-purpose JavaScript utility designed specifically for transforming camelCase strings into kebab-case (hyphenated strings). Currently at version 0.2.0, the package prioritizes a small footprint and focused functionality over extensive features. Its release cadence is infrequent, as the project appears to be stable and feature-complete for its intended use case, with the last code changes occurring several years ago. Key differentiators include its 'micro' nature, making it ideal for environments where bundle size and minimal dependencies are crucial. It provides a straightforward API for a common string manipulation task without incorporating complex linguistic hyphenation rules or broader string utility features.
Common errors
-
TypeError: uhyphen is not a function
cause This error typically occurs when attempting to use 'uhyphen' as a named export (e.g., `import { uhyphen } from 'uhyphen'`) when it is exclusively a default export.fixChange your import statement from `import { uhyphen } from 'uhyphen'` to `import uhyphen from 'uhyphen'` to correctly import the default export. -
Cannot find module 'uhyphen' or its corresponding type declarations.
cause This is a common TypeScript error indicating that the TypeScript compiler cannot locate the module or its type definitions. 'uhyphen' does not ship with `.d.ts` files.fixCreate a file named `uhyphen.d.ts` in your project (e.g., in a `src/types` or `types` directory) and add the content `declare module 'uhyphen';` to provide a minimal type declaration, or a more detailed one if specific function signatures are needed.
Warnings
- gotcha The 'uhyphen' library does not ship with its own TypeScript declaration files. Users working in TypeScript projects will need to provide their own ambient module declarations or use JSDoc for type inference to avoid 'Cannot find module' errors.
- gotcha As a 'micro utility' in version 0.x.y, the API is not subject to strict semantic versioning. While the library is stable for its current functionality, minor versions (e.g., 0.2.0 to 0.3.0) could potentially introduce breaking changes without a major version bump, though this is unlikely given the project's current status.
- gotcha This utility specifically converts camelCase to kebab-case. It does not handle other string transformation patterns (e.g., snake_case, PascalCase with complex rules, or sentences with spaces/special characters) and may produce unexpected or incorrect results if input strings deviate significantly from standard camelCase format.
Install
-
npm install uhyphen -
yarn add uhyphen -
pnpm add uhyphen
Imports
- uhyphen
import { uhyphen } from 'uhyphen';import uhyphen from 'uhyphen';
- uhyphen (CJS)
import uhyphen from 'uhyphen';
const uhyphen = require('uhyphen'); - uhyphen (Type)
import type { uhyphen } from 'uhyphen';import uhyphen from 'uhyphen'; // Usage with JSDoc
Quickstart
import uhyphen from 'uhyphen';
// Example 1: Basic camelCase to kebab-case transformation
const camelCaseString1 = 'XMLHttpRequest';
const kebabCaseString1 = uhyphen(camelCaseString1);
console.log(`'${camelCaseString1}' -> '${kebabCaseString1}'`);
// Expected: 'XMLHttpRequest' -> 'xml-http-request'
// Example 2: Another common camelCase pattern
const camelCaseString2 = 'myAwesomeComponent';
const kebabCaseString2 = uhyphen(camelCaseString2);
console.log(`'${camelCaseString2}' -> '${kebabCaseString2}'`);
// Expected: 'myAwesomeComponent' -> 'my-awesome-component'
// Example 3: Handling leading capitals
const camelCaseString3 = 'HTMLElement';
const kebabCaseString3 = uhyphen(camelCaseString3);
console.log(`'${camelCaseString3}' -> '${kebabCaseString3}'`);
// Expected: 'HTMLElement' -> 'html-element'
// The utility is synchronous and returns the transformed string.