Skip to content

Commit e4c876c

Browse files
committed
Some improvements to list and small refactoring
1 parent ec7be0f commit e4c876c

File tree

4 files changed

+84
-22
lines changed

4 files changed

+84
-22
lines changed

lib/api-functions.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import inquirer from 'inquirer';
2-
import { tokenGuard } from './functions.js';
32
import fetch from 'node-fetch';
4-
import { LOGIN_LINK, TABLE_HEADERS } from './constant.js';
3+
import { LOGIN_LINK } from './constant.js';
54
import { rmSync, writeFileSync } from 'fs';
65
import { homedir } from 'os';
7-
import { JSDOM } from 'jsdom';
86
import { table } from 'table';
7+
import {
8+
convertToDate,
9+
getListTable,
10+
mapToVisibilityString,
11+
tokenGuard,
12+
} from './util.js';
913

1014
export async function loginUser(argv, apiToken) {
1115
if (tokenGuard(apiToken)) {
@@ -70,22 +74,7 @@ export async function listPastes(argv, apiToken, userToken) {
7074
const data = await response.text();
7175

7276
if (response.status === 200) {
73-
const tableData = [];
74-
tableData.push(TABLE_HEADERS);
75-
76-
const xmlResponse = new JSDOM(data);
77-
78-
xmlResponse.window.document.querySelectorAll('paste').forEach((paste) => {
79-
const pasteRow = [];
80-
81-
pasteRow.push(paste.querySelector('paste_key').textContent);
82-
pasteRow.push(paste.querySelector('paste_title').textContent);
83-
pasteRow.push(paste.querySelector('paste_private').textContent);
84-
pasteRow.push(paste.querySelector('paste_expire_date').textContent);
85-
pasteRow.push(paste.querySelector('paste_format_short').textContent);
86-
87-
tableData.push(pasteRow);
88-
});
77+
const tableData = getListTable(data);
8978

9079
console.log(table(tableData));
9180
} else {

lib/constant.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,11 @@ export const TABLE_HEADERS = [
77
'paste expiry',
88
'paste syntax highlighting',
99
];
10+
11+
export const PASTE_LIST_KEYS = {
12+
key: 'paste_key',
13+
title: 'paste_title',
14+
visibility: 'paste_private',
15+
expiryDate: 'paste_expire_date',
16+
format: 'paste_format_short',
17+
};

lib/functions.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
export function tokenGuard(token) {
2-
return token === '' || token === undefined || token === null;
3-
}
1+

lib/util.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { TABLE_HEADERS, PASTE_LIST_KEYS } from './constant.js';
2+
import { JSDOM } from 'jsdom';
3+
4+
export function mapToVisibilityString(code) {
5+
switch (Number(code)) {
6+
case 0:
7+
return 'public';
8+
case 1:
9+
return 'unlisted';
10+
case 2:
11+
return 'private';
12+
default:
13+
return 'unknown';
14+
}
15+
}
16+
17+
export function convertToDate(epoch) {
18+
const epochNumber = Number(epoch);
19+
const date = new Date(epochNumber * 1000);
20+
21+
const day = date.getDate();
22+
const month = date.getMonth() + 1;
23+
const year = date.getFullYear();
24+
const hours = date.getHours();
25+
const minutes = date.getMinutes();
26+
27+
return epochNumber === 0
28+
? 'Never'
29+
: `${day < 10 ? '0' + month : '' + day}.${
30+
month < 10 ? '0' + month : '' + month
31+
}.${year} ${hours < 10 ? '0' + hours : '' + hours}:${
32+
minutes < 10 ? '0' + minutes : '' + minutes
33+
}`;
34+
}
35+
36+
export function tokenGuard(token) {
37+
return token === '' || token === undefined || token === null;
38+
}
39+
40+
export function getListTable(xmlData) {
41+
const tableData = [];
42+
tableData.push(TABLE_HEADERS);
43+
44+
const xmlResponse = new JSDOM(xmlData);
45+
46+
xmlResponse.window.document.querySelectorAll('paste').forEach((paste) => {
47+
const pasteKey = paste.querySelector(PASTE_LIST_KEYS.key).textContent;
48+
const pasteName = paste.querySelector(PASTE_LIST_KEYS.title).textContent;
49+
const pasteVisibility = paste.querySelector(
50+
PASTE_LIST_KEYS.visibility
51+
).textContent;
52+
const pasteExpiryDate = paste.querySelector(
53+
PASTE_LIST_KEYS.expiryDate
54+
).textContent;
55+
const pasteFormat = paste.querySelector(PASTE_LIST_KEYS.format).textContent;
56+
57+
tableData.push([
58+
pasteKey,
59+
pasteName,
60+
mapToVisibilityString(pasteVisibility),
61+
convertToDate(pasteExpiryDate),
62+
pasteFormat,
63+
]);
64+
});
65+
66+
return tableData;
67+
}

0 commit comments

Comments
 (0)