no-case.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var lowerCase = require('lower-case')
  2. var NON_WORD_REGEXP = require('./vendor/non-word-regexp')
  3. var CAMEL_CASE_REGEXP = require('./vendor/camel-case-regexp')
  4. var CAMEL_CASE_UPPER_REGEXP = require('./vendor/camel-case-upper-regexp')
  5. /**
  6. * Sentence case a string.
  7. *
  8. * @param {string} str
  9. * @param {string} locale
  10. * @param {string} replacement
  11. * @return {string}
  12. */
  13. module.exports = function (str, locale, replacement) {
  14. if (str == null) {
  15. return ''
  16. }
  17. replacement = typeof replacement !== 'string' ? ' ' : replacement
  18. function replace (match, index, value) {
  19. if (index === 0 || index === (value.length - match.length)) {
  20. return ''
  21. }
  22. return replacement
  23. }
  24. str = String(str)
  25. // Support camel case ("camelCase" -> "camel Case").
  26. .replace(CAMEL_CASE_REGEXP, '$1 $2')
  27. // Support odd camel case ("CAMELCase" -> "CAMEL Case").
  28. .replace(CAMEL_CASE_UPPER_REGEXP, '$1 $2')
  29. // Remove all non-word characters and replace with a single space.
  30. .replace(NON_WORD_REGEXP, replace)
  31. // Lower case the entire string.
  32. return lowerCase(str, locale)
  33. }