lifecycleion 0.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 (177) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +125 -0
  3. package/dist/index.cjs +7 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +2 -0
  6. package/dist/index.d.ts +2 -0
  7. package/dist/index.js +5 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/lib/arrays.cjs +95 -0
  10. package/dist/lib/arrays.cjs.map +1 -0
  11. package/dist/lib/arrays.d.cts +15 -0
  12. package/dist/lib/arrays.d.ts +15 -0
  13. package/dist/lib/arrays.js +63 -0
  14. package/dist/lib/arrays.js.map +1 -0
  15. package/dist/lib/ascii-tables/index.cjs +642 -0
  16. package/dist/lib/ascii-tables/index.cjs.map +1 -0
  17. package/dist/lib/ascii-tables/index.d.cts +66 -0
  18. package/dist/lib/ascii-tables/index.d.ts +66 -0
  19. package/dist/lib/ascii-tables/index.js +603 -0
  20. package/dist/lib/ascii-tables/index.js.map +1 -0
  21. package/dist/lib/clamp.cjs +41 -0
  22. package/dist/lib/clamp.cjs.map +1 -0
  23. package/dist/lib/clamp.d.cts +26 -0
  24. package/dist/lib/clamp.d.ts +26 -0
  25. package/dist/lib/clamp.js +15 -0
  26. package/dist/lib/clamp.js.map +1 -0
  27. package/dist/lib/constants.cjs +73 -0
  28. package/dist/lib/constants.cjs.map +1 -0
  29. package/dist/lib/constants.d.cts +17 -0
  30. package/dist/lib/constants.d.ts +17 -0
  31. package/dist/lib/constants.js +34 -0
  32. package/dist/lib/constants.js.map +1 -0
  33. package/dist/lib/curly-brackets.cjs +77 -0
  34. package/dist/lib/curly-brackets.cjs.map +1 -0
  35. package/dist/lib/curly-brackets.d.cts +17 -0
  36. package/dist/lib/curly-brackets.d.ts +17 -0
  37. package/dist/lib/curly-brackets.js +52 -0
  38. package/dist/lib/curly-brackets.js.map +1 -0
  39. package/dist/lib/deep-clone.cjs +87 -0
  40. package/dist/lib/deep-clone.cjs.map +1 -0
  41. package/dist/lib/deep-clone.d.cts +19 -0
  42. package/dist/lib/deep-clone.d.ts +19 -0
  43. package/dist/lib/deep-clone.js +62 -0
  44. package/dist/lib/deep-clone.js.map +1 -0
  45. package/dist/lib/error-to-string.cjs +743 -0
  46. package/dist/lib/error-to-string.cjs.map +1 -0
  47. package/dist/lib/error-to-string.d.cts +3 -0
  48. package/dist/lib/error-to-string.d.ts +3 -0
  49. package/dist/lib/error-to-string.js +706 -0
  50. package/dist/lib/error-to-string.js.map +1 -0
  51. package/dist/lib/event-emitter.cjs +899 -0
  52. package/dist/lib/event-emitter.cjs.map +1 -0
  53. package/dist/lib/event-emitter.d.cts +78 -0
  54. package/dist/lib/event-emitter.d.ts +78 -0
  55. package/dist/lib/event-emitter.js +861 -0
  56. package/dist/lib/event-emitter.js.map +1 -0
  57. package/dist/lib/id-helpers.cjs +205 -0
  58. package/dist/lib/id-helpers.cjs.map +1 -0
  59. package/dist/lib/id-helpers.d.cts +198 -0
  60. package/dist/lib/id-helpers.d.ts +198 -0
  61. package/dist/lib/id-helpers.js +170 -0
  62. package/dist/lib/id-helpers.js.map +1 -0
  63. package/dist/lib/is-boolean.cjs +33 -0
  64. package/dist/lib/is-boolean.cjs.map +1 -0
  65. package/dist/lib/is-boolean.d.cts +19 -0
  66. package/dist/lib/is-boolean.d.ts +19 -0
  67. package/dist/lib/is-boolean.js +8 -0
  68. package/dist/lib/is-boolean.js.map +1 -0
  69. package/dist/lib/is-function.cjs +33 -0
  70. package/dist/lib/is-function.cjs.map +1 -0
  71. package/dist/lib/is-function.d.cts +3 -0
  72. package/dist/lib/is-function.d.ts +3 -0
  73. package/dist/lib/is-function.js +8 -0
  74. package/dist/lib/is-function.js.map +1 -0
  75. package/dist/lib/is-number.cjs +38 -0
  76. package/dist/lib/is-number.cjs.map +1 -0
  77. package/dist/lib/is-number.d.cts +38 -0
  78. package/dist/lib/is-number.d.ts +38 -0
  79. package/dist/lib/is-number.js +12 -0
  80. package/dist/lib/is-number.js.map +1 -0
  81. package/dist/lib/is-plain-object.cjs +33 -0
  82. package/dist/lib/is-plain-object.cjs.map +1 -0
  83. package/dist/lib/is-plain-object.d.cts +20 -0
  84. package/dist/lib/is-plain-object.d.ts +20 -0
  85. package/dist/lib/is-plain-object.js +8 -0
  86. package/dist/lib/is-plain-object.js.map +1 -0
  87. package/dist/lib/is-promise.cjs +34 -0
  88. package/dist/lib/is-promise.cjs.map +1 -0
  89. package/dist/lib/is-promise.d.cts +3 -0
  90. package/dist/lib/is-promise.d.ts +3 -0
  91. package/dist/lib/is-promise.js +9 -0
  92. package/dist/lib/is-promise.js.map +1 -0
  93. package/dist/lib/json-helpers.cjs +49 -0
  94. package/dist/lib/json-helpers.cjs.map +1 -0
  95. package/dist/lib/json-helpers.d.cts +10 -0
  96. package/dist/lib/json-helpers.d.ts +10 -0
  97. package/dist/lib/json-helpers.js +22 -0
  98. package/dist/lib/json-helpers.js.map +1 -0
  99. package/dist/lib/lifecycle-manager/index.cjs +5594 -0
  100. package/dist/lib/lifecycle-manager/index.cjs.map +1 -0
  101. package/dist/lib/lifecycle-manager/index.d.cts +2044 -0
  102. package/dist/lib/lifecycle-manager/index.d.ts +2044 -0
  103. package/dist/lib/lifecycle-manager/index.js +5543 -0
  104. package/dist/lib/lifecycle-manager/index.js.map +1 -0
  105. package/dist/lib/logger/index.cjs +2514 -0
  106. package/dist/lib/logger/index.cjs.map +1 -0
  107. package/dist/lib/logger/index.d.cts +630 -0
  108. package/dist/lib/logger/index.d.ts +630 -0
  109. package/dist/lib/logger/index.js +2470 -0
  110. package/dist/lib/logger/index.js.map +1 -0
  111. package/dist/lib/padding-utils.cjs +77 -0
  112. package/dist/lib/padding-utils.cjs.map +1 -0
  113. package/dist/lib/padding-utils.d.cts +44 -0
  114. package/dist/lib/padding-utils.d.ts +44 -0
  115. package/dist/lib/padding-utils.js +46 -0
  116. package/dist/lib/padding-utils.js.map +1 -0
  117. package/dist/lib/process-signal-manager.cjs +1306 -0
  118. package/dist/lib/process-signal-manager.cjs.map +1 -0
  119. package/dist/lib/process-signal-manager.d.cts +305 -0
  120. package/dist/lib/process-signal-manager.d.ts +305 -0
  121. package/dist/lib/process-signal-manager.js +1269 -0
  122. package/dist/lib/process-signal-manager.js.map +1 -0
  123. package/dist/lib/promise-protected-resolver.cjs +828 -0
  124. package/dist/lib/promise-protected-resolver.cjs.map +1 -0
  125. package/dist/lib/promise-protected-resolver.d.cts +17 -0
  126. package/dist/lib/promise-protected-resolver.d.ts +17 -0
  127. package/dist/lib/promise-protected-resolver.js +791 -0
  128. package/dist/lib/promise-protected-resolver.js.map +1 -0
  129. package/dist/lib/retry-utils/index.cjs +2183 -0
  130. package/dist/lib/retry-utils/index.cjs.map +1 -0
  131. package/dist/lib/retry-utils/index.d.cts +321 -0
  132. package/dist/lib/retry-utils/index.d.ts +321 -0
  133. package/dist/lib/retry-utils/index.js +2133 -0
  134. package/dist/lib/retry-utils/index.js.map +1 -0
  135. package/dist/lib/safe-handle-callback.cjs +818 -0
  136. package/dist/lib/safe-handle-callback.cjs.map +1 -0
  137. package/dist/lib/safe-handle-callback.d.cts +43 -0
  138. package/dist/lib/safe-handle-callback.d.ts +43 -0
  139. package/dist/lib/safe-handle-callback.js +780 -0
  140. package/dist/lib/safe-handle-callback.js.map +1 -0
  141. package/dist/lib/serialize-error/index.cjs +93 -0
  142. package/dist/lib/serialize-error/index.cjs.map +1 -0
  143. package/dist/lib/serialize-error/index.d.cts +26 -0
  144. package/dist/lib/serialize-error/index.d.ts +26 -0
  145. package/dist/lib/serialize-error/index.js +64 -0
  146. package/dist/lib/serialize-error/index.js.map +1 -0
  147. package/dist/lib/single-event-observer.cjs +841 -0
  148. package/dist/lib/single-event-observer.cjs.map +1 -0
  149. package/dist/lib/single-event-observer.d.cts +54 -0
  150. package/dist/lib/single-event-observer.d.ts +54 -0
  151. package/dist/lib/single-event-observer.js +803 -0
  152. package/dist/lib/single-event-observer.js.map +1 -0
  153. package/dist/lib/sleep.cjs +37 -0
  154. package/dist/lib/sleep.cjs.map +1 -0
  155. package/dist/lib/sleep.d.cts +11 -0
  156. package/dist/lib/sleep.d.ts +11 -0
  157. package/dist/lib/sleep.js +12 -0
  158. package/dist/lib/sleep.js.map +1 -0
  159. package/dist/lib/strings.cjs +186 -0
  160. package/dist/lib/strings.cjs.map +1 -0
  161. package/dist/lib/strings.d.cts +107 -0
  162. package/dist/lib/strings.d.ts +107 -0
  163. package/dist/lib/strings.js +149 -0
  164. package/dist/lib/strings.js.map +1 -0
  165. package/dist/lib/tmp-dir.cjs +254 -0
  166. package/dist/lib/tmp-dir.cjs.map +1 -0
  167. package/dist/lib/tmp-dir.d.cts +63 -0
  168. package/dist/lib/tmp-dir.d.ts +63 -0
  169. package/dist/lib/tmp-dir.js +211 -0
  170. package/dist/lib/tmp-dir.js.map +1 -0
  171. package/dist/lib/unix-time-helpers.cjs +53 -0
  172. package/dist/lib/unix-time-helpers.cjs.map +1 -0
  173. package/dist/lib/unix-time-helpers.d.cts +56 -0
  174. package/dist/lib/unix-time-helpers.d.ts +56 -0
  175. package/dist/lib/unix-time-helpers.js +24 -0
  176. package/dist/lib/unix-time-helpers.js.map +1 -0
  177. package/package.json +220 -0
@@ -0,0 +1,603 @@
1
+ // src/lib/constants.ts
2
+ var BLANK_SPACE = " ";
3
+ var EOL = "\n";
4
+ var DOUBLE_EOL = EOL + EOL;
5
+ var INDENT = " ".repeat(4);
6
+ var DOUBLE_INDENT = INDENT + INDENT;
7
+ var ASCII_LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
8
+ var ASCII_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
9
+ var ASCII_LETTERS = ASCII_LOWERCASE + ASCII_UPPERCASE;
10
+ var DIGITS = "0123456789";
11
+ var HEX_DIGITS = DIGITS + "abcdefABCDEF";
12
+ var PUNCTUATION = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
13
+ var WHITESPACE = " \n\r\v\f";
14
+ var PRINTABLE = DIGITS + ASCII_LETTERS + PUNCTUATION + WHITESPACE;
15
+
16
+ // src/lib/padding-utils.ts
17
+ function padLeft(str, length, padStr = BLANK_SPACE) {
18
+ return str.padStart(length, padStr);
19
+ }
20
+ function padRight(str, length, padStr = BLANK_SPACE) {
21
+ return str.padEnd(length, padStr);
22
+ }
23
+ function padCenter(str, length, prefer = "left", padStr = BLANK_SPACE) {
24
+ const midStrLength = length - str.length;
25
+ if (midStrLength > 0) {
26
+ const padLeftAmount = prefer === "left" ? Math.ceil(midStrLength / 2) : Math.floor(midStrLength / 2);
27
+ const padRightAmount = prefer === "left" ? Math.floor(midStrLength / 2) : Math.ceil(midStrLength / 2);
28
+ return padLeft("", padLeftAmount, padStr) + str + padRight("", padRightAmount, padStr);
29
+ } else {
30
+ return str;
31
+ }
32
+ }
33
+ function padCenterPreferRight(str, length, padStr = BLANK_SPACE) {
34
+ return padCenter(str, length, "right", padStr);
35
+ }
36
+
37
+ // src/lib/ascii-tables/ascii-table-utils.ts
38
+ import stringWidth from "string-width";
39
+
40
+ // src/lib/strings.ts
41
+ function isString(value) {
42
+ return typeof value === "string";
43
+ }
44
+ function splitGraphemes(text) {
45
+ const graphemes = [];
46
+ let grapheme = "";
47
+ let zwjSequence = "";
48
+ for (let i = 0; i < text.length; i++) {
49
+ const char = text[i];
50
+ const nextChar = text[i + 1] || "";
51
+ const code = char.charCodeAt(0);
52
+ if (code >= 768 && code <= 879 || // Combining Diacritical Marks
53
+ code >= 6832 && code <= 6911 || // Combining Diacritical Marks Extended
54
+ code >= 7616 && code <= 7679 || // Combining Diacritical Marks Supplement
55
+ code >= 65056 && code <= 65071 || // Combining Half Marks
56
+ code >= 3633 && code <= 3642 || // Thai combining marks
57
+ code >= 3655 && code <= 3662) {
58
+ grapheme += char;
59
+ } else if (char === "\u200D") {
60
+ zwjSequence += grapheme + char;
61
+ grapheme = "";
62
+ } else {
63
+ if (grapheme) {
64
+ if (zwjSequence) {
65
+ graphemes.push(zwjSequence + grapheme);
66
+ zwjSequence = "";
67
+ } else {
68
+ graphemes.push(grapheme);
69
+ }
70
+ }
71
+ grapheme = char;
72
+ if (char >= "\uD800" && char <= "\uDBFF" && nextChar >= "\uDC00" && nextChar <= "\uDFFF") {
73
+ grapheme += nextChar;
74
+ i++;
75
+ }
76
+ }
77
+ }
78
+ if (grapheme) {
79
+ if (zwjSequence) {
80
+ graphemes.push(zwjSequence + grapheme);
81
+ } else {
82
+ graphemes.push(grapheme);
83
+ }
84
+ }
85
+ return graphemes;
86
+ }
87
+
88
+ // src/lib/ascii-tables/ascii-table-utils.ts
89
+ var ASCIITableUtils = class _ASCIITableUtils {
90
+ static centerText(text, width) {
91
+ return padCenterPreferRight(text, width, " ");
92
+ }
93
+ static createSeparator(columnWidths, character = "=") {
94
+ const totalWidth = columnWidths.reduce((sum, width) => sum + width + 3, 0) - 1;
95
+ return `+${padRight("", totalWidth, character)}+`;
96
+ }
97
+ static wrapText(text, maxLength) {
98
+ const words = text.split(" ");
99
+ const lines = [];
100
+ let currentLine = "";
101
+ for (const word of words) {
102
+ if (stringWidth(currentLine) + stringWidth(word) + 1 <= maxLength) {
103
+ currentLine += (currentLine ? " " : "") + word;
104
+ } else {
105
+ if (currentLine) {
106
+ lines.push(currentLine);
107
+ }
108
+ if (stringWidth(word) <= maxLength) {
109
+ currentLine = word;
110
+ } else {
111
+ const subWords = _ASCIITableUtils.splitWord(word, maxLength);
112
+ lines.push(...subWords.slice(0, -1));
113
+ currentLine = subWords[subWords.length - 1];
114
+ }
115
+ }
116
+ }
117
+ if (currentLine) {
118
+ lines.push(currentLine);
119
+ }
120
+ return lines;
121
+ }
122
+ static splitWord(word, maxLength) {
123
+ const graphemes = splitGraphemes(word);
124
+ const subWords = [];
125
+ let currentSubWord = "";
126
+ for (const grapheme of graphemes) {
127
+ if (stringWidth(currentSubWord + grapheme) <= maxLength) {
128
+ currentSubWord += grapheme;
129
+ } else {
130
+ subWords.push(currentSubWord);
131
+ currentSubWord = grapheme;
132
+ }
133
+ }
134
+ if (currentSubWord) {
135
+ subWords.push(currentSubWord);
136
+ }
137
+ return subWords;
138
+ }
139
+ };
140
+
141
+ // src/lib/ascii-tables/multi-column-ascii-table.ts
142
+ import stringWidth2 from "string-width";
143
+ var MultiColumnASCIITable = class {
144
+ headers;
145
+ rows;
146
+ tableWidth;
147
+ emptyMessage;
148
+ widthMode;
149
+ constructor(headers, options = {}) {
150
+ this.headers = headers;
151
+ this.rows = [];
152
+ this.tableWidth = options.tableWidth || 80;
153
+ this.emptyMessage = options.emptyMessage || "";
154
+ this.widthMode = options.widthMode || "flex";
155
+ const minTableWidth = this.getMinimumWidth();
156
+ if (this.tableWidth < minTableWidth) {
157
+ throw new Error(
158
+ `Table width must be at least ${minTableWidth} to accommodate the headers.`
159
+ );
160
+ }
161
+ }
162
+ getMinimumWidth() {
163
+ return this.headers.length * 4 + 1;
164
+ }
165
+ addRow(row) {
166
+ if (row.length !== this.headers.length) {
167
+ throw new Error(
168
+ `Number of values in the row (${row.length}) must match the number of headers (${this.headers.length}).`
169
+ );
170
+ }
171
+ this.rows.push(row);
172
+ }
173
+ toString(options = {}) {
174
+ const tableWidth = options.tableWidth || this.tableWidth;
175
+ const emptyMessage = options.emptyMessage || this.emptyMessage;
176
+ if (this.rows.length === 0) {
177
+ const emptyTableWidth = Math.min(tableWidth, 40);
178
+ const separator = "+" + "-".repeat(emptyTableWidth - 2) + "+";
179
+ const emptyMessageLines = ASCIITableUtils.wrapText(
180
+ emptyMessage,
181
+ emptyTableWidth - 4
182
+ );
183
+ const emptyRows = emptyMessageLines.map((line) => {
184
+ const paddingLeft = " ".repeat(
185
+ Math.floor((emptyTableWidth - stringWidth2(line) - 4) / 2)
186
+ );
187
+ const paddingRight = " ".repeat(
188
+ Math.ceil((emptyTableWidth - stringWidth2(line) - 4) / 2)
189
+ );
190
+ return `| ${paddingLeft}${line}${paddingRight} |`;
191
+ });
192
+ if (emptyRows.length === 0) {
193
+ emptyRows.push(`| ${" ".repeat(emptyTableWidth - 4)} |`);
194
+ }
195
+ return [separator, ...emptyRows, separator].join("\n");
196
+ }
197
+ const columnWidths = this.calculateColumnWidths(options);
198
+ const headerSeparator = ASCIITableUtils.createSeparator(columnWidths);
199
+ const rowSeparator = ASCIITableUtils.createSeparator(columnWidths, "-");
200
+ let tableString = headerSeparator + "\n";
201
+ const header = this.renderRow(this.headers, columnWidths);
202
+ tableString += header + "\n" + rowSeparator + "\n";
203
+ const rows = this.rows.map((row) => {
204
+ return this.renderRow(row, columnWidths);
205
+ });
206
+ tableString += rows.join("\n" + rowSeparator + "\n");
207
+ tableString += "\n" + headerSeparator;
208
+ return tableString;
209
+ }
210
+ calculateColumnWidths(options = {}) {
211
+ const tableWidth = options.tableWidth || this.tableWidth;
212
+ const widthMode = options.widthMode || this.widthMode;
213
+ const numColumns = this.headers.length;
214
+ if (widthMode === "fixed") {
215
+ const availableWidth2 = tableWidth - (numColumns + 1) * 3 + 1;
216
+ const columnWidth = Math.floor(availableWidth2 / numColumns);
217
+ const extraWidth = availableWidth2 % numColumns;
218
+ const columnWidths = new Array(numColumns).fill(columnWidth);
219
+ for (let i = 0; i < extraWidth; i++) {
220
+ columnWidths[i] += 1;
221
+ }
222
+ return columnWidths;
223
+ }
224
+ const availableWidth = tableWidth - (numColumns + 1) * 3 + 1;
225
+ const maxColumnWidth = Math.floor(availableWidth / numColumns);
226
+ const totalContentWidth = this.headers.reduce(
227
+ (sum, header) => sum + stringWidth2(header),
228
+ 0
229
+ );
230
+ if (availableWidth >= totalContentWidth) {
231
+ const remainingWidth = availableWidth - totalContentWidth;
232
+ const extraCharWidth = Math.floor(remainingWidth / numColumns);
233
+ const extraCharRemainder = remainingWidth % numColumns;
234
+ const columnWidths = this.headers.map((header, index) => {
235
+ const extraWidth = index < extraCharRemainder ? 1 : 0;
236
+ return stringWidth2(header) + extraCharWidth + extraWidth;
237
+ });
238
+ return columnWidths;
239
+ } else {
240
+ const columnWidths = this.headers.map(() => maxColumnWidth);
241
+ return columnWidths;
242
+ }
243
+ }
244
+ renderRow(row, columnWidths) {
245
+ const wrappedCells = row.map((value, index) => {
246
+ const wrappedLines = ASCIITableUtils.wrapText(value, columnWidths[index]);
247
+ return wrappedLines.map((line) => line.padEnd(columnWidths[index])).join("\n");
248
+ });
249
+ const maxLines = Math.max(
250
+ ...wrappedCells.map((cell) => cell.split("\n").length)
251
+ );
252
+ const paddedRows = [];
253
+ for (let i = 0; i < maxLines; i++) {
254
+ const rowLine = wrappedCells.map((cell, index) => {
255
+ const cellLines = cell.split("\n");
256
+ const cellLine = cellLines[i] || "";
257
+ const padding = " ".repeat(columnWidths[index] - stringWidth2(cellLine));
258
+ return " " + cellLine + padding + " ";
259
+ });
260
+ paddedRows.push("|" + rowLine.join("|") + "|");
261
+ }
262
+ return paddedRows.join("\n");
263
+ }
264
+ };
265
+
266
+ // src/lib/ascii-tables/key-value-ascii-table.ts
267
+ import stringWidth3 from "string-width";
268
+
269
+ // src/lib/clamp.ts
270
+ function clamp(value, min, max) {
271
+ return Math.max(min, Math.min(value, max));
272
+ }
273
+
274
+ // src/lib/ascii-tables/key-value-ascii-table.ts
275
+ var KeyValueASCIITable = class _KeyValueASCIITable {
276
+ tableWidth;
277
+ emptyMessage;
278
+ autoAdjustWidthWhenPossible = true;
279
+ rows = [];
280
+ constructor(options = {}) {
281
+ const minTableWidth = this.getMinimumWidth();
282
+ if (options.tableWidth && options.tableWidth < minTableWidth) {
283
+ throw new Error(
284
+ `Table width must be at least ${minTableWidth} to accommodate the table structure.`
285
+ );
286
+ }
287
+ this.tableWidth = options.tableWidth || 80;
288
+ this.autoAdjustWidthWhenPossible = options.autoAdjustWidthWhenPossible ?? true;
289
+ this.emptyMessage = options.emptyMessage || "";
290
+ }
291
+ getMinimumWidth() {
292
+ return 9;
293
+ }
294
+ /**
295
+ * Adds key and value to the table but placing the value on its own row.
296
+ *
297
+ * @param key
298
+ * @param value
299
+ */
300
+ addValueOnSeparateRow(key, value) {
301
+ const row = { kind: "own", key, value };
302
+ this.rows.push(row);
303
+ }
304
+ /**
305
+ * Adds key and value to the table.
306
+ *
307
+ * If provided value is an instance of ASCIITable or MultiColumnASCIITable, it will be rendered as a nested table on its own row for readability.
308
+ *
309
+ * @param key
310
+ * @param value
311
+ */
312
+ addRow(key, value) {
313
+ const row = { kind: "regular", key, value };
314
+ this.rows.push(row);
315
+ }
316
+ toString(options = {}) {
317
+ const tableWidth = options.tableWidth || this.tableWidth;
318
+ const canAutoAdjustWidthWhenPossible = options.autoAdjustWidthWhenPossible ?? this.autoAdjustWidthWhenPossible;
319
+ const emptyMessage = options.emptyMessage || this.emptyMessage;
320
+ if (this.rows.length === 0) {
321
+ const emptyTableWidth = Math.min(tableWidth, 40);
322
+ const separator = "+" + "-".repeat(emptyTableWidth - 2) + "+";
323
+ const emptyMessageLines = ASCIITableUtils.wrapText(
324
+ emptyMessage,
325
+ emptyTableWidth - 4
326
+ );
327
+ const emptyRows = emptyMessageLines.map((line) => {
328
+ const paddingLeft = padRight(
329
+ "",
330
+ Math.floor((emptyTableWidth - stringWidth3(line) - 4) / 2),
331
+ " "
332
+ );
333
+ const paddingRight = padRight(
334
+ "",
335
+ Math.ceil((emptyTableWidth - stringWidth3(line) - 4) / 2),
336
+ " "
337
+ );
338
+ return `| ${paddingLeft}${line}${paddingRight} |`;
339
+ });
340
+ if (emptyRows.length === 0) {
341
+ emptyRows.push(`| ${" ".repeat(emptyTableWidth - 4)} |`);
342
+ }
343
+ return [separator, ...emptyRows, separator].join("\n");
344
+ }
345
+ const columnWidths = this.calculateColumnWidths(options);
346
+ const headerSeparator = ASCIITableUtils.createSeparator(columnWidths);
347
+ const rowSeparator = ASCIITableUtils.createSeparator(columnWidths, "-");
348
+ let tableString = headerSeparator + "\n";
349
+ for (const [rowIndex, row] of this.rows.entries()) {
350
+ const { kind, key, value } = row;
351
+ if (kind === "own" || value instanceof _KeyValueASCIITable || value instanceof MultiColumnASCIITable || Array.isArray(value)) {
352
+ const keyString = ASCIITableUtils.centerText(
353
+ key,
354
+ columnWidths[0] + columnWidths[1] + 3
355
+ );
356
+ tableString += `| ${keyString} |
357
+ `;
358
+ tableString += rowSeparator + "\n";
359
+ let valueString = "";
360
+ if (value instanceof _KeyValueASCIITable) {
361
+ valueString = this.formatValue(
362
+ value,
363
+ tableWidth - 4,
364
+ canAutoAdjustWidthWhenPossible,
365
+ ""
366
+ );
367
+ } else if (value instanceof MultiColumnASCIITable) {
368
+ valueString = this.formatValue(
369
+ value,
370
+ tableWidth - 4,
371
+ canAutoAdjustWidthWhenPossible,
372
+ ""
373
+ );
374
+ } else if (Array.isArray(value)) {
375
+ valueString = this.formatValue(
376
+ value,
377
+ tableWidth - 4,
378
+ canAutoAdjustWidthWhenPossible,
379
+ ""
380
+ );
381
+ } else if (row.kind === "own") {
382
+ valueString = this.formatTableRowOnOwnRow(
383
+ value,
384
+ tableWidth - 4,
385
+ tableWidth
386
+ );
387
+ }
388
+ const valueLines = valueString.split("\n");
389
+ const paddedValueLines = valueLines.map((line) => {
390
+ const padding = padRight("", tableWidth - stringWidth3(line) - 4, " ");
391
+ return `| ${line}${padding} |`;
392
+ });
393
+ tableString += paddedValueLines.join("\n") + "\n";
394
+ tableString += headerSeparator + "\n";
395
+ } else {
396
+ const keyLines = ASCIITableUtils.wrapText(key, columnWidths[0]);
397
+ const valueLines = ASCIITableUtils.wrapText(
398
+ this.formatValue(
399
+ value,
400
+ columnWidths[1],
401
+ canAutoAdjustWidthWhenPossible,
402
+ ""
403
+ ),
404
+ columnWidths[1]
405
+ );
406
+ const maxLines = Math.max(keyLines.length, valueLines.length);
407
+ for (let i = 0; i < maxLines; i++) {
408
+ const keyLine = keyLines[i] || "";
409
+ const valueLine = valueLines[i] || "";
410
+ const keyPadding = " ".repeat(columnWidths[0] - stringWidth3(keyLine));
411
+ const valuePadding = " ".repeat(
412
+ columnWidths[1] - stringWidth3(valueLine)
413
+ );
414
+ tableString += `| ${keyLine}${keyPadding} | ${valueLine}${valuePadding} |
415
+ `;
416
+ if (i === maxLines - 1) {
417
+ if (rowIndex === this.rows.length - 1) {
418
+ tableString += headerSeparator + "\n";
419
+ } else {
420
+ tableString += rowSeparator + "\n";
421
+ }
422
+ }
423
+ }
424
+ }
425
+ }
426
+ return tableString.trim();
427
+ }
428
+ calculateColumnWidths(options = {}) {
429
+ const tableWidth = options.tableWidth || this.tableWidth;
430
+ const canAutoAdjustWidthWhenPossible = options.autoAdjustWidthWhenPossible ?? this.autoAdjustWidthWhenPossible;
431
+ const columnWidths = [0, 0];
432
+ for (const row of this.rows) {
433
+ const { key, value } = row;
434
+ const keyWidth = stringWidth3(key);
435
+ const maxKeyWidth = Math.floor((tableWidth - 7) / 2);
436
+ if (keyWidth > columnWidths[0]) {
437
+ columnWidths[0] = Math.min(keyWidth, maxKeyWidth);
438
+ columnWidths[1] = Math.max(0, tableWidth - columnWidths[0] - 7);
439
+ }
440
+ if (typeof value === "string") {
441
+ const valueWidth = Math.max(
442
+ ...value.split("\n").map((line) => stringWidth3(line))
443
+ );
444
+ if (valueWidth > columnWidths[1]) {
445
+ columnWidths[1] = Math.min(
446
+ valueWidth,
447
+ tableWidth - columnWidths[0] - 7
448
+ );
449
+ columnWidths[0] = Math.max(0, tableWidth - columnWidths[1] - 7);
450
+ }
451
+ } else if (row.kind === "own") {
452
+ let valueWidth = 0;
453
+ if (isString(value)) {
454
+ valueWidth = Math.max(
455
+ ...value.split("\n").map((line) => stringWidth3(line))
456
+ );
457
+ }
458
+ if (valueWidth > columnWidths[1]) {
459
+ columnWidths[1] = Math.min(
460
+ valueWidth,
461
+ tableWidth - columnWidths[0] - 7
462
+ );
463
+ columnWidths[0] = Math.max(0, tableWidth - columnWidths[1] - 7);
464
+ }
465
+ } else if (value instanceof _KeyValueASCIITable) {
466
+ let nestedTableColumnWidths = [];
467
+ if (canAutoAdjustWidthWhenPossible) {
468
+ const minWidth = value.getMinimumWidth();
469
+ const availableWidth2 = tableWidth - columnWidths[0] - 7;
470
+ const adjustedWidth = clamp(availableWidth2, minWidth, availableWidth2);
471
+ nestedTableColumnWidths = value.calculateColumnWidths({
472
+ tableWidth: adjustedWidth
473
+ });
474
+ } else {
475
+ nestedTableColumnWidths = value.calculateColumnWidths();
476
+ }
477
+ const nestedTableWidth = nestedTableColumnWidths.reduce((sum, width) => sum + width, 0) + nestedTableColumnWidths.length * 3 - 1;
478
+ const availableWidth = tableWidth - columnWidths[0] - 7;
479
+ if (nestedTableWidth > availableWidth) {
480
+ columnWidths[1] = availableWidth;
481
+ } else {
482
+ columnWidths[1] = Math.max(columnWidths[1], nestedTableWidth);
483
+ }
484
+ } else if (value instanceof MultiColumnASCIITable) {
485
+ let nestedTableColumnWidths = [];
486
+ if (canAutoAdjustWidthWhenPossible) {
487
+ const minWidth = value.getMinimumWidth();
488
+ const availableWidth2 = tableWidth - columnWidths[0] - 7;
489
+ const adjustedWidth = clamp(availableWidth2, minWidth, availableWidth2);
490
+ nestedTableColumnWidths = value.calculateColumnWidths({
491
+ tableWidth: adjustedWidth
492
+ });
493
+ } else {
494
+ nestedTableColumnWidths = value.calculateColumnWidths();
495
+ }
496
+ const nestedTableWidth = nestedTableColumnWidths.reduce((sum, width) => sum + width, 0) + nestedTableColumnWidths.length * 3 - 1;
497
+ const availableWidth = tableWidth - columnWidths[0] - 7;
498
+ if (nestedTableWidth > availableWidth) {
499
+ columnWidths[1] = availableWidth;
500
+ } else {
501
+ columnWidths[1] = Math.max(columnWidths[1], nestedTableWidth);
502
+ }
503
+ } else if (Array.isArray(value)) {
504
+ for (const nestedCell of value) {
505
+ const nestedKeyWidth = stringWidth3(nestedCell.key);
506
+ const maxNestedKeyWidth = Math.floor((tableWidth - 7) / 2);
507
+ if (nestedKeyWidth > columnWidths[0]) {
508
+ columnWidths[0] = Math.min(nestedKeyWidth, maxNestedKeyWidth);
509
+ columnWidths[1] = Math.max(0, tableWidth - columnWidths[0] - 7);
510
+ }
511
+ if (typeof nestedCell.value === "string") {
512
+ const nestedValueWidth = Math.max(
513
+ ...nestedCell.value.split("\n").map((line) => stringWidth3(line))
514
+ );
515
+ if (nestedValueWidth > columnWidths[1]) {
516
+ columnWidths[1] = Math.min(
517
+ nestedValueWidth,
518
+ tableWidth - columnWidths[0] - 7
519
+ );
520
+ columnWidths[0] = Math.max(0, tableWidth - columnWidths[1] - 7);
521
+ }
522
+ }
523
+ }
524
+ }
525
+ }
526
+ return columnWidths;
527
+ }
528
+ formatValue(value, cellWidth, canAutoAdjustWidthWhenPossible, indent = "") {
529
+ if (typeof value === "string") {
530
+ return value;
531
+ } else if (typeof value === "number") {
532
+ return String(value);
533
+ } else if (typeof value === "boolean") {
534
+ return String(value);
535
+ } else if (value === null) {
536
+ return "null";
537
+ } else if (value === void 0) {
538
+ return "undefined";
539
+ } else if (value instanceof _KeyValueASCIITable) {
540
+ let nestedTableLines;
541
+ if (canAutoAdjustWidthWhenPossible) {
542
+ const minWidth = value.getMinimumWidth();
543
+ const adjustedWidth = clamp(cellWidth, minWidth, cellWidth);
544
+ nestedTableLines = value.toString({ tableWidth: adjustedWidth }).split("\n");
545
+ } else {
546
+ nestedTableLines = value.toString().split("\n");
547
+ }
548
+ const indentedLines = nestedTableLines.map((line) => `${indent}${line}`);
549
+ return indentedLines.join("\n");
550
+ } else if (value instanceof MultiColumnASCIITable) {
551
+ let nestedTableLines;
552
+ if (canAutoAdjustWidthWhenPossible) {
553
+ const minWidth = value.getMinimumWidth();
554
+ const adjustedWidth = clamp(cellWidth, minWidth, cellWidth);
555
+ nestedTableLines = value.toString({ tableWidth: adjustedWidth }).split("\n");
556
+ } else {
557
+ nestedTableLines = value.toString().split("\n");
558
+ }
559
+ const indentedLines = nestedTableLines.map((line) => `${indent}${line}`);
560
+ return indentedLines.join("\n");
561
+ } else if (Array.isArray(value)) {
562
+ const nestedValueLines = [];
563
+ for (const { key, value: nestedValue } of value) {
564
+ const formattedKey = `${indent}${key}:`;
565
+ const formattedValue = this.formatValue(
566
+ nestedValue,
567
+ cellWidth - indent.length - stringWidth3(key) - 2,
568
+ canAutoAdjustWidthWhenPossible,
569
+ `${indent}`
570
+ );
571
+ const wrappedSpacer = padRight("", 4, " ");
572
+ const wrappedValue = formattedValue.split("\n").map((line) => `${indent}${wrappedSpacer}${line}`);
573
+ nestedValueLines.push(formattedKey);
574
+ nestedValueLines.push(...wrappedValue);
575
+ nestedValueLines.push("");
576
+ }
577
+ return nestedValueLines.slice(0, -1).join("\n");
578
+ } else {
579
+ throw new TypeError("Invalid value type provided");
580
+ }
581
+ }
582
+ formatTableRowOnOwnRow(value, width, maxRowLength) {
583
+ const lines = value.split("\n");
584
+ const paddedLines = lines.map((line) => {
585
+ const wrappedLines = ASCIITableUtils.wrapText(line, maxRowLength - 4);
586
+ return wrappedLines.map((wrappedLine) => {
587
+ const padding = padRight(
588
+ "",
589
+ width - stringWidth3(wrappedLine) - 2,
590
+ " "
591
+ );
592
+ return `${wrappedLine}${padding}`;
593
+ }).join("\n");
594
+ });
595
+ return paddedLines.join("\n");
596
+ }
597
+ };
598
+ export {
599
+ ASCIITableUtils,
600
+ KeyValueASCIITable,
601
+ MultiColumnASCIITable
602
+ };
603
+ //# sourceMappingURL=index.js.map