1
- function assertNotNullOrUndefined ( matcher ) {
2
- if ( matcher == null ) {
1
+ import {
2
+ Matcher ,
3
+ NormalizerFn ,
4
+ NormalizerOptions ,
5
+ DefaultNormalizerOptions ,
6
+ } from '../types'
7
+
8
+ type Nullish < T > = T | null | undefined
9
+
10
+ function assertNotNullOrUndefined < T > (
11
+ matcher : T ,
12
+ ) : asserts matcher is NonNullable < T > {
13
+ if ( matcher === null || matcher === undefined ) {
3
14
throw new Error (
15
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- implicitly converting `T` to `string`
4
16
`It looks like ${ matcher } was passed instead of a matcher. Did you do something like getByText(${ matcher } )?` ,
5
17
)
6
18
}
7
19
}
8
20
9
- function fuzzyMatches ( textToMatch , node , matcher , normalizer ) {
21
+ function fuzzyMatches (
22
+ textToMatch : Nullish < string > ,
23
+ node : Nullish < Element > ,
24
+ matcher : Nullish < Matcher > ,
25
+ normalizer : NormalizerFn ,
26
+ ) {
10
27
if ( typeof textToMatch !== 'string' ) {
11
28
return false
12
29
}
13
-
14
30
assertNotNullOrUndefined ( matcher )
15
31
16
32
const normalizedText = normalizer ( textToMatch )
33
+
17
34
if ( typeof matcher === 'string' ) {
18
35
return normalizedText . toLowerCase ( ) . includes ( matcher . toLowerCase ( ) )
19
36
} else if ( typeof matcher === 'function' ) {
@@ -23,7 +40,12 @@ function fuzzyMatches(textToMatch, node, matcher, normalizer) {
23
40
}
24
41
}
25
42
26
- function matches ( textToMatch , node , matcher , normalizer ) {
43
+ function matches (
44
+ textToMatch : Nullish < string > ,
45
+ node : Nullish < Element > ,
46
+ matcher : Nullish < Matcher > ,
47
+ normalizer : NormalizerFn ,
48
+ ) {
27
49
if ( typeof textToMatch !== 'string' ) {
28
50
return false
29
51
}
@@ -40,7 +62,10 @@ function matches(textToMatch, node, matcher, normalizer) {
40
62
}
41
63
}
42
64
43
- function getDefaultNormalizer ( { trim = true , collapseWhitespace = true } = { } ) {
65
+ function getDefaultNormalizer ( {
66
+ trim = true ,
67
+ collapseWhitespace = true ,
68
+ } : DefaultNormalizerOptions = { } ) : NormalizerFn {
44
69
return text => {
45
70
let normalizedText = text
46
71
normalizedText = trim ? normalizedText . trim ( ) : normalizedText
@@ -60,7 +85,12 @@ function getDefaultNormalizer({trim = true, collapseWhitespace = true} = {}) {
60
85
* @param {Function|undefined } normalizer The user-specified normalizer
61
86
* @returns {Function } A normalizer
62
87
*/
63
- function makeNormalizer ( { trim, collapseWhitespace, normalizer} ) {
88
+
89
+ function makeNormalizer ( {
90
+ trim,
91
+ collapseWhitespace,
92
+ normalizer,
93
+ } : NormalizerOptions ) {
64
94
if ( normalizer ) {
65
95
// User has specified a custom normalizer
66
96
if (
0 commit comments