-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (71 loc) · 2.01 KB
/
index.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
import { parseCsvFile } from "./csv-reader/csv-reader.js";
import { init as initMenu } from "./menu/menu.js";
import { displayTable } from "./table/table.js";
import { calculateLastPage, sliceData } from "./utils/utils.js";
const DEFAULT_ROWS_PER_PAGE = 3;
let currentPage = 0;
function getFilePath() {
const args = process.argv.slice(2);
const filename = args[0];
if (!filename) {
console.error("Missing filename.");
return null;
}
return `./${filename}`;
}
function getRowsPerPage() {
const args = process.argv.slice(2);
if (args.length > 1) {
const userRowsPerPage = parseInt(args[1], 10);
if (userRowsPerPage > 0) {
return userRowsPerPage;
}
}
return DEFAULT_ROWS_PER_PAGE;
}
function displayFirstPage(data, rowsPerPage) {
currentPage = 0;
displaySelectedData(data, rowsPerPage);
}
function displayPreviousPage(data, rowsPerPage) {
if (currentPage > 0) {
currentPage--;
}
displaySelectedData(data, rowsPerPage);
}
function displayNextPage(data, rowsPerPage) {
const lastPage = calculateLastPage(data.data, rowsPerPage);
if (currentPage < lastPage) {
currentPage++;
}
displaySelectedData(data, rowsPerPage);
}
function displayLastPage(data, rowsPerPage) {
currentPage = calculateLastPage(data.data, rowsPerPage);
displaySelectedData(data, rowsPerPage);
}
function onExit() {
console.log("See ya!");
}
function displaySelectedData(data, rowsPerPage) {
const selectedData = sliceData(data, currentPage * rowsPerPage, rowsPerPage);
displayTable(selectedData);
}
async function main() {
const filename = getFilePath();
if (filename) {
const data = parseCsvFile(filename);
if (data) {
const rowsPerPage = getRowsPerPage();
displayFirstPage(data, rowsPerPage);
await initMenu(
() => displayFirstPage(data, rowsPerPage),
() => displayPreviousPage(data, rowsPerPage),
() => displayNextPage(data, rowsPerPage),
() => displayLastPage(data, rowsPerPage),
onExit
);
}
}
}
main();