|
| 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