snakeCase.js 626 B

1234567891011121314151617181920212223242526
  1. var createCompounder = require('../internal/createCompounder');
  2. /**
  3. * Converts `string` to [snake case](https://en.wikipedia.org/wiki/Snake_case).
  4. *
  5. * @static
  6. * @memberOf _
  7. * @category String
  8. * @param {string} [string=''] The string to convert.
  9. * @returns {string} Returns the snake cased string.
  10. * @example
  11. *
  12. * _.snakeCase('Foo Bar');
  13. * // => 'foo_bar'
  14. *
  15. * _.snakeCase('fooBar');
  16. * // => 'foo_bar'
  17. *
  18. * _.snakeCase('--foo-bar');
  19. * // => 'foo_bar'
  20. */
  21. var snakeCase = createCompounder(function(result, word, index) {
  22. return result + (index ? '_' : '') + word.toLowerCase();
  23. });
  24. module.exports = snakeCase;