---
title: "snakeCase"
description: "Convert any common string casing to snake_case - handles camelCase, PascalCase, kebab-case, spaces and consecutive capitals."
package: "@maz-ui/utils"
url: "https://maz-ui.com/ecosystem/utils/snake-case"
---

# snakeCase

> Convert any common string casing to snake_case - handles camelCase, PascalCase, kebab-case, spaces and consecutive capitals.

- Package: `@maz-ui/utils`
- Import: `import { snakeCase } from '@maz-ui/utils/helpers/snakeCase'`
- Web: https://maz-ui.com/ecosystem/utils/snake-case

## Usage

```ts
import { snakeCase } from '@maz-ui/utils'

snakeCase('myComponent') // → 'my_component'
snakeCase('MyComponent') // → 'my_component'
snakeCase('XMLParser') // → 'xml_parser'
snakeCase('hello-world') // → 'hello_world'
snakeCase('hello world') // → 'hello_world'
```

## API

```ts
function snakeCase(str: string): string
```

| Parameter | Type     | Description           |
| --------- | -------- | --------------------- |
| `str`     | `string` | The string to convert |

## Notes

- Consecutive uppercase letters are handled (e.g. `XMLParser` → `xml_parser`).
- Leading and trailing underscores are stripped.
- Multiple separators collapse into a single underscore.
