-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
131 lines (112 loc) Β· 3.06 KB
/
util.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const {
LIST_TABLE_HEADERS,
PASTE_LIST_KEYS,
USER_TABLE_HEADERS,
} = require('./constant.js');
const { JSDOM } = require('jsdom');
module.exports = {
mapToVisibilityString,
mapToVisiblityCode,
convertToDate,
tokenGuard,
getListTable,
getUserData,
mapAccountPremium,
mapAccountVisibility,
};
function mapToVisibilityString(code) {
switch (Number(code)) {
case 0:
return 'public';
case 1:
return 'unlisted';
case 2:
return 'private';
default:
return 'unknown';
}
}
function mapToVisiblityCode(string) {
if (string === 'public') {
return 0;
} else if (string === 'unlisted') {
return 1;
} else if (string === 'private') {
return 2;
}
}
function convertToDate(epoch) {
const epochNumber = Number(epoch);
const date = new Date(epochNumber * 1000);
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
const hours = date.getHours();
const minutes = date.getMinutes();
return epochNumber === 0
? 'Never'
: `${day < 10 ? '0' + day : '' + day}.${
month < 10 ? '0' + month : '' + month
}.${year} ${hours < 10 ? '0' + hours : '' + hours}:${
minutes < 10 ? '0' + minutes : '' + minutes
}`;
}
function tokenGuard(token) {
return token === '' || token === undefined || token === null;
}
function getListTable(xmlData) {
const tableData = [];
tableData.push(LIST_TABLE_HEADERS);
const xmlResponse = new JSDOM(xmlData);
xmlResponse.window.document.querySelectorAll('paste').forEach((paste) => {
const pasteKey = paste.querySelector(PASTE_LIST_KEYS.key).textContent;
const pasteName = paste.querySelector(PASTE_LIST_KEYS.title).textContent;
const pasteVisibility = paste.querySelector(
PASTE_LIST_KEYS.visibility
).textContent;
const pasteExpiryDate = paste.querySelector(
PASTE_LIST_KEYS.expiryDate
).textContent;
const pasteFormat = paste.querySelector(PASTE_LIST_KEYS.format).textContent;
tableData.push([
pasteKey,
pasteName,
mapToVisibilityString(pasteVisibility),
convertToDate(pasteExpiryDate),
pasteFormat,
]);
});
return tableData;
}
function getUserData(xml) {
const tableData = [];
tableData.push(USER_TABLE_HEADERS);
const xmlResponse = new JSDOM(xml);
const user = xmlResponse.window.document.querySelector('user');
const userName = user.querySelector('user_name').textContent;
const privacy = user.querySelector('user_private').textContent;
const email = user.querySelector('user_email').textContent;
const premium = user.querySelector('user_account_type').textContent;
tableData.push([
userName,
mapAccountVisibility(privacy),
email,
mapAccountPremium(premium),
]);
return tableData;
}
function mapAccountPremium(code) {
return Number(code) === 0 ? 'free' : 'premium';
}
function mapAccountVisibility(code) {
switch (Number(code)) {
case 0:
return 'public';
case 1:
return 'unlisted';
case 2:
return 'private';
default:
return 'error, could not get from api';
}
}