Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 75b9e44

Browse files
committedJan 1, 2022
(#354) Fix duplication issues
1 parent 20f12c8 commit 75b9e44

File tree

1 file changed

+41
-22
lines changed

1 file changed

+41
-22
lines changed
 

Diff for: ‎lib/screen.class.ts

+41-22
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,15 @@ export class ScreenClass {
9999
template: Image | Promise<Image>,
100100
params?: OptionalSearchParameters,
101101
): Promise<Region> {
102-
const minMatch = (params && params.confidence) || this.config.confidence;
103-
const screenSize = await this.providerRegistry.getScreen().screenSize();
104-
const searchRegion = (params && params.searchRegion) || screenSize;
105-
const searchMultipleScales = (params && params.searchMultipleScales)
106-
107-
const needle = await template;
102+
const {
103+
minMatch,
104+
screenSize,
105+
searchRegion,
106+
screenImage,
107+
searchMultipleScales
108+
} = await this.getFindParameters(params);
108109

109-
if (!isImage(needle)) {
110-
throw Error(`find requires an Image, but received ${JSON.stringify(needle)}`)
111-
}
112-
113-
const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion);
110+
const needle = await ScreenClass.getNeedle(template);
114111

115112
const matchRequest = new MatchRequest(
116113
screenImage,
@@ -155,18 +152,15 @@ export class ScreenClass {
155152
template: FirstArgumentType<typeof ScreenClass.prototype.find>,
156153
params?: OptionalSearchParameters,
157154
): Promise<Region[]> {
158-
const minMatch = (params && params.confidence) || this.config.confidence;
159-
const screenSize = await this.providerRegistry.getScreen().screenSize();
160-
const searchRegion = (params && params.searchRegion) || screenSize;
161-
const searchMultipleScales = (params && params.searchMultipleScales)
162-
163-
const needle = await template;
164-
165-
if (!isImage(needle)) {
166-
throw Error(`findAll requires an Image, but received ${JSON.stringify(needle)}`)
167-
}
155+
const {
156+
minMatch,
157+
screenSize,
158+
searchRegion,
159+
screenImage,
160+
searchMultipleScales
161+
} = await this.getFindParameters(params);
168162

169-
const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion);
163+
const needle = await ScreenClass.getNeedle(template);
170164

171165
const matchRequest = new MatchRequest(
172166
screenImage,
@@ -356,4 +350,29 @@ export class ScreenClass {
356350
await this.providerRegistry.getImageWriter().store({image, path: outputPath})
357351
return outputPath;
358352
}
353+
354+
private async getFindParameters(params?: OptionalSearchParameters) {
355+
const minMatch = params?.confidence ?? this.config.confidence;
356+
const screenSize = await this.providerRegistry.getScreen().screenSize();
357+
const searchRegion = params?.searchRegion ?? screenSize;
358+
const screenImage = await this.providerRegistry.getScreen().grabScreenRegion(searchRegion);
359+
const searchMultipleScales = params?.searchMultipleScales ?? true;
360+
361+
return ({
362+
minMatch,
363+
screenSize,
364+
searchRegion,
365+
screenImage,
366+
searchMultipleScales
367+
});
368+
}
369+
370+
private static async getNeedle(template: FirstArgumentType<typeof ScreenClass.prototype.find>) {
371+
const needle = await template;
372+
373+
if (!isImage(needle)) {
374+
throw Error(`find requires an Image, but received ${JSON.stringify(needle)}`)
375+
}
376+
return needle;
377+
}
359378
}

0 commit comments

Comments
 (0)
Please sign in to comment.