forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate-node-results.js
More file actions
65 lines (55 loc) · 1.95 KB
/
aggregate-node-results.js
File metadata and controls
65 lines (55 loc) · 1.95 KB
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
import aggregateChecks from './aggregate-checks';
import aggregate from './aggregate';
import finalizeRuleResult from './finalize-result';
import constants from '../constants';
/**
* Calculates the result of a Rule based on its types and the result of its child Checks
* @param {Array} nodeResults The array of nodes tested by the Rule
*/
function aggregateNodeResults(nodeResults) {
const ruleResult = {};
// For each node, retrieve the result and impact
nodeResults = nodeResults.map(nodeResult => {
// Known result
if (nodeResult.any && nodeResult.all && nodeResult.none) {
return aggregateChecks(nodeResult);
} else if (Array.isArray(nodeResult.node)) {
return finalizeRuleResult(nodeResult);
} else {
throw new TypeError('Invalid Result type');
}
});
// Aggregate the result
// If there were no nodes passed in, mark the test as inapplicable
if (nodeResults && nodeResults.length) {
const resultList = nodeResults.map(node => node.result);
ruleResult.result = aggregate(
constants.results,
resultList,
ruleResult.result
);
} else {
ruleResult.result = 'inapplicable';
}
// Create an array for each type
constants.resultGroups.forEach(group => (ruleResult[group] = []));
// Fill the array with nodes
nodeResults.forEach(nodeResult => {
const groupName = constants.resultGroupMap[nodeResult.result];
ruleResult[groupName].push(nodeResult);
});
// Take the highest impact of failed or canttell rules
let impactGroup = constants.FAIL_GROUP;
if (ruleResult[impactGroup].length === 0) {
impactGroup = constants.CANTTELL_GROUP;
}
if (ruleResult[impactGroup].length > 0) {
// Get the impact of all issues
const impactList = ruleResult[impactGroup].map(failure => failure.impact);
ruleResult.impact = aggregate(constants.impact, impactList) || null;
} else {
ruleResult.impact = null;
}
return ruleResult;
}
export default aggregateNodeResults;