-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
125b7bb
commit 5e70e17
Showing
2 changed files
with
136 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,86 @@ | ||
import { deserialize } from './deserializer'; | ||
|
||
// @todo consider using protobuf | ||
function serialize(obj: object) { | ||
return Buffer.from(JSON.stringify(obj)).toString(`base64`); | ||
} | ||
|
||
export type SerializableFinder = string; | ||
export type Pattern = string | RegExp; | ||
|
||
export const ancestor = (args: { | ||
of: SerializableFinder; | ||
matching: SerializableFinder; | ||
matchRoot: boolean; | ||
}) => { | ||
const { of, matching } = args; | ||
const matchRoot = args.matchRoot || false; | ||
const a: any = { | ||
finderType: `Ancestor`, | ||
matchRoot, | ||
}; | ||
Object.entries(deserialize(of)).forEach( | ||
([key, value]) => (a[`of_${key}`] = value), | ||
); | ||
Object.entries(deserialize(matching)).forEach( | ||
([key, value]) => (a[`matching_${key}`] = value), | ||
); | ||
return serialize(a); | ||
}; | ||
|
||
export const bySemanticsLabel = (label: Pattern) => | ||
serialize({ | ||
finderType: `BySemanticsLabel`, | ||
isRegExp: label instanceof RegExp ? true : false, | ||
label: label.toString().slice(1, -1), | ||
}); | ||
|
||
export const byTooltip = (text: string) => | ||
serialize({ | ||
finderType: `ByTooltipMessage`, | ||
text, | ||
}); | ||
|
||
export const byType = (type: string) => | ||
serialize({ | ||
finderType: `ByType`, | ||
type, | ||
}); | ||
|
||
export const byValueKey = (key: string | number) => | ||
serialize({ | ||
finderType: `ByValueKey`, | ||
keyValueString: key, | ||
// @todo is `int` correct? | ||
keyValueType: typeof key === `string` ? `String` : `int`, | ||
}); | ||
|
||
export const descendant = (args: { | ||
of: SerializableFinder; | ||
matching: SerializableFinder; | ||
matchRoot: boolean; | ||
}) => { | ||
const { of, matching } = args; | ||
const matchRoot = args.matchRoot || false; | ||
const a: any = { | ||
finderType: `Descendant`, | ||
matchRoot, | ||
}; | ||
Object.entries(deserialize(of)).forEach( | ||
([key, value]) => (a[`of_${key}`] = value), | ||
); | ||
Object.entries(deserialize(matching)).forEach( | ||
([key, value]) => (a[`matching_${key}`] = value), | ||
); | ||
return serialize(a); | ||
}; | ||
|
||
export const pageBack = () => | ||
serialize({ | ||
finderType: `PageBack`, | ||
}); | ||
|
||
export const byText = (text: string) => | ||
serialize({ | ||
finderType: `ByText`, | ||
text, | ||
}); |