0
# lodash.snakecase
1
2
The lodash method `_.snakeCase` exported as a standalone Node.js module for converting strings to snake_case format. This utility transforms various string formats including camelCase, PascalCase, kebab-case, and space-separated words into consistent snake_case output with lowercase words separated by underscores.
3
4
## Package Information
5
6
- **Package Name**: lodash.snakecase
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.snakecase`
10
- **Version**: 3.1.1
11
- **License**: MIT
12
13
## Core Imports
14
15
```javascript
16
var snakeCase = require('lodash.snakecase');
17
```
18
19
For ES6 modules (with transpiler):
20
21
```javascript
22
import snakeCase from 'lodash.snakecase';
23
```
24
25
## Basic Usage
26
27
```javascript
28
var snakeCase = require('lodash.snakecase');
29
30
// Convert camelCase
31
console.log(snakeCase('fooBar'));
32
// => 'foo_bar'
33
34
// Convert PascalCase
35
console.log(snakeCase('FooBar'));
36
// => 'foo_bar'
37
38
// Convert kebab-case
39
console.log(snakeCase('--foo-bar'));
40
// => 'foo_bar'
41
42
// Convert space-separated words
43
console.log(snakeCase('Foo Bar'));
44
// => 'foo_bar'
45
46
// Handle empty input
47
console.log(snakeCase(''));
48
// => ''
49
```
50
51
## Capabilities
52
53
### String Conversion
54
55
Converts strings to snake_case format by extracting words and joining them with underscores.
56
57
```javascript { .api }
58
/**
59
* Converts string to snake case format
60
* @param {string} [string=''] The string to convert
61
* @returns {string} Returns the snake cased string
62
*/
63
function snakeCase(string)
64
```
65
66
**Parameters:**
67
- `string` (string, optional): The string to convert. Defaults to empty string if not provided.
68
69
**Returns:**
70
- (string): The converted string in snake_case format with lowercase words separated by underscores.
71
72
**Behavior:**
73
- Removes accents and diacritical marks from characters
74
- Extracts words from the input string (handles camelCase, PascalCase, kebab-case, spaces, punctuation)
75
- Converts all words to lowercase
76
- Joins words with underscore separators
77
- Returns empty string for null, undefined, or empty input
78
79
**Usage Examples:**
80
81
```javascript
82
var snakeCase = require('lodash.snakecase');
83
84
// Various input formats
85
snakeCase('Foo Bar'); // => 'foo_bar'
86
snakeCase('fooBar'); // => 'foo_bar'
87
snakeCase('FooBar'); // => 'foo_bar'
88
snakeCase('--FOO-BAR--'); // => 'foo_bar'
89
snakeCase('foo2bar'); // => 'foo_2_bar'
90
snakeCase('foo-bar_baz'); // => 'foo_bar_baz'
91
92
// Special characters and accents
93
snakeCase('résumé'); // => 'resume'
94
snakeCase('café-société'); // => 'cafe_societe'
95
96
// Numbers and mixed content
97
snakeCase('version2Beta'); // => 'version_2_beta'
98
snakeCase('XMLHttpRequest'); // => 'xml_http_request'
99
100
// Edge cases
101
snakeCase(''); // => ''
102
snakeCase(null); // => ''
103
snakeCase(undefined); // => ''
104
```
105
106
## Implementation Details
107
108
The function uses lodash helper utilities for processing:
109
- `lodash.deburr`: Removes accents and diacritical marks from characters
110
- `lodash.words`: Extracts words from strings using Unicode word boundaries
111
112
The implementation follows a composable pattern where the input string is:
113
1. Processed through `deburr` to normalize accented characters
114
2. Split into words using the `words` function
115
3. Reduced to a single string with underscores between lowercase words