-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
62 lines (53 loc) · 1.4 KB
/
utils.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
import config from "./config.js";
class BungieAPIError extends Error {
constructor(message) {
super(message);
this.name = "BungieAPIError";
}
}
export default {
BungieAPIError,
debugLog() {
if (config.debug) console.log(...arguments);
},
normalizeURL(url) {
try {
return (new URL(url)).pathname;
} catch(ex) {
return "";
}
},
slugify(text) {
return text
.toString()
.normalize('NFKD')
.toLowerCase()
.trim()
.replace(/\s+/g, '-')
.replace(/[^\w\-]+/g, '')
.replace(/\-\-+/g, '-');
},
getURLFromPageInfo(...info) {
var type, title;
if (typeof info[0] === "object") {
// We received a pageInfo object
({type, title: {en: title}} = info);
} else {
// We received type and title
[type, title] = info;
}
var path = "";
if (type == "entry") {
path = "/entries/";
} else if (type == "card") {
path = "/cards/";
} else if (type == "book") {
path = "/categories/" + (!["The Maraid", "Books of Sorrow"].includes(title) ? "book-" : "");
} else {
return "";
}
var slug = this.slugify(title);
var url = path + slug;
return url;
}
}