mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
* Enable strings category methods tests * Ensure escape, pad, padEnd, padStart, trim, trimEnd, trimStart, unescape return an empty string for falsey values * Coerce value to string using toString in truncate, capitalize and case methods * Ensure createCaseFirst returns an empty string for falsey values
34 lines
789 B
JavaScript
34 lines
789 B
JavaScript
import words from './words.js'
|
|
import toString from './toString.js'
|
|
|
|
/**
|
|
* Converts `string` to
|
|
* [snake case](https://en.wikipedia.org/wiki/Snake_case).
|
|
*
|
|
* @since 3.0.0
|
|
* @category String
|
|
* @param {string} [string=''] The string to convert.
|
|
* @returns {string} Returns the snake cased string.
|
|
* @see camelCase, lowerCase, kebabCase, startCase, upperCase, upperFirst
|
|
* @example
|
|
*
|
|
* snakeCase('Foo Bar')
|
|
* // => 'foo_bar'
|
|
*
|
|
* snakeCase('fooBar')
|
|
* // => 'foo_bar'
|
|
*
|
|
* snakeCase('--FOO-BAR--')
|
|
* // => 'foo_bar'
|
|
*
|
|
* snakeCase('foo2bar')
|
|
* // => 'foo_2_bar'
|
|
*/
|
|
const snakeCase = (string) => (
|
|
words(toString(string).replace(/['\u2019]/g, '')).reduce((result, word, index) => (
|
|
result + (index ? '_' : '') + word.toLowerCase()
|
|
), '')
|
|
)
|
|
|
|
export default snakeCase
|