Aller au contenu principal

snakeCase()

snakeCase(str): string

Converts a string to snake_case.

remarque

Handles camelCase, PascalCase, acronyms, kebab-case, and spaces.


Parameters

str: string

The string to convert.


Returns: string

The string in snake_case.


Since

2.0.0


Performance

O(n) where n is string length. Single regex pass with match + join.


Also known as

snake (Radashi) · snakeCase (Lodash, es-toolkit, Modern Dash) · toSnakeCase (Remeda) · ❌ (Ramda, Effect, Antfu)


Example

snakeCase('helloWorld');     // => 'hello_world'
snakeCase('XMLHttpRequest'); // => 'xml_http_request'
snakeCase('hello-world'); // => 'hello_world'
snakeCase('Hello World'); // => 'hello_world'
snakeCase('--foo--bar--'); // => 'foo_bar'

How it works?

Converts a string to snake_case format. All lowercase, words separated by underscores.

Conversion Examples

InputOutput
helloWorldhello_world
XMLHttpRequestxml_http_request
hello-worldhello_world
Hello Worldhello_world

Process


Use Cases

Format database columns 📌

Convert strings to snake_case for database column names or Python API compatibility. Critical for backend integration.

const column = snakeCase('userId'); // 'user_id'

Convert payload keys before sending to a Python/Ruby API

Transform camelCase JavaScript keys into snake_case before sending to a backend API. The reverse of the camelCase normalization — essential for full-stack interop.

const payload = {
userId: 42,
firstName: "Alice",
isActive: true,
};

// Combined with mapKeys for full object transformation
const apiPayload = mapKeys(payload, (_, key) => snakeCase(key));
// => { user_id: 42, first_name: "Alice", is_active: true }

await fetch("/api/users", {
method: "POST",
body: JSON.stringify(apiPayload),
});