swap-case.js 577 B

123456789101112131415161718192021222324252627
  1. var upperCase = require('upper-case')
  2. var lowerCase = require('lower-case')
  3. /**
  4. * Swap the case of a string. Manually iterate over every character and check
  5. * instead of replacing certain characters for better unicode support.
  6. *
  7. * @param {String} str
  8. * @param {String} [locale]
  9. * @return {String}
  10. */
  11. module.exports = function (str, locale) {
  12. if (str == null) {
  13. return ''
  14. }
  15. var result = ''
  16. for (var i = 0; i < str.length; i++) {
  17. var c = str[i]
  18. var u = upperCase(c, locale)
  19. result += u === c ? lowerCase(c, locale) : u
  20. }
  21. return result
  22. }