-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildPredicate.ts
44 lines (41 loc) · 1.34 KB
/
BuildPredicate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { PredicateInterface } from "./types/types";
import WebFunctions from "./types/WebFunctions";
const BuildPredicate = (predicate: PredicateInterface) => {
let returnedSelector = '';
if (predicate.functions && predicate.functions?.length > 0) {
predicate.functions.forEach((xpathFunction) => {
switch(xpathFunction) {
case WebFunctions.contains: {
if(returnedSelector.length === 0) {
returnedSelector = `contains(${predicate.attribute}, "${predicate.value}")`;
} else {
throw new Error("Contains has to be first function.");
}
break;
}
case WebFunctions.not: {
if(returnedSelector.length === 0) {
returnedSelector = `not(${handleBuildPredicate(predicate)})`;
} else {
returnedSelector = `not(${returnedSelector})`;
}
break;
}
default: {
break;
}
}
});
} else {
returnedSelector += handleBuildPredicate(predicate);
}
return returnedSelector;
}
const handleBuildPredicate = (predicate: PredicateInterface):string => {
if(predicate.operator) {
return `${predicate.attribute} ${predicate.operator} "${predicate.value}"`
} else {
return `${predicate.attribute} = "${predicate.value}"`
}
}
export default BuildPredicate;