-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_tasks.js
87 lines (75 loc) · 2.55 KB
/
query_tasks.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// query_tasks.js
exports.DoDateQueryTask = function()
{
function padNumber(numberAsString)
{
if (numberAsString.length < 2)
{
numberAsString = "0" + numberAsString;
}
return numberAsString;
};
dateNode = document.querySelector("#overview-of-covid-19-surveillance p");
dateString = dateNode.lastChild.textContent.match(/Report Date: (.*)/)[1];
dateObject = new Date(dateString);
dateFormatted =
(dateObject.getYear() + 1900).toString()
+ "-"
+ padNumber((dateObject.getMonth() + 1).toString())
+ "-"
+ padNumber((dateObject.getDate()).toString())
return dateFormatted;
};
exports.DoJurisdictionQueryTask = function(jurisdictionQueries)
{
function formatNumberString(numberString)
{
if (numberString && typeof(numberString) == "string")
{
numberString =
parseInt(numberString.replace(/,/g, ""));
}
return numberString;
}
let jurisdictionQueryResults = [];
// Do each jurisdiction query
jurisdictionQueries.forEach((jurisdictionQuery) =>
{
// Do validations
jurisdictionQuery.validators.forEach((validator) =>
{
let resultNode = document.querySelector(validator.querySelector);
let resultValue = resultNode ? resultNode.innerHTML : "None";
if (resultValue != validator.queryValue)
{
console.log(
"Validation failure:",
'"' + validator.querySelector + '"',
'"' + validator.queryValue + '"',
'"' + resultValue + '"');
}
});
// Create result object
let jurisdictionQueryResult =
{
jurisdictionName: jurisdictionQuery.jurisdictionName,
selectorResults: []
}
// Retrieve data for each selector
jurisdictionQuery.selectors.forEach((selector) =>
{
let resultNode =
document.querySelector(selector.querySelector);
if (resultNode)
{
let selectorResult = {};
selectorResult.selectorName = selector.selectorName;
selectorResult.queryValue =
formatNumberString(resultNode.innerHTML);
jurisdictionQueryResult.selectorResults.push(selectorResult);
}
});
jurisdictionQueryResults.push(jurisdictionQueryResult)
});
return jurisdictionQueryResults;
};