Camel Case
Transform text into camelCase format where the first letter is lowercase and subsequent words have their first letter capitalized with no separators.
๐ Features
- Lightweight - Only ~450B minified + gzipped
- Type-safe - Full TypeScript support with comprehensive type definitions
- Zero dependencies - No external dependencies
- Tree-shakeable - ES modules support
- Universal - Works in browsers, Node.js, and serverless environments
- Well-tested - Comprehensive test suite with edge cases
- Customizable - Flexible options for advanced use cases
๐ฆ Installation
# npm
npm install text-camel-case
# yarn
yarn add text-camel-case
# pnpm
pnpm add text-camel-case
# bun
bun add text-camel-case
๐ฏ Quick Start
import { camelCase } from "text-camel-case";
console.log(camelCase("hello world")); // "helloWorld"
console.log(camelCase("user_profile_data")); // "userProfileData"
console.log(camelCase("background-color")); // "backgroundColor"
๐ Usage
ES Modules (Recommended)
import { camelCase } from "text-camel-case";
console.log(camelCase("hello world")); // "helloWorld"
CommonJS
const { camelCase } = require("text-camel-case");
console.log(camelCase("hello world")); // "helloWorld"
TypeScript
import { camelCase, camelCaseTransformMerge, Options } from "text-camel-case";
const result: string = camelCase("hello world");
console.log(result); // "helloWorld"
๐ Transformation Examples
Basic Transformations
import { camelCase } from "text-camel-case";
// From different cases
camelCase("hello world"); // "helloWorld"
camelCase("Hello World"); // "helloWorld"
camelCase("HELLO WORLD"); // "helloWorld"
camelCase("snake_case"); // "snakeCase"
camelCase("kebab-case"); // "kebabCase"
camelCase("dot.case"); // "dotCase"
camelCase("PascalCase"); // "pascalCase"
camelCase("CONSTANT_CASE"); // "constantCase"
// Complex examples
camelCase("XMLHttpRequest"); // "xmlHttpRequest"
camelCase("iPhone"); // "iPhone"
camelCase("version 1.2.3"); // "version123"
camelCase("user-profile-data"); // "userProfileData"
Advanced Options
import { camelCase, camelCaseTransformMerge } from "text-camel-case";
// Custom transform to merge numbers without separator
camelCase("version 1.2.3", {
transform: camelCaseTransformMerge,
}); // "version123"
// Custom word splitting
camelCase("XMLHttpRequest", {
splitRegexp: /([a-z])([A-Z])/g,
}); // "xmlHttpRequest"
// Custom character stripping
camelCase("hello@world.com", {
stripRegexp: /[@.]/g,
}); // "helloWorldCom"
// Custom transformation function
camelCase("api-v2-endpoint", {
transform: (word, index) => {
if (word === "api") return "API";
if (word === "v2") return "V2";
return word;
},
}); // "APIV2Endpoint"
๐ Real-World Examples
JavaScript Variables
import { camelCase } from "text-camel-case";
// API field names
camelCase("first_name"); // "firstName"
camelCase("email_address"); // "emailAddress"
camelCase("created_at"); // "createdAt"
camelCase("user_id"); // "userId"
camelCase("access_token"); // "accessToken"
CSS Properties to JavaScript
import { camelCase } from "text-camel-case";
// CSS properties
camelCase("background-color"); // "backgroundColor"
camelCase("font-family"); // "fontFamily"
camelCase("border-radius"); // "borderRadius"
camelCase("margin-top"); // "marginTop"
camelCase("z-index"); // "zIndex"
Database Columns to Object Properties
import { camelCase } from "text-camel-case";
// Database columns
camelCase("user_profile"); // "userProfile"
camelCase("last_login_date"); // "lastLoginDate"
camelCase("is_active"); // "isActive"
camelCase("created_by_user_id"); // "createdByUserId"
Object Key Transformation
import { camelCase } from "text-camel-case";
// Transform object keys from snake_case to camelCase
const dbUser = {
first_name: "John",
last_name: "Doe",
email_address: "john@example.com",
created_at: "2023-01-01",
};
const jsUser = Object.fromEntries(
Object.entries(dbUser).map(([key, value]) => [camelCase(key), value]),
);
console.log(jsUser);
// {
// firstName: "John",
// lastName: "Doe",
// emailAddress: "john@example.com",
// createdAt: "2023-01-01"
// }
๐ API Reference
camelCase(input, options?)
Converts a string to camelCase.
Parameters
input
(string
): The string to convertoptions
(Options
, optional): Configuration options
Returns
string
: The camelCase formatted string
Options
interface Options {
// Custom transform function for word processing
transform?: (word: string, index: number, words: string[]) => string;
// Regex to strip characters before processing
stripRegexp?: RegExp;
// Custom split function
split?: (value: string) => string[];
}
camelCaseTransformMerge
A transform function that merges numeric characters without separation.
import { camelCase, camelCaseTransformMerge } from "text-camel-case";
camelCase("version 1.2.3", { transform: camelCaseTransformMerge }); // "version123"
๐ง Advanced Configuration
Custom Word Splitting
import { camelCase } from "text-camel-case";
// Split on specific patterns
camelCase("XMLHttpRequest", {
splitRegexp: /([a-z])([A-Z])/g,
}); // "xmlHttpRequest"
// Split on numbers
camelCase("user123data", {
splitRegexp: /([a-zA-Z])(\d)/g,
}); // "user123Data"
Custom Character Stripping
import { camelCase } from "text-camel-case";
// Strip specific characters
camelCase("hello@world.com", {
stripRegexp: /[@.]/g,
}); // "helloWorldCom"
// Strip all non-alphanumeric
camelCase("hello!@#world", {
stripRegexp: /[^a-zA-Z0-9]/g,
}); // "helloWorld"
Custom Transform Functions
import { camelCase } from "text-camel-case";
// Preserve acronyms
camelCase("xml-http-request", {
transform: (word, index) => {
const acronyms = ["xml", "http", "api", "url"];
if (acronyms.includes(word.toLowerCase())) {
return word.toUpperCase();
}
return word;
},
}); // "XMLHTTPRequest"
// Custom business logic
camelCase("user-v2-api", {
transform: (word, index) => {
if (word === "v2") return "V2";
if (word === "api") return "API";
return word;
},
}); // "userV2API"
๐ Bundle Size
This package is optimized for minimal bundle size:
- Minified: ~450B
- Gzipped: ~250B
- Tree-shakeable: Yes
- Side effects: None
๐ Browser Support
- Modern browsers: ES2015+ (Chrome 51+, Firefox 54+, Safari 10+)
- Node.js: 12+
- TypeScript: 4.0+
- Bundle formats: UMD, ESM, CommonJS
๐งช Testing
# Run tests
pnpm test
# Run tests in watch mode
pnpm test --watch
# Run tests with coverage
pnpm test --coverage
# Type checking
pnpm typecheck
# Linting
pnpm lint
๐ Related Packages
text-pascal-case
- Convert to PascalCasetext-snake-case
- Convert to snake_casetext-kebab-case
- Convert to kebab-casetext-title-case
- Convert to Title Casetext-case
- All case transformations in one package
๐ License
๐ค Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
๐ Support
- ๐ง Email: selikhov.dmitrey@gmail.com
- ๐ Issues: GitHub Issues
- ๐ฌ Discussions: GitHub Discussions
- ๐ Documentation: Full Documentation
Made with โค๏ธ by Dmitry Selikhov