remove-unicode.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import {
  2. getUnicodeNonBmpRegExp,
  3. getSupplementaryPrivateUseRegExp,
  4. getPunctuationRegExp
  5. } from './unicode.js';
  6. import emojiRegexText from 'emoji-regex';
  7. /**
  8. * Remove specified type(s) unicode characters
  9. *
  10. * @method removeUnicode
  11. * @memberof axe.commons.text
  12. * @instance
  13. * @param {String} str string to operate on
  14. * @param {Object} options config containing which unicode character sets to remove
  15. * @property {Boolean} options.emoji remove emoji unicode
  16. * @property {Boolean} options.nonBmp remove nonBmp unicode
  17. * @property {Boolean} options.punctuations remove punctuations unicode
  18. * @returns {String}
  19. */
  20. function removeUnicode(str, options) {
  21. const { emoji, nonBmp, punctuations } = options;
  22. if (emoji) {
  23. str = str.replace(emojiRegexText(), '');
  24. }
  25. if (nonBmp) {
  26. str = str.replace(getUnicodeNonBmpRegExp(), '');
  27. str = str.replace(getSupplementaryPrivateUseRegExp(), '');
  28. }
  29. if (punctuations) {
  30. str = str.replace(getPunctuationRegExp(), '');
  31. }
  32. return str;
  33. }
  34. export default removeUnicode;