-
Notifications
You must be signed in to change notification settings - Fork 0
/
breadcrumbGenerator.js
82 lines (74 loc) · 1.88 KB
/
breadcrumbGenerator.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
/*
Problem URL
https://www.codewars.com/kata/563fbac924106b8bf7000046
*/
const invalidShortList = [
"the",
"of",
"in",
"from",
"by",
"with",
"and",
"or",
"for",
"to",
"at",
"a",
];
function generateBC(url, separator) {
let indexOfWildCard = url.indexOf("?");
if (indexOfWildCard >= 0) {
url = url.substring(indexOfWildCard, url.lenght - 1);
}
let indexOfSharp = url.indexOf("#");
if (indexOfSharp >= 0) {
url = url.substring(indexOfSharp, url.lenght - 1);
}
url = url.replace('http://');
url = url.replace('https://');
url = url.replace('www.');
let urlParts = url.split("/");
let prevLink = "/";
let bC = '';
urlParts = urlParts.filter((x)=>{return !x.startsWith('index.')});
for (let i = 1; i < urlParts.length; i++) {
let urlPart = urlParts[i].split(".")[0];
let urlPartText = "";
if (urlPart.length > 30) {
urlPartText = urlPart
.split("-")
.filter((x) => {
return invalidShortList.indexOf(x) < 0;
})
.map((x)=> {return x[0]})
.join("")
.toUpperCase();
} else {
urlPartText = urlPart.split("-").join(" ").toUpperCase();
}
if(!urlPartText)
continue;
if (i === (urlParts.length - 1)) {
bC = bC+'<span class="active">' + urlPartText + "</span>";
} else {
bC =
bC +
'<a href="' +
prevLink +
urlPart +
'/">' +
urlPartText +
"</a>"
+separator;
prevLink = prevLink + urlPart + '/';
}
}
if (!bC) {
bC = '<span class="active">HOME</span>';
}
else{
bC = '<a href="/">HOME</a>' + separator + bC;
}
return bC;
}