starlight-table 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/starlight-table.mjs +80 -57
package/package.json
CHANGED
package/starlight-table.mjs
CHANGED
|
@@ -1,94 +1,117 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Starlight Table Builder
|
|
3
|
-
* Fully configurable ASCII table generator
|
|
3
|
+
* Fully configurable ASCII table generator with list support
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
function pad(text, width) {
|
|
7
|
-
const str = String(text)
|
|
8
|
-
return str + " ".repeat(Math.max(0, width - str.length))
|
|
7
|
+
const str = String(text);
|
|
8
|
+
return str + " ".repeat(Math.max(0, width - str.length));
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
function separator(widths) {
|
|
12
|
-
return "+" + widths.map(w => "-".repeat(w + 2)).join("+") + "+"
|
|
12
|
+
return "+" + widths.map(w => "-".repeat(w + 2)).join("+") + "+";
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
function row(cells, widths) {
|
|
16
|
-
return "| " + cells.map((c, i) => pad(c, widths[i])).join(" | ") + " |"
|
|
16
|
+
return "| " + cells.map((c, i) => pad(c, widths[i] || 0)).join(" | ") + " |";
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
19
|
+
function createTable() {
|
|
20
|
+
return {
|
|
21
|
+
headers: [],
|
|
22
|
+
rows: []
|
|
23
|
+
};
|
|
24
|
+
}
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
function setHeaders(table, headers) {
|
|
27
|
+
table.headers = headers.map(h => String(h));
|
|
28
|
+
return table;
|
|
29
|
+
}
|
|
29
30
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
function addRow(table, row) {
|
|
32
|
+
table.rows.push(row.map(v => v == null ? "" : String(v)));
|
|
33
|
+
return table;
|
|
34
|
+
}
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
return this
|
|
36
|
+
function addRows(table, rows) {
|
|
37
|
+
for (const r of rows) {
|
|
38
|
+
addRow(table, r);
|
|
40
39
|
}
|
|
40
|
+
return table;
|
|
41
|
+
}
|
|
41
42
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
function setHeadersFromCSV(table, csv) {
|
|
44
|
+
const headers = csv.split(",").map(s => s.trim());
|
|
45
|
+
return setHeaders(table, headers);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function addRowFromCSV(table, csv) {
|
|
49
|
+
const row = csv.split(",").map(s => s.trim());
|
|
50
|
+
return addRow(table, row);
|
|
51
|
+
}
|
|
46
52
|
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
function columnWidths(table) {
|
|
54
|
+
const all = [];
|
|
55
|
+
if (table.headers.length) all.push(table.headers);
|
|
56
|
+
all.push(...table.rows);
|
|
49
57
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
...all.map(r => (r[i] ? r[i].length : 0))
|
|
53
|
-
)
|
|
54
|
-
}
|
|
58
|
+
const cols = Math.max(...all.map(r => r.length));
|
|
59
|
+
const widths = [];
|
|
55
60
|
|
|
56
|
-
|
|
61
|
+
for (let i = 0; i < cols; i++) {
|
|
62
|
+
widths[i] = Math.max(...all.map(r => (r[i] ? r[i].length : 0)));
|
|
57
63
|
}
|
|
58
64
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return ""
|
|
62
|
-
}
|
|
65
|
+
return widths;
|
|
66
|
+
}
|
|
63
67
|
|
|
64
|
-
|
|
65
|
-
|
|
68
|
+
function renderTable(table) {
|
|
69
|
+
if (!table.headers.length && !table.rows.length) return "";
|
|
66
70
|
|
|
67
|
-
|
|
71
|
+
const widths = columnWidths(table);
|
|
72
|
+
const lines = [];
|
|
68
73
|
|
|
69
|
-
|
|
70
|
-
lines.push(row(this.headers, widths))
|
|
71
|
-
lines.push(separator(widths))
|
|
72
|
-
}
|
|
74
|
+
lines.push(separator(widths));
|
|
73
75
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
if (table.headers.length) {
|
|
77
|
+
lines.push(row(table.headers, widths));
|
|
78
|
+
lines.push(separator(widths));
|
|
79
|
+
}
|
|
77
80
|
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
for (const r of table.rows) {
|
|
82
|
+
lines.push(row(r, widths));
|
|
80
83
|
}
|
|
81
|
-
}
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
+
lines.push(separator(widths));
|
|
86
|
+
|
|
87
|
+
return lines.join("\n");
|
|
85
88
|
}
|
|
86
89
|
|
|
87
90
|
function createList(items, bullet = "-") {
|
|
88
|
-
return items.map(i => `${bullet} ${i}`).join("\n")
|
|
91
|
+
return items.map(i => `${bullet} ${i}`).join("\n");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function createNumberedList(items) {
|
|
95
|
+
return items.map((i, idx) => `${idx + 1}. ${i}`).join("\n");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function createTableFrom2DArray(arr) {
|
|
99
|
+
const table = createTable();
|
|
100
|
+
if (arr.length === 0) return table;
|
|
101
|
+
setHeaders(table, arr[0]);
|
|
102
|
+
if (arr.length > 1) addRows(table, arr.slice(1));
|
|
103
|
+
return table;
|
|
89
104
|
}
|
|
90
105
|
|
|
91
106
|
export {
|
|
92
107
|
createTable,
|
|
93
|
-
|
|
94
|
-
|
|
108
|
+
setHeaders,
|
|
109
|
+
addRow,
|
|
110
|
+
addRows,
|
|
111
|
+
setHeadersFromCSV,
|
|
112
|
+
addRowFromCSV,
|
|
113
|
+
renderTable,
|
|
114
|
+
createList,
|
|
115
|
+
createNumberedList,
|
|
116
|
+
createTableFrom2DArray
|
|
117
|
+
};
|