is-human-interpretable.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import removeUnicode from './remove-unicode';
  2. import sanitize from './sanitize';
  3. /**
  4. * Determines if a given text is human friendly and interpretable
  5. *
  6. * @method isHumanInterpretable
  7. * @memberof axe.commons.text
  8. * @instance
  9. * @param {String} str text to be validated
  10. * @returns {Number} Between 0 and 1, (0 -> not interpretable, 1 -> interpretable)
  11. */
  12. function isHumanInterpretable(str) {
  13. /**
  14. * Steps:
  15. * 1) Check for single character edge cases
  16. * a) handle if character is alphanumeric & within the given icon mapping
  17. * eg: x (close), i (info)
  18. *
  19. * 3) handle unicode from astral (non bilingual multi plane) unicode, emoji & punctuations
  20. * eg: Windings font
  21. * eg: '💪'
  22. * eg: I saw a shooting 💫
  23. * eg: ? (help), > (next arrow), < (back arrow), need help ?
  24. */
  25. if (!str.length) {
  26. return 0;
  27. }
  28. // Step 1
  29. const alphaNumericIconMap = [
  30. 'x', // close
  31. 'i' // info
  32. ];
  33. // Step 1a
  34. if (alphaNumericIconMap.includes(str)) {
  35. return 0;
  36. }
  37. // Step 2
  38. const noUnicodeStr = removeUnicode(str, {
  39. emoji: true,
  40. nonBmp: true,
  41. punctuations: true
  42. });
  43. if (!sanitize(noUnicodeStr)) {
  44. return 0;
  45. }
  46. return 1;
  47. }
  48. export default isHumanInterpretable;