Skip to the content.

Camel Case

NPM version NPM downloads Bundle size License: MIT TypeScript

Transform text into camelCase format where the first letter is lowercase and subsequent words have their first letter capitalized with no separators.

๐Ÿš€ Features

๐Ÿ“ฆ 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

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

Returns

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:

๐ŸŒ Browser Support

๐Ÿงช 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

๐Ÿ“œ License

MIT ยฉ Dmitry Selikhov

๐Ÿค Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ†˜ Support


Made with โค๏ธ by Dmitry Selikhov