deburr.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var deburrLetter = require('./_deburrLetter'),
  2. toString = require('./toString');
  3. /** Used to match latin-1 supplementary letters (excluding mathematical operators). */
  4. var reLatin1 = /[\xc0-\xd6\xd8-\xde\xdf-\xf6\xf8-\xff]/g;
  5. /** Used to compose unicode character classes. */
  6. var rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23',
  7. rsComboSymbolsRange = '\\u20d0-\\u20f0';
  8. /** Used to compose unicode capture groups. */
  9. var rsCombo = '[' + rsComboMarksRange + rsComboSymbolsRange + ']';
  10. /**
  11. * Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks) and
  12. * [combining diacritical marks for symbols](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks_for_Symbols).
  13. */
  14. var reComboMark = RegExp(rsCombo, 'g');
  15. /**
  16. * Deburrs `string` by converting
  17. * [latin-1 supplementary letters](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)#Character_table)
  18. * to basic latin letters and removing
  19. * [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks).
  20. *
  21. * @static
  22. * @memberOf _
  23. * @since 3.0.0
  24. * @category String
  25. * @param {string} [string=''] The string to deburr.
  26. * @returns {string} Returns the deburred string.
  27. * @example
  28. *
  29. * _.deburr('déjà vu');
  30. * // => 'deja vu'
  31. */
  32. function deburr(string) {
  33. string = toString(string);
  34. return string && string.replace(reLatin1, deburrLetter).replace(reComboMark, '');
  35. }
  36. module.exports = deburr;