has-unicode.js 1.0 KB

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