Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

transformCamelSnakeCase

Converts between camelCase and snake_case formats.

TypeScript
Copied!
1/**
2 * Converts between camelCase and snake_case formats.
3 *
4 * @param str - The input string.
5 * @param direction - Direction of transformation: 'camelToSnake' or 'snakeToCamel'.
6 * @returns Transformed string in the desired format.
7 */
8export function transformCamelSnakeCase(
9  str: string,
10  direction: 'camelToSnake' | 'snakeToCamel'
11): string {
12  if (direction === 'camelToSnake') {
13    return str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
14  }
15
16  if (direction === 'snakeToCamel') {
17    return str.replace(/_([a-z])/g, (_, char) => char.toUpperCase());
18  }
19
20  throw new Error('Invalid direction: use "camelToSnake" or "snakeToCamel"');
21}
  • Supports Two-Way Transformation

    Handles both camelCase → snake_case and snake_case → camelCase within a single utility.

  • Readable and Maintainable

    Clear branching logic improves maintainability and readability compared to using multiple separate functions.

  • Lightweight and Performant

    Uses optimized regular expressions without dependencies, suitable for performance-sensitive scenarios.

  • Fail-Safe Design

    Throws an explicit error for invalid direction values, improving developer feedback and debugging.

Tests | Examples

TypeScript
Copied!
1test('transformCamelSnakeCase - camel to snake', () => {
2  expect(transformCamelSnakeCase('myVariableName', 'camelToSnake')).toBe('my_variable_name');
3});
4
5test('transformCamelSnakeCase - snake to camel', () => {
6  expect(transformCamelSnakeCase('my_variable_name', 'snakeToCamel')).toBe('myVariableName');
7});
8
9test('transformCamelSnakeCase - empty string', () => {
10  expect(transformCamelSnakeCase('', 'camelToSnake')).toBe('');
11  expect(transformCamelSnakeCase('', 'snakeToCamel')).toBe('');
12});
13
14test('transformCamelSnakeCase - no uppercase in camel', () => {
15  expect(transformCamelSnakeCase('simple', 'camelToSnake')).toBe('simple');
16});
17
18test('transformCamelSnakeCase - invalid direction throws', () => {
19  expect(() => transformCamelSnakeCase('test', 'wrong' as any)).toThrow(
20    'Invalid direction: use "camelToSnake" or "snakeToCamel"'
21  );
22});

Common Use Cases

  • API Data Normalization

    Convert snake_case API responses to camelCase for use in JavaScript applications (and vice versa when sending data).

  • Database-to-JS Mapping

    Useful for ORM layers where database column names are in snake_case and JavaScript uses camelCase.

  • Code Generation Tools

    Helps generate consistent identifiers across layers with different naming conventions (e.g., back-end models to front-end props).

  • Linter / Refactoring Utilities

    Can be used in code transformation tools or custom linters to enforce naming conventions automatically.

  • Form Field Mapping

    Translate between HTML form inputs (snake_case) and internal JavaScript object keys (camelCase).

Codebase: Utilities -> Strings -> transformCamelSnakeCase | Yevhen Klymentiev