← Back to utilities
string

snake-case

Converts a string to snake_case. Transforms a string by converting it to lowercase and replacing spaces, hyphens, and camelCase boundaries with underscores. Commonly used for database column names, Python variables, and configuration keys.

Installation

npx fragmen add string/snake-case

Source Code

/**
 * Converts a string to snake_case.
 *
 * Transforms a string by converting it to lowercase and replacing spaces, hyphens,
 * and camelCase boundaries with underscores. Commonly used for database column names,
 * Python variables, and configuration keys.
 *
 * @tags pure, string-manipulation, formatting
 * @param {string} str The string to convert
 * @returns {string} The snake_case version of the string
 *
 * @example
 * ```typescript
 * snakeCase('Hello World'); // 'hello_world'
 * snakeCase('firstName'); // 'first_name'
 * snakeCase('XMLHttpRequest'); // 'xml_http_request'
 * snakeCase('kebab-case-string'); // 'kebab_case_string'
 * snakeCase('  multiple   spaces  '); // 'multiple_spaces'
 * snakeCase('already_snake_case'); // 'already_snake_case'
 * snakeCase(''); // ''
 * ```
 */
export function snakeCase(str: string): string {
  if (typeof str !== 'string') {
    return '';
  }

  return (
    str
      .trim()
      // Handle consecutive uppercase letters followed by lowercase: XMLHttp -> XML_Http
      .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
      // Handle lowercase followed by uppercase: camelCase -> camel_Case
      .replace(/([a-z])([A-Z])/g, '$1_$2')
      // Handle digit followed by letter: version2Update -> version2_Update
      .replace(/([0-9])([A-Z])/g, '$1_$2')
      // Replace hyphens with underscores
      .replace(/-/g, '_')
      // Replace one or more spaces with single underscore
      .replace(/\s+/g, '_')
      // Convert to lowercase
      .toLowerCase()
      // Remove multiple consecutive underscores
      .replace(/_+/g, '_')
      // Remove leading/trailing underscores
      .replace(/^_+|_+$/g, '')
  );
}

Examples

snakeCase('Hello World'); // 'hello_world'
snakeCase('firstName'); // 'first_name'
snakeCase('XMLHttpRequest'); // 'xml_http_request'
snakeCase('kebab-case-string'); // 'kebab_case_string'
snakeCase('  multiple   spaces  '); // 'multiple_spaces'
snakeCase('already_snake_case'); // 'already_snake_case'
snakeCase(''); // ''

Related Utilities

camel-case

string

Converts a string to camelCase. Transforms a string by removing spaces, underscores, and hyphens, then capitalizing the first letter of each word except the first one. Commonly used for JavaScript variable names, object properties, and function names.

#pure#string-manipulation#formatting

capitalize

string

Capitalizes the first letter of a string. Converts the first character to uppercase while leaving the rest unchanged. Safely handles edge cases like empty strings and non-string inputs.

#pure#string-manipulation#formatting

kebab-case

string

Converts a string to kebab-case. Transforms a string by converting it to lowercase and replacing spaces, underscores, and camelCase boundaries with hyphens. Useful for creating URL slugs, CSS class names, and file names.

#pure#string-manipulation#formatting

pad-end

string

Pads the end of a string with another string until it reaches the target length. If padString is '', the original string is returned (mirrors native behavior).

#pure#string-manipulation#formatting

pad-start

string

Pads the start of a string with another string until it reaches the target length. If padString is '', the original string is returned (mirrors native behavior).

#pure#string-manipulation#formatting

pascal-case

string

Converts a string to PascalCase. Transforms a string by removing spaces, underscores, and hyphens, then capitalizing the first letter of each word including the first one. Commonly used for class names, interface names, type names, and component names.

#pure#string-manipulation#formatting

Quick Actions

Estimated size:1.63 KB

Tags

Parameters

strstring

The string to convert

Returns

string

The snake_case version of the string