1
- function assertNotNullOrUndefined ( matcher ) {
2
- if ( matcher == null ) {
1
+ import { ARIARole } from 'aria-query'
2
+ import { Nullish } from './types'
3
+
4
+ export type ByRoleMatcher = ARIARole | MatcherFunction | { }
5
+
6
+ export type Matcher = MatcherFunction | RegExp | string
7
+
8
+ export type MatcherFunction = (
9
+ content : string ,
10
+ element : Nullish < Element > ,
11
+ ) => boolean
12
+
13
+ export type NormalizerFn = ( text : string ) => string
14
+
15
+ export interface DefaultNormalizerOptions {
16
+ trim ?: boolean
17
+ collapseWhitespace ?: boolean
18
+ }
19
+
20
+ export interface MatcherOptions {
21
+ exact ?: boolean
22
+ /** Use normalizer with getDefaultNormalizer instead */
23
+ trim ?: boolean
24
+ /** Use normalizer with getDefaultNormalizer instead */
25
+ collapseWhitespace ?: boolean
26
+ normalizer ?: NormalizerFn
27
+ /** suppress suggestions for a specific query */
28
+ suggest ?: boolean
29
+ }
30
+
31
+ export type NormalizerOptions = DefaultNormalizerOptions & {
32
+ normalizer ?: NormalizerFn
33
+ }
34
+
35
+ function assertNotNullOrUndefined < T > (
36
+ matcher : T ,
37
+ ) : asserts matcher is NonNullable < T > {
38
+ if ( matcher === null || matcher === undefined ) {
3
39
throw new Error (
40
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
4
41
`It looks like ${ matcher } was passed instead of a matcher. Did you do something like getByText(${ matcher } )?` ,
5
42
)
6
43
}
7
44
}
8
45
9
- function fuzzyMatches ( textToMatch , node , matcher , normalizer ) {
46
+ function fuzzyMatches (
47
+ textToMatch : Nullish < string > ,
48
+ node : Nullish < Element > ,
49
+ matcher : Nullish < Matcher > ,
50
+ normalizer : NormalizerFn ,
51
+ ) {
10
52
if ( typeof textToMatch !== 'string' ) {
11
53
return false
12
54
}
13
-
14
55
assertNotNullOrUndefined ( matcher )
15
56
16
57
const normalizedText = normalizer ( textToMatch )
58
+
17
59
if ( typeof matcher === 'string' ) {
18
60
return normalizedText . toLowerCase ( ) . includes ( matcher . toLowerCase ( ) )
19
61
} else if ( typeof matcher === 'function' ) {
@@ -23,7 +65,12 @@ function fuzzyMatches(textToMatch, node, matcher, normalizer) {
23
65
}
24
66
}
25
67
26
- function matches ( textToMatch , node , matcher , normalizer ) {
68
+ function matches (
69
+ textToMatch : Nullish < string > ,
70
+ node : Nullish < Element > ,
71
+ matcher : Nullish < Matcher > ,
72
+ normalizer : NormalizerFn ,
73
+ ) {
27
74
if ( typeof textToMatch !== 'string' ) {
28
75
return false
29
76
}
@@ -40,7 +87,10 @@ function matches(textToMatch, node, matcher, normalizer) {
40
87
}
41
88
}
42
89
43
- function getDefaultNormalizer ( { trim = true , collapseWhitespace = true } = { } ) {
90
+ function getDefaultNormalizer ( {
91
+ trim = true ,
92
+ collapseWhitespace = true ,
93
+ } : DefaultNormalizerOptions = { } ) : NormalizerFn {
44
94
return text => {
45
95
let normalizedText = text
46
96
normalizedText = trim ? normalizedText . trim ( ) : normalizedText
@@ -60,7 +110,12 @@ function getDefaultNormalizer({trim = true, collapseWhitespace = true} = {}) {
60
110
* @param {Function|undefined } normalizer The user-specified normalizer
61
111
* @returns {Function } A normalizer
62
112
*/
63
- function makeNormalizer ( { trim, collapseWhitespace, normalizer} ) {
113
+
114
+ function makeNormalizer ( {
115
+ trim,
116
+ collapseWhitespace,
117
+ normalizer,
118
+ } : NormalizerOptions ) {
64
119
if ( normalizer ) {
65
120
// User has specified a custom normalizer
66
121
if (
0 commit comments