/
identical-links-same-purpose-after.js
61 lines (55 loc) · 1.73 KB
/
identical-links-same-purpose-after.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
/**
* Skip unless there are more than a single result
*/
if (results.length < 2) {
return results;
}
/**
* for each result
* - get other results with matching accessible name
* - check if same purpose is served
* - if not change `result` to `undefined`
*/
for (let i = 0; i < results.length; i++) {
const { accessibleText, linkResource } = results[i].data;
const resourceLocation = linkResource.split('/').pop();
const matchingIndices = getIndicesOfLinksWithMatchingAccessibleText(
accessibleText,
i
);
const identicalLinks = matchingIndices.map(index => results[index]);
if (!hasSamePurpose(resourceLocation, identicalLinks)) {
results[i].result = undefined;
}
}
return results;
/**
* Get list of items from results which match a prescribed accessible name
* @param {String} expectedAccessibleText accessible name to be matched
* @param {Number} excludeIndex exclude `index` of result, that should not be taken into consideration
* @returns {Array<Number>}
*/
function getIndicesOfLinksWithMatchingAccessibleText(
expectedAccessibleText,
excludeIndex
) {
return results
.map(({ data: { accessibleText } }, index) => {
if (index !== excludeIndex && accessibleText === expectedAccessibleText) {
return index;
}
})
.filter(value => typeof value !== 'undefined');
}
/**
* Check if a given set of links have same resource
* @param {String} expectedResource resource string
* @param {Array<Object>} identicalLinks results where `data.accessibleText` were identical
* @returns {Boolean}
*/
function hasSamePurpose(expectedResource, identicalLinks) {
return identicalLinks.every(({ data: { linkResource } }) => {
const resource = linkResource.split('/').pop();
return expectedResource.includes(resource);
});
}