Skip to content

Commit ab4b9e8

Browse files
committed
(#500) Add new query kind ColorQuery + test
1 parent 20b3091 commit ab4b9e8

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

lib/query.class.spec.ts

+37
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import {
2+
ColorQuery,
3+
isColorQuery,
24
isTextQuery,
35
isWindowQuery,
46
TextQuery,
57
WindowQuery,
68
} from "./query.class";
79
import { Image, isImage } from "./image.class";
10+
import { RGBA } from "./rgba.class";
811

912
const dummyImage = new Image(0, 0, Buffer.of(0), 3, "foo", 0, 0);
1013

@@ -53,6 +56,40 @@ describe("query types", () => {
5356
}
5457
);
5558

59+
it.each<[ColorQuery, boolean]>([
60+
[
61+
{
62+
id: "dummy",
63+
type: "color",
64+
by: {
65+
color: new RGBA(0, 0, 0, 0),
66+
},
67+
},
68+
true,
69+
],
70+
[
71+
{
72+
id: "dummy",
73+
type: "foo",
74+
by: {
75+
line: "dummy-query",
76+
},
77+
} as unknown as ColorQuery,
78+
false,
79+
],
80+
])(
81+
"should correctly identify text queries",
82+
(query: ColorQuery, expected: boolean) => {
83+
// GIVEN
84+
85+
// WHEN
86+
const result = isColorQuery(query);
87+
88+
// THEN
89+
expect(result).toBe(expected);
90+
}
91+
);
92+
5693
it.each<[WindowQuery, boolean]>([
5794
[
5895
{

lib/query.class.ts

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { RGBA } from "./rgba.class";
2+
13
type Query =
24
| {
35
id: string;
@@ -19,6 +21,13 @@ type Query =
1921
by: {
2022
title: string | RegExp;
2123
};
24+
}
25+
| {
26+
id: string;
27+
type: "color";
28+
by: {
29+
color: RGBA;
30+
};
2231
};
2332

2433
export type TextQuery = Extract<Query, { type: "text" }>;
@@ -41,6 +50,22 @@ export type LineQuery = Extract<TextQuery, { by: { line: string } }>;
4150
*/
4251
export type WindowQuery = Extract<Query, { type: "window" }>;
4352

53+
/**
54+
* A color query is a query that searches for a certain RGBA color on screen.
55+
* It will be processed by an {@link ColorFinderInterface} instance.
56+
*/
57+
export type ColorQuery = Extract<Query, { type: "color" }>;
58+
59+
/**
60+
* Type guard for {@link ColorQuery}
61+
* @param possibleQuery A possible color query
62+
*/
63+
export const isColorQuery = (
64+
possibleQuery: any
65+
): possibleQuery is ColorQuery => {
66+
return possibleQuery?.type === "color" && possibleQuery?.by?.color != null;
67+
};
68+
4469
/**
4570
* Type guard for {@link WordQuery}
4671
* @param possibleQuery A possible word query

0 commit comments

Comments
 (0)