Skip to content

Commit 8c82991

Browse files
committed
chore: migrate matches.js to typescript
1 parent c3b9704 commit 8c82991

File tree

7 files changed

+69
-12
lines changed

7 files changed

+69
-12
lines changed

src/__tests__/matches.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {fuzzyMatches, matches} from '../matches'
1+
import {fuzzyMatches, matches} from '../matches.ts'
22

33
// unit tests for text match utils
44

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export * from './wait-for'
77
export * from './wait-for-element'
88
export * from './wait-for-element-to-be-removed'
99
export * from './wait-for-dom-change'
10-
export {getDefaultNormalizer} from './matches'
10+
export {getDefaultNormalizer} from './matches.ts'
1111
export * from './get-node-text'
1212
export * from './events'
1313
export * from './get-queries-for-element'

src/matches.js renamed to src/matches.ts

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,61 @@
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) {
339
throw new Error(
40+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
441
`It looks like ${matcher} was passed instead of a matcher. Did you do something like getByText(${matcher})?`,
542
)
643
}
744
}
845

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+
) {
1052
if (typeof textToMatch !== 'string') {
1153
return false
1254
}
13-
1455
assertNotNullOrUndefined(matcher)
1556

1657
const normalizedText = normalizer(textToMatch)
58+
1759
if (typeof matcher === 'string') {
1860
return normalizedText.toLowerCase().includes(matcher.toLowerCase())
1961
} else if (typeof matcher === 'function') {
@@ -23,7 +65,12 @@ function fuzzyMatches(textToMatch, node, matcher, normalizer) {
2365
}
2466
}
2567

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+
) {
2774
if (typeof textToMatch !== 'string') {
2875
return false
2976
}
@@ -40,7 +87,10 @@ function matches(textToMatch, node, matcher, normalizer) {
4087
}
4188
}
4289

43-
function getDefaultNormalizer({trim = true, collapseWhitespace = true} = {}) {
90+
function getDefaultNormalizer({
91+
trim = true,
92+
collapseWhitespace = true,
93+
}: DefaultNormalizerOptions = {}): NormalizerFn {
4494
return text => {
4595
let normalizedText = text
4696
normalizedText = trim ? normalizedText.trim() : normalizedText
@@ -60,7 +110,12 @@ function getDefaultNormalizer({trim = true, collapseWhitespace = true} = {}) {
60110
* @param {Function|undefined} normalizer The user-specified normalizer
61111
* @returns {Function} A normalizer
62112
*/
63-
function makeNormalizer({trim, collapseWhitespace, normalizer}) {
113+
114+
function makeNormalizer({
115+
trim,
116+
collapseWhitespace,
117+
normalizer,
118+
}: NormalizerOptions) {
64119
if (normalizer) {
65120
// User has specified a custom normalizer
66121
if (

src/queries/all-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export * from '../matches'
1+
export * from '../matches.ts'
22
export * from '../get-node-text'
33
export * from '../query-helpers'
44
export * from '../config.ts'

src/query-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {getSuggestedQuery} from './suggestions'
2-
import {fuzzyMatches, matches, makeNormalizer} from './matches'
2+
import {fuzzyMatches, matches, makeNormalizer} from './matches.ts'
33
import {waitFor} from './wait-for'
44
import {getConfig} from './config.ts'
55

src/suggestions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {computeAccessibleName} from 'dom-accessibility-api'
2-
import {getDefaultNormalizer} from './matches'
2+
import {getDefaultNormalizer} from './matches.ts'
33
import {getNodeText} from './get-node-text'
44
import {DEFAULT_IGNORE_TAGS, getConfig} from './config.ts'
55
import {getImplicitAriaRoles, isInaccessible} from './role-helpers'

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export type Nullish<T> = T | null | undefined
2+
13
export type Callback<T> = () => T

0 commit comments

Comments
 (0)