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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/starlight-table.mjs +80 -57
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-table",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Flexible table and list builder for Starlight",
5
5
  "type": "module",
6
6
  "main": "starlight-table.mjs",
@@ -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
- class Table {
20
- constructor() {
21
- this.headers = []
22
- this.rows = []
23
- }
19
+ function createTable() {
20
+ return {
21
+ headers: [],
22
+ rows: []
23
+ };
24
+ }
24
25
 
25
- setHeaders(headers) {
26
- this.headers = headers.map(h => String(h))
27
- return this
28
- }
26
+ function setHeaders(table, headers) {
27
+ table.headers = headers.map(h => String(h));
28
+ return table;
29
+ }
29
30
 
30
- addRow(row) {
31
- this.rows.push(row.map(v => v === null || v === undefined ? "" : String(v)))
32
- return this
33
- }
31
+ function addRow(table, row) {
32
+ table.rows.push(row.map(v => v == null ? "" : String(v)));
33
+ return table;
34
+ }
34
35
 
35
- addRows(rows) {
36
- for (const r of rows) {
37
- this.addRow(r)
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
- columnWidths() {
43
- const all = []
44
- if (this.headers.length) all.push(this.headers)
45
- all.push(...this.rows)
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
- const cols = Math.max(...all.map(r => r.length))
48
- const widths = []
53
+ function columnWidths(table) {
54
+ const all = [];
55
+ if (table.headers.length) all.push(table.headers);
56
+ all.push(...table.rows);
49
57
 
50
- for (let i = 0; i < cols; i++) {
51
- widths[i] = Math.max(
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
- return widths
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
- render() {
60
- if (!this.headers.length && !this.rows.length) {
61
- return ""
62
- }
65
+ return widths;
66
+ }
63
67
 
64
- const widths = this.columnWidths()
65
- const lines = []
68
+ function renderTable(table) {
69
+ if (!table.headers.length && !table.rows.length) return "";
66
70
 
67
- lines.push(separator(widths))
71
+ const widths = columnWidths(table);
72
+ const lines = [];
68
73
 
69
- if (this.headers.length) {
70
- lines.push(row(this.headers, widths))
71
- lines.push(separator(widths))
72
- }
74
+ lines.push(separator(widths));
73
75
 
74
- for (const r of this.rows) {
75
- lines.push(row(r, widths))
76
- }
76
+ if (table.headers.length) {
77
+ lines.push(row(table.headers, widths));
78
+ lines.push(separator(widths));
79
+ }
77
80
 
78
- lines.push(separator(widths))
79
- return lines.join("\n")
81
+ for (const r of table.rows) {
82
+ lines.push(row(r, widths));
80
83
  }
81
- }
82
84
 
83
- function createTable() {
84
- return new Table()
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
- createList
94
- }
108
+ setHeaders,
109
+ addRow,
110
+ addRows,
111
+ setHeadersFromCSV,
112
+ addRowFromCSV,
113
+ renderTable,
114
+ createList,
115
+ createNumberedList,
116
+ createTableFrom2DArray
117
+ };