toCamelCase

toCamelCase normalizes input text and converts it into camelCase.

It treats spaces, underscores, and hyphens as word boundaries, lowercases all tokens, collapses repeated separators, and removes leading/trailing separators.

When to use

  • Converting user-entered labels to JavaScript-friendly keys.
  • Normalizing API field names before object mapping.
  • Turning slug-like text into camelCase identifiers.

Behavior notes

  • Empty input returns an empty string.
  • Separator-only input returns an empty string.
  • Numeric tokens are preserved.
  • All-uppercase and mixed-case input is normalized to lowercase-first camelCase.

Examples

toCamelCase('customer_success-manager');
// "customerSuccessManager"
toCamelCase('  Q4-2026 product roadmap  ');
// "q42026ProductRoadmap"

Usage

import { toCamelCase } from '@branditdev/utils';

const profileField = 'customer_success-manager';
const roadmapLabel = '  Q4-2026 product roadmap  ';
const mixedCaseLabel = 'API RESPONSE-TIME_ms';

export const toCamelCaseDemo = {
  profileKey: toCamelCase(profileField),
  roadmapKey: toCamelCase(roadmapLabel),
  mixedCaseKey: toCamelCase(mixedCaseLabel),
};

API

toCamelCase(value): string

Defined in: string/toCamelCase/toCamelCase.ts:21

Converts text into camelCase using spaces, underscores, and hyphens as separators.

The function lowercases all tokens, removes extra separators, and joins subsequent tokens with an uppercase leading character.

Parameters

NameTypeOptionalDefaultDescription
valuestringno-The input text to convert.

Returns

string

A camelCased string with normalized separators.

Source

export function toCamelCase(value: string): string {
  if (value.length === 0) return value;

  const tokens = value
    .toLowerCase()
    .split(/[\s_-]+/)
    .filter(Boolean);

  if (tokens.length === 0) return '';

  return tokens
    .map((token, index) =>
      index === 0 ? token : token.charAt(0).toUpperCase() + token.slice(1)
    )
    .join('');
}