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

transformSnakeKebabCase

Converts between snake_case and kebab-case formats.

TypeScript
Copied!
1/**
2 * Converts between snake_case and kebab-case formats.
3 *
4 * @param str - The input string.
5 * @param direction - Direction of transformation: 'snakeToKebab' or 'kebabToSnake'.
6 * @returns Transformed string in the desired format.
7 */
8export function transformSnakeKebabCase(
9  str: string,
10  direction: 'snakeToKebab' | 'kebabToSnake'
11): string {
12  if (direction === 'snakeToKebab') {
13    return str.replace(/_/g, '-');
14  }
15
16  if (direction === 'kebabToSnake') {
17    return str.replace(/-/g, '_');
18  }
19
20  throw new Error('Invalid direction: use "snakeToKebab" or "kebabToSnake"');
21}
  • Bi-Directional Conversion

    Handles both snake_case to kebab-case and the reverse using a unified interface.

  • Minimal and Fast

    Utilizes simple and efficient String.replace() operations for optimal performance.

  • Easy to Integrate

    Can be used seamlessly in various naming standardization tasks without external dependencies.

  • Fail-Fast Error Handling

    Explicit error on invalid direction input improves reliability and prevents silent bugs.

Tests | Examples

TypeScript
Copied!
1test('transformSnakeKebabCase - snake to kebab', () => {
2  expect(transformSnakeKebabCase('some_variable_name', 'snakeToKebab')).toBe('some-variable-name');
3});
4
5test('transformSnakeKebabCase - kebab to snake', () => {
6  expect(transformSnakeKebabCase('some-variable-name', 'kebabToSnake')).toBe('some_variable_name');
7});
8
9test('transformSnakeKebabCase - no separators', () => {
10  expect(transformSnakeKebabCase('simple', 'snakeToKebab')).toBe('simple');
11  expect(transformSnakeKebabCase('simple', 'kebabToSnake')).toBe('simple');
12});
13
14test('transformSnakeKebabCase - empty string', () => {
15  expect(transformSnakeKebabCase('', 'snakeToKebab')).toBe('');
16});
17
18test('transformSnakeKebabCase - invalid direction throws', () => {
19  expect(() => transformSnakeKebabCase('x', 'invalid' as any)).toThrow(
20    'Invalid direction: use "snakeToKebab" or "kebabToSnake"'
21  );
22});

Common Use Cases

  • CSS Class Normalization

    Convert snake_case naming from backend config files to kebab-case for use in HTML/CSS.

  • Build Tools and Compilers

    Useful in build pipelines where string transformations are needed for consistency across file or variable naming schemes.

  • URL or Slug Generation

    When converting API or database keys (snake_case) to frontend-friendly URLs or slugs (kebab-case).

  • Code Migration

    Assist in transforming legacy codebases from one naming style to another with minimal overhead.

  • Templating Systems

    Adapt dynamic keys in templates depending on naming convention of the rendering engine.

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