Transform a string between `camelCase`, `PascalCase`, `Capital Case`, `snake_case`, `kebab-case`, `CONSTANT_CASE` and others
The change-case package is a comprehensive string transformation library that converts text between 12+ different case formats including camelCase, snake_case, kebab-case, PascalCase, CONSTANT_CASE, and more exotic variants like pascalSnakeCase and trainCase. With over 16 million weekly downloads, it's become the de facto standard for case conversion in JavaScript and TypeScript projects.
The package solves a deceptively complex problem: reliably splitting strings into words and recombining them with different delimiters and capitalization rules. While simple cases like converting "hello world" to "helloWorld" seem trivial, real-world scenarios involve parsing mixed formats ("XMLHttpRequest"), handling locale-specific casing rules, preserving numbers and special characters, and transforming deeply nested object keys. The library provides a battle-tested word segmentation algorithm that handles these edge cases consistently.
Developers working with APIs, configuration files, database schemas, and code generation tools rely on change-case to normalize data between systems with different naming conventions. Frontend teams use it to transform API responses from snake_case backends into camelCase JavaScript objects. CLI tool builders convert kebab-case command flags into application variables. The package's monorepo structure keeps each transformation function tree-shakeable, ensuring minimal bundle impact despite supporting a dozen case formats.
Unlike heavyweight utility libraries that bundle case conversion with unrelated functions, change-case focuses exclusively on string transformation with locale awareness and customizable delimiters. Each function accepts options for fine-tuning behavior, and the separate keys module enables recursive transformation of object properties without manual traversal logic.
import { camelCase, snakeCase, pascalCase } from 'change-case';
import { camelCase as camelKeys } from 'change-case/keys';
// Basic string transformations
const apiField = 'user_created_at';
console.log(camelCase(apiField)); // 'userCreatedAt'
console.log(pascalCase(apiField)); // 'UserCreatedAt'
// Handle mixed formats with custom options
const envVar = 'DATABASE_CONNECTION_URL';
console.log(camelCase(envVar, { locale: 'en-US' })); // 'databaseConnectionUrl'
// Transform object keys recursively
const apiResponse = {
user_name: 'alice',
account_settings: {
email_verified: true,
two_factor_enabled: false
},
created_at: '2024-01-15'
};
const jsObject = camelKeys(apiResponse, { depth: 2 });
console.log(jsObject);
// {
// userName: 'alice',
// accountSettings: {
// emailVerified: true,
// twoFactorEnabled: false
// },
// createdAt: '2024-01-15'
// }
// Generate URL slugs
const title = 'Understanding JavaScript Promises';
const slug = snakeCase(title).replace(/_/g, '-'); // or use kebabCase directly
console.log(slug); // 'understanding-javascript-promises'API response normalization: Backend APIs often return JSON with snake_case keys (common in Python/Ruby services), while JavaScript conventions favor camelCase. Use change-case to transform entire response objects recursively, converting { user_name: 'alice', created_at: '2024' } to { userName: 'alice', createdAt: '2024' } automatically.
Environment variable mapping: Reading process.env variables (typically CONSTANT_CASE like DATABASE_URL) into application config objects with camelCase properties. Convert SMTP_HOST_NAME to smtpHostName for cleaner code access without manual string manipulation.
Code generation and templating: Build tools that generate TypeScript interfaces from OpenAPI schemas or database tables need reliable case conversion. Transform database column names like order_total_amount into TypeScript property names orderTotalAmount while generating class definitions or GraphQL resolvers.
URL slug generation: Convert article titles or product names into URL-safe kebab-case slugs. Transform "10 Best Practices for React Hooks" into "10-best-practices-for-react-hooks" for SEO-friendly routes, handling spaces, punctuation, and mixed case input consistently.
Configuration file translation: Migrate between config formats that use different conventions (YAML snake_case to JSON camelCase, or environment files to JavaScript modules). Automate conversion of legacy .env files with CONSTANT_CASE keys into modern config objects with camelCase or nested structures.
npm install change-casepnpm add change-casebun add change-case