forked from Laboratoria/DEV007-md-links
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
40 lines (37 loc) · 959 Bytes
/
utils.js
File metadata and controls
40 lines (37 loc) · 959 Bytes
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
// funcion stats
const statsLinks = (links) => {
return {
Total: links.length,
Unique: new Set(links.map((link) => link.url)).size,
};
};
// funcion validate
const validateLinks = (link) => {
return fetch(link.url)
.then((response) => {
if (response.status >= 200 && response.status < 400) {
link.status = response.status;
link.ok = "ok";
return link;
} else {
link.status = response.status;
link.ok = "fail";
return link;
}
})
.catch((error) => {
link.status = "Error";
link.ok = "fail";
return link;
});
};
// funcion validate y stats
const statsValidateLinks = (links) => {
const brokenLinks = links.filter((link) => link.ok !== "ok");
return {
Total: links.length,
Unique: new Set(links.map((link) => link.url)).size,
Broken: brokenLinks.length,
};
};
module.exports = { statsLinks, validateLinks, statsValidateLinks };