matches.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.fuzzyMatches = fuzzyMatches;
  6. exports.matches = matches;
  7. exports.getDefaultNormalizer = getDefaultNormalizer;
  8. exports.makeNormalizer = makeNormalizer;
  9. function assertNotNullOrUndefined(matcher) {
  10. if (matcher === null || matcher === undefined) {
  11. throw new Error( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- implicitly converting `T` to `string`
  12. `It looks like ${matcher} was passed instead of a matcher. Did you do something like getByText(${matcher})?`);
  13. }
  14. }
  15. function fuzzyMatches(textToMatch, node, matcher, normalizer) {
  16. if (typeof textToMatch !== 'string') {
  17. return false;
  18. }
  19. assertNotNullOrUndefined(matcher);
  20. const normalizedText = normalizer(textToMatch);
  21. if (typeof matcher === 'string' || typeof matcher === 'number') {
  22. return normalizedText.toLowerCase().includes(matcher.toString().toLowerCase());
  23. } else if (typeof matcher === 'function') {
  24. return matcher(normalizedText, node);
  25. } else {
  26. return matcher.test(normalizedText);
  27. }
  28. }
  29. function matches(textToMatch, node, matcher, normalizer) {
  30. if (typeof textToMatch !== 'string') {
  31. return false;
  32. }
  33. assertNotNullOrUndefined(matcher);
  34. const normalizedText = normalizer(textToMatch);
  35. if (matcher instanceof Function) {
  36. return matcher(normalizedText, node);
  37. } else if (matcher instanceof RegExp) {
  38. return matcher.test(normalizedText);
  39. } else {
  40. return normalizedText === String(matcher);
  41. }
  42. }
  43. function getDefaultNormalizer({
  44. trim = true,
  45. collapseWhitespace = true
  46. } = {}) {
  47. return text => {
  48. let normalizedText = text;
  49. normalizedText = trim ? normalizedText.trim() : normalizedText;
  50. normalizedText = collapseWhitespace ? normalizedText.replace(/\s+/g, ' ') : normalizedText;
  51. return normalizedText;
  52. };
  53. }
  54. /**
  55. * Constructs a normalizer to pass to functions in matches.js
  56. * @param {boolean|undefined} trim The user-specified value for `trim`, without
  57. * any defaulting having been applied
  58. * @param {boolean|undefined} collapseWhitespace The user-specified value for
  59. * `collapseWhitespace`, without any defaulting having been applied
  60. * @param {Function|undefined} normalizer The user-specified normalizer
  61. * @returns {Function} A normalizer
  62. */
  63. function makeNormalizer({
  64. trim,
  65. collapseWhitespace,
  66. normalizer
  67. }) {
  68. if (normalizer) {
  69. // User has specified a custom normalizer
  70. if (typeof trim !== 'undefined' || typeof collapseWhitespace !== 'undefined') {
  71. // They've also specified a value for trim or collapseWhitespace
  72. throw new Error('trim and collapseWhitespace are not supported with a normalizer. ' + 'If you want to use the default trim and collapseWhitespace logic in your normalizer, ' + 'use "getDefaultNormalizer({trim, collapseWhitespace})" and compose that into your normalizer');
  73. }
  74. return normalizer;
  75. } else {
  76. // No custom normalizer specified. Just use default.
  77. return getDefaultNormalizer({
  78. trim,
  79. collapseWhitespace
  80. });
  81. }
  82. }