or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md

index.mddocs/

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 camelCase, PascalCase, kebab-case, and space-separated strings into lowercase words connected by underscores, with robust Unicode and internationalization support.

3

4

## Package Information

5

6

- **Package Name**: lodash.snakecase

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install lodash.snakecase`

10

11

## Core Imports

12

13

```javascript

14

var snakeCase = require('lodash.snakecase');

15

```

16

17

For ES6+ environments with module support:

18

19

```javascript

20

import snakeCase from 'lodash.snakecase';

21

```

22

23

## Basic Usage

24

25

```javascript

26

var snakeCase = require('lodash.snakecase');

27

28

// Convert camelCase

29

snakeCase('fooBar');

30

// => 'foo_bar'

31

32

// Convert PascalCase

33

snakeCase('FooBar');

34

// => 'foo_bar'

35

36

// Convert kebab-case

37

snakeCase('--FOO-BAR--');

38

// => 'foo_bar'

39

40

// Convert space-separated words

41

snakeCase('Foo Bar');

42

// => 'foo_bar'

43

44

// Handle complex strings with mixed patterns

45

snakeCase('XMLHttpRequest');

46

// => 'xml_http_request'

47

```

48

49

## Capabilities

50

51

### String Case Conversion

52

53

Converts strings to snake_case format by transforming various naming conventions into lowercase words connected by underscores. Handles Unicode characters, accents, and complex string patterns through sophisticated regular expression processing.

54

55

```javascript { .api }

56

/**

57

* Converts string to snake case

58

* @param {string} [string=''] - The string to convert

59

* @returns {string} Returns the snake cased string

60

*/

61

function snakeCase(string)

62

```

63

64

**Parameter Details:**

65

66

- `string` (string, optional): The string to convert. Defaults to empty string if not provided or if null/undefined is passed.

67

68

**Return Value:**

69

70

- Returns a string in snake_case format where words are lowercase and separated by underscores.

71

72

**Behavior:**

73

74

- Converts camelCase and PascalCase by inserting underscores before uppercase letters

75

- Handles kebab-case, space-separated, and mixed delimiter strings

76

- Removes leading/trailing delimiters and normalizes multiple delimiters

77

- Processes Unicode characters and diacritical marks (converts "café" to "cafe")

78

- Handles complex patterns like acronyms and numbers within strings

79

- Returns empty string for null, undefined, or empty input

80

81

**Comprehensive Examples:**

82

83

```javascript

84

// Basic case conversions

85

snakeCase('Foo Bar');

86

// => 'foo_bar'

87

88

snakeCase('fooBar');

89

// => 'foo_bar'

90

91

snakeCase('FooBar');

92

// => 'foo_bar'

93

94

// Delimiter handling

95

snakeCase('--FOO-BAR--');

96

// => 'foo_bar'

97

98

snakeCase('foo-bar-baz');

99

// => 'foo_bar_baz'

100

101

snakeCase('foo_bar_baz');

102

// => 'foo_bar_baz'

103

104

// Complex patterns

105

snakeCase('XMLHttpRequest');

106

// => 'xml_http_request'

107

108

snakeCase('iOS App');

109

// => 'i_os_app'

110

111

snakeCase('HTML5Parser');

112

// => 'html5_parser'

113

114

// Unicode and special characters

115

snakeCase('café münü');

116

// => 'cafe_munu'

117

118

snakeCase('naïve approach');

119

// => 'naive_approach'

120

121

// Numbers and mixed content

122

snakeCase('user123ID');

123

// => 'user123_id'

124

125

snakeCase('API2Client');

126

// => 'api2_client'

127

128

// Edge cases

129

snakeCase('');

130

// => ''

131

132

snakeCase(null);

133

// => ''

134

135

snakeCase(undefined);

136

// => ''

137

138

snakeCase(' ');

139

// => ''

140

141

snakeCase('a');

142

// => 'a'

143

```

144

145

## Advanced Usage Patterns

146

147

### Database Column Naming

148

149

Convert JavaScript object properties to database-friendly column names:

150

151

```javascript

152

var snakeCase = require('lodash.snakecase');

153

154

var userData = {

155

firstName: 'John',

156

lastName: 'Doe',

157

emailAddress: 'john@example.com',

158

phoneNumber: '+1234567890'

159

};

160

161

var dbColumns = {};

162

Object.keys(userData).forEach(function(key) {

163

dbColumns[snakeCase(key)] = userData[key];

164

});

165

166

console.log(dbColumns);

167

// => {

168

// first_name: 'John',

169

// last_name: 'Doe',

170

// email_address: 'john@example.com',

171

// phone_number: '+1234567890'

172

// }

173

```

174

175

### API Key Transformation

176

177

Transform camelCase API keys to snake_case for external APIs:

178

179

```javascript

180

var apiKeys = ['userId', 'accessToken', 'refreshToken', 'clientSecret'];

181

var snakeKeys = apiKeys.map(snakeCase);

182

183

console.log(snakeKeys);

184

// => ['user_id', 'access_token', 'refresh_token', 'client_secret']

185

```

186

187

### Environment Variable Naming

188

189

Convert configuration keys to environment variable format:

190

191

```javascript

192

var configKeys = ['serverPort', 'databaseUrl', 'apiSecret', 'debugMode'];

193

var envVars = configKeys.map(function(key) {

194

return snakeCase(key).toUpperCase();

195

});

196

197

console.log(envVars);

198

// => ['SERVER_PORT', 'DATABASE_URL', 'API_SECRET', 'DEBUG_MODE']

199

```

200

201

### Form Field Processing

202

203

Process form field names for backend APIs:

204

205

```javascript

206

var formData = {

207

'userFirstName': 'Alice',

208

'userLastName': 'Smith',

209

'contactEmail': 'alice@example.com',

210

'preferredLanguage': 'en-US'

211

};

212

213

var processedData = {};

214

for (var key in formData) {

215

processedData[snakeCase(key)] = formData[key];

216

}

217

218

console.log(processedData);

219

// => {

220

// user_first_name: 'Alice',

221

// user_last_name: 'Smith',

222

// contact_email: 'alice@example.com',

223

// preferred_language: 'en-US'

224

// }

225

```

226

227

### GraphQL Field Mapping

228

229

Convert GraphQL field names to match database schema:

230

231

```javascript

232

var graphqlFields = [

233

'createdAt',

234

'updatedAt',

235

'isActive',

236

'lastLoginTime',

237

'userPreferences'

238

];

239

240

var dbFields = graphqlFields.map(snakeCase);

241

242

console.log(dbFields);

243

// => [

244

// 'created_at',

245

// 'updated_at',

246

// 'is_active',

247

// 'last_login_time',

248

// 'user_preferences'

249

// ]

250

```

251

252

## Error Handling

253

254

The `snakeCase` function is designed to be robust and never throw errors:

255

256

- Handles `null` and `undefined` inputs gracefully (returns empty string)

257

- Converts non-string inputs to strings before processing

258

- Processes invalid Unicode sequences without throwing exceptions

259

- Returns predictable output for edge cases like empty strings or whitespace-only strings

260

261

```javascript

262

// Safe with any input type

263

snakeCase(123); // => '123'

264

snakeCase(true); // => 'true'

265

snakeCase([1, 2, 3]); // => '1,2,3'

266

snakeCase({}); // => '[object Object]' -> '[object_object]'

267

```