@cj-tech-master/excelts 7.6.0 → 8.0.0

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 (79) hide show
  1. package/README.md +99 -577
  2. package/README_zh.md +101 -577
  3. package/dist/browser/index.browser.d.ts +3 -0
  4. package/dist/browser/index.browser.js +2 -0
  5. package/dist/browser/index.d.ts +3 -0
  6. package/dist/browser/index.js +2 -0
  7. package/dist/browser/modules/archive/compression/compress.browser.js +4 -4
  8. package/dist/browser/modules/archive/compression/deflate-fallback.d.ts +24 -22
  9. package/dist/browser/modules/archive/compression/deflate-fallback.js +664 -360
  10. package/dist/browser/modules/archive/compression/streaming-compress.browser.d.ts +7 -0
  11. package/dist/browser/modules/archive/compression/streaming-compress.browser.js +15 -3
  12. package/dist/browser/modules/archive/compression/streaming-compress.d.ts +5 -0
  13. package/dist/browser/modules/archive/compression/streaming-compress.js +7 -0
  14. package/dist/browser/modules/archive/zip/stream.js +27 -3
  15. package/dist/browser/modules/excel/workbook.browser.d.ts +72 -0
  16. package/dist/browser/modules/excel/workbook.browser.js +226 -0
  17. package/dist/browser/modules/excel/workbook.d.ts +32 -1
  18. package/dist/browser/modules/excel/workbook.js +47 -2
  19. package/dist/browser/modules/excel/xlsx/xlsx.browser.js +42 -4
  20. package/dist/browser/modules/markdown/constants.d.ts +30 -0
  21. package/dist/browser/modules/markdown/constants.js +30 -0
  22. package/dist/browser/modules/markdown/errors.d.ts +21 -0
  23. package/dist/browser/modules/markdown/errors.js +23 -0
  24. package/dist/browser/modules/markdown/format/index.d.ts +54 -0
  25. package/dist/browser/modules/markdown/format/index.js +307 -0
  26. package/dist/browser/modules/markdown/index.d.ts +15 -0
  27. package/dist/browser/modules/markdown/index.js +22 -0
  28. package/dist/browser/modules/markdown/parse/index.d.ts +70 -0
  29. package/dist/browser/modules/markdown/parse/index.js +428 -0
  30. package/dist/browser/modules/markdown/types.d.ts +130 -0
  31. package/dist/browser/modules/markdown/types.js +6 -0
  32. package/dist/cjs/index.js +5 -1
  33. package/dist/cjs/modules/archive/compression/compress.browser.js +4 -4
  34. package/dist/cjs/modules/archive/compression/deflate-fallback.js +664 -360
  35. package/dist/cjs/modules/archive/compression/streaming-compress.browser.js +15 -2
  36. package/dist/cjs/modules/archive/compression/streaming-compress.js +8 -0
  37. package/dist/cjs/modules/archive/zip/stream.js +26 -2
  38. package/dist/cjs/modules/excel/workbook.browser.js +226 -0
  39. package/dist/cjs/modules/excel/workbook.js +46 -1
  40. package/dist/cjs/modules/excel/xlsx/xlsx.browser.js +42 -4
  41. package/dist/cjs/modules/markdown/constants.js +33 -0
  42. package/dist/cjs/modules/markdown/errors.js +28 -0
  43. package/dist/cjs/modules/markdown/format/index.js +310 -0
  44. package/dist/cjs/modules/markdown/index.js +30 -0
  45. package/dist/cjs/modules/markdown/parse/index.js +432 -0
  46. package/dist/cjs/modules/markdown/types.js +7 -0
  47. package/dist/esm/index.browser.js +2 -0
  48. package/dist/esm/index.js +2 -0
  49. package/dist/esm/modules/archive/compression/compress.browser.js +4 -4
  50. package/dist/esm/modules/archive/compression/deflate-fallback.js +664 -360
  51. package/dist/esm/modules/archive/compression/streaming-compress.browser.js +15 -3
  52. package/dist/esm/modules/archive/compression/streaming-compress.js +7 -0
  53. package/dist/esm/modules/archive/zip/stream.js +27 -3
  54. package/dist/esm/modules/excel/workbook.browser.js +226 -0
  55. package/dist/esm/modules/excel/workbook.js +47 -2
  56. package/dist/esm/modules/excel/xlsx/xlsx.browser.js +42 -4
  57. package/dist/esm/modules/markdown/constants.js +30 -0
  58. package/dist/esm/modules/markdown/errors.js +23 -0
  59. package/dist/esm/modules/markdown/format/index.js +307 -0
  60. package/dist/esm/modules/markdown/index.js +22 -0
  61. package/dist/esm/modules/markdown/parse/index.js +428 -0
  62. package/dist/esm/modules/markdown/types.js +6 -0
  63. package/dist/iife/excelts.iife.js +1342 -283
  64. package/dist/iife/excelts.iife.js.map +1 -1
  65. package/dist/iife/excelts.iife.min.js +38 -34
  66. package/dist/types/index.browser.d.ts +3 -0
  67. package/dist/types/index.d.ts +3 -0
  68. package/dist/types/modules/archive/compression/deflate-fallback.d.ts +24 -22
  69. package/dist/types/modules/archive/compression/streaming-compress.browser.d.ts +7 -0
  70. package/dist/types/modules/archive/compression/streaming-compress.d.ts +5 -0
  71. package/dist/types/modules/excel/workbook.browser.d.ts +72 -0
  72. package/dist/types/modules/excel/workbook.d.ts +32 -1
  73. package/dist/types/modules/markdown/constants.d.ts +30 -0
  74. package/dist/types/modules/markdown/errors.d.ts +21 -0
  75. package/dist/types/modules/markdown/format/index.d.ts +54 -0
  76. package/dist/types/modules/markdown/index.d.ts +15 -0
  77. package/dist/types/modules/markdown/parse/index.d.ts +70 -0
  78. package/dist/types/modules/markdown/types.d.ts +130 -0
  79. package/package.json +56 -32
@@ -0,0 +1,310 @@
1
+ "use strict";
2
+ /**
3
+ * Markdown Table Formatter
4
+ *
5
+ * Formats data into well-formed Markdown table strings.
6
+ *
7
+ * Features:
8
+ * - Auto column width calculation with padding
9
+ * - Column alignment (left, center, right, none)
10
+ * - Proper escaping of pipe characters and backslashes
11
+ * - Compact mode (disable column-width alignment) for minimal output
12
+ * - Configurable column definitions
13
+ * - Multiline cell content (newlines converted to `<br>`)
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * // Simple array data with headers
18
+ * formatMarkdown(["Name", "Age"], [["Alice", "30"], ["Bob", "25"]]);
19
+ * // | Name | Age |
20
+ * // | ----- | --- |
21
+ * // | Alice | 30 |
22
+ * // | Bob | 25 |
23
+ *
24
+ * // With alignment
25
+ * formatMarkdown(["Left", "Center", "Right"], data, {
26
+ * alignment: "left",
27
+ * columns: [
28
+ * { header: "Left", alignment: "left" },
29
+ * { header: "Center", alignment: "center" },
30
+ * { header: "Right", alignment: "right" }
31
+ * ]
32
+ * });
33
+ * ```
34
+ */
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.formatMarkdown = formatMarkdown;
37
+ const constants_1 = require("../constants");
38
+ // =============================================================================
39
+ // Unicode Display Width
40
+ // =============================================================================
41
+ /**
42
+ * Calculate the display width of a string in a monospace terminal.
43
+ * CJK characters, fullwidth forms, and most emoji are 2 columns wide.
44
+ * This enables proper column alignment in tables containing these characters.
45
+ */
46
+ function displayWidth(str) {
47
+ let width = 0;
48
+ for (let i = 0; i < str.length; i++) {
49
+ const code = str.charCodeAt(i);
50
+ // Handle surrogate pairs (emoji and supplementary plane characters)
51
+ if (code >= 0xd800 && code <= 0xdbff && i + 1 < str.length) {
52
+ const low = str.charCodeAt(i + 1);
53
+ if (low >= 0xdc00 && low <= 0xdfff) {
54
+ width += 2;
55
+ i++;
56
+ continue;
57
+ }
58
+ }
59
+ // Zero-width characters
60
+ if (code === 0x200b || // zero-width space
61
+ code === 0x200c || // zero-width non-joiner
62
+ code === 0x200d || // zero-width joiner
63
+ code === 0xfeff // BOM / zero-width no-break space
64
+ ) {
65
+ continue;
66
+ }
67
+ // Combining marks (general categories Mn, Mc, Me)
68
+ if ((code >= 0x0300 && code <= 0x036f) || // Combining Diacritical Marks
69
+ (code >= 0x1ab0 && code <= 0x1aff) || // Combining Diacritical Marks Extended
70
+ (code >= 0x1dc0 && code <= 0x1dff) || // Combining Diacritical Marks Supplement
71
+ (code >= 0x20d0 && code <= 0x20ff) || // Combining Diacritical Marks for Symbols
72
+ (code >= 0xfe20 && code <= 0xfe2f) // Combining Half Marks
73
+ ) {
74
+ continue;
75
+ }
76
+ // Fullwidth and wide characters: CJK, Hangul, Katakana/Hiragana, etc.
77
+ if ((code >= 0x1100 && code <= 0x115f) || // Hangul Jamo
78
+ (code >= 0x2e80 && code <= 0x303e) || // CJK Radicals, Kangxi, CJK Symbols
79
+ (code >= 0x3040 && code <= 0x33bf) || // Hiragana, Katakana, Bopomofo, CJK Compat
80
+ (code >= 0x3400 && code <= 0x4dbf) || // CJK Unified Ideographs Extension A
81
+ (code >= 0x4e00 && code <= 0xa4cf) || // CJK Unified Ideographs, Yi
82
+ (code >= 0xa960 && code <= 0xa97f) || // Hangul Jamo Extended-A
83
+ (code >= 0xac00 && code <= 0xd7af) || // Hangul Syllables
84
+ (code >= 0xf900 && code <= 0xfaff) || // CJK Compatibility Ideographs
85
+ (code >= 0xfe10 && code <= 0xfe19) || // Vertical forms
86
+ (code >= 0xfe30 && code <= 0xfe6f) || // CJK Compatibility Forms
87
+ (code >= 0xff01 && code <= 0xff60) || // Fullwidth ASCII/Latin
88
+ (code >= 0xffe0 && code <= 0xffe6) // Fullwidth Signs
89
+ ) {
90
+ width += 2;
91
+ continue;
92
+ }
93
+ width += 1;
94
+ }
95
+ return width;
96
+ }
97
+ // =============================================================================
98
+ // Internal Helpers
99
+ // =============================================================================
100
+ /**
101
+ * Default value-to-string converter.
102
+ */
103
+ function defaultStringify(value) {
104
+ if (value === null || value === undefined) {
105
+ return "";
106
+ }
107
+ if (value instanceof Date) {
108
+ return value.toISOString();
109
+ }
110
+ if (typeof value === "object") {
111
+ try {
112
+ return JSON.stringify(value);
113
+ }
114
+ catch {
115
+ return "[object Object]";
116
+ }
117
+ }
118
+ return String(value);
119
+ }
120
+ /**
121
+ * Escape pipe characters, backslashes, and convert newlines to `<br>` in a single pass.
122
+ * `|` → `\|`, `\` → `\\`, `\r\n`/`\r`/`\n` → `<br>`
123
+ */
124
+ function escapeCell(value) {
125
+ return value.replace(constants_1.ESCAPE_AND_NEWLINE, ch => (ch === "|" || ch === "\\" ? "\\" + ch : "<br>"));
126
+ }
127
+ /**
128
+ * Convert literal newlines to `<br>` without escaping pipes/backslashes.
129
+ */
130
+ function convertNewlines(value) {
131
+ if (value.indexOf("\n") !== -1 || value.indexOf("\r") !== -1) {
132
+ return value.replace(constants_1.NEWLINE_IN_CELL, "<br>");
133
+ }
134
+ return value;
135
+ }
136
+ /**
137
+ * Build the separator cell for a column based on alignment and width.
138
+ *
139
+ * Examples (width=5):
140
+ * - none: `-----`
141
+ * - left: `:----`
142
+ * - right: `----:`
143
+ * - center: `:---:`
144
+ */
145
+ function buildSeparator(alignment, width) {
146
+ switch (alignment) {
147
+ case "left":
148
+ return ":" + "-".repeat(width - 1);
149
+ case "right":
150
+ return "-".repeat(width - 1) + ":";
151
+ case "center":
152
+ return ":" + "-".repeat(Math.max(width - 2, 1)) + ":";
153
+ default: // "none"
154
+ return "-".repeat(width);
155
+ }
156
+ }
157
+ /**
158
+ * Pad a cell value to the target display width with alignment.
159
+ * Uses displayWidth() for proper CJK/emoji handling.
160
+ */
161
+ function padCell(value, targetWidth, alignment) {
162
+ const len = displayWidth(value);
163
+ if (len >= targetWidth) {
164
+ return value;
165
+ }
166
+ const diff = targetWidth - len;
167
+ switch (alignment) {
168
+ case "right":
169
+ return " ".repeat(diff) + value;
170
+ case "center": {
171
+ const left = Math.floor(diff / 2);
172
+ const right = diff - left;
173
+ return " ".repeat(left) + value + " ".repeat(right);
174
+ }
175
+ default: // "left" or "none"
176
+ return value + " ".repeat(diff);
177
+ }
178
+ }
179
+ /**
180
+ * Resolve column configuration from options.
181
+ */
182
+ function resolveColumns(headers, options) {
183
+ const columnCount = headers.length;
184
+ const defaultAlignment = options.alignment ?? "left";
185
+ const displayHeaders = new Array(columnCount);
186
+ const alignments = new Array(columnCount);
187
+ const minWidths = new Array(columnCount);
188
+ if (options.columns && options.columns.length > 0) {
189
+ for (let i = 0; i < columnCount; i++) {
190
+ const col = i < options.columns.length ? options.columns[i] : undefined;
191
+ if (typeof col === "string") {
192
+ displayHeaders[i] = col;
193
+ alignments[i] = defaultAlignment;
194
+ minWidths[i] = 3;
195
+ }
196
+ else if (col) {
197
+ displayHeaders[i] = col.header;
198
+ alignments[i] = col.alignment ?? defaultAlignment;
199
+ minWidths[i] = col.minWidth ?? 3;
200
+ }
201
+ else {
202
+ displayHeaders[i] = headers[i] ?? "";
203
+ alignments[i] = defaultAlignment;
204
+ minWidths[i] = 3;
205
+ }
206
+ }
207
+ }
208
+ else {
209
+ for (let i = 0; i < columnCount; i++) {
210
+ displayHeaders[i] = headers[i] ?? "";
211
+ alignments[i] = defaultAlignment;
212
+ minWidths[i] = 3;
213
+ }
214
+ }
215
+ return { displayHeaders, alignments, minWidths };
216
+ }
217
+ // =============================================================================
218
+ // Main Formatter
219
+ // =============================================================================
220
+ /**
221
+ * Format data as a Markdown table string.
222
+ *
223
+ * @param headers - Column header strings
224
+ * @param rows - Data rows (each row is an array of cell values)
225
+ * @param options - Formatting options
226
+ * @returns Formatted Markdown table string
227
+ *
228
+ * @example
229
+ * ```ts
230
+ * formatMarkdown(
231
+ * ["Name", "Age", "City"],
232
+ * [
233
+ * ["Alice", 30, "New York"],
234
+ * ["Bob", 25, "London"]
235
+ * ]
236
+ * );
237
+ * ```
238
+ */
239
+ function formatMarkdown(headers, rows, options = {}) {
240
+ const { padding = true, trailingNewline = true, escapeContent = true, stringify = defaultStringify } = options;
241
+ const columnCount = headers.length;
242
+ if (columnCount === 0) {
243
+ return "";
244
+ }
245
+ // Resolve column configs
246
+ const { displayHeaders, alignments, minWidths } = resolveColumns(headers, options);
247
+ // Single-pass: convert all cell values to strings, apply escaping,
248
+ // and compute column widths simultaneously.
249
+ const headerStrings = new Array(columnCount);
250
+ const widths = new Array(columnCount);
251
+ // Initialize widths from headers
252
+ for (let i = 0; i < columnCount; i++) {
253
+ const h = escapeContent ? escapeCell(displayHeaders[i]) : convertNewlines(displayHeaders[i]);
254
+ headerStrings[i] = h;
255
+ widths[i] = padding ? Math.max(displayWidth(h), minWidths[i]) : Math.max(minWidths[i], 3);
256
+ }
257
+ // Convert rows and update widths in a single pass
258
+ const rowStrings = new Array(rows.length);
259
+ for (let r = 0; r < rows.length; r++) {
260
+ const row = rows[r];
261
+ const cells = new Array(columnCount);
262
+ for (let c = 0; c < columnCount; c++) {
263
+ const raw = c < row.length ? stringify(row[c]) : "";
264
+ const cell = escapeContent ? escapeCell(raw) : convertNewlines(raw);
265
+ cells[c] = cell;
266
+ if (padding) {
267
+ const cellWidth = displayWidth(cell);
268
+ if (cellWidth > widths[c]) {
269
+ widths[c] = cellWidth;
270
+ }
271
+ }
272
+ }
273
+ rowStrings[r] = cells;
274
+ }
275
+ // Build the complete table output
276
+ // Pre-calculate total line count: header + separator + data rows
277
+ const totalLines = 2 + rowStrings.length;
278
+ const lines = new Array(totalLines);
279
+ // Header row
280
+ const headerParts = new Array(columnCount);
281
+ for (let c = 0; c < columnCount; c++) {
282
+ headerParts[c] = padding
283
+ ? " " + padCell(headerStrings[c], widths[c], alignments[c]) + " "
284
+ : " " + headerStrings[c] + " ";
285
+ }
286
+ lines[0] = "|" + headerParts.join("|") + "|";
287
+ // Separator row
288
+ // In both modes, cell content has 1 space padding on each side (" value "),
289
+ // so separator width must be widths[c] + 2 to match.
290
+ const sepParts = new Array(columnCount);
291
+ for (let c = 0; c < columnCount; c++) {
292
+ sepParts[c] = buildSeparator(alignments[c], widths[c] + 2);
293
+ }
294
+ lines[1] = "|" + sepParts.join("|") + "|";
295
+ // Data rows
296
+ for (let r = 0; r < rowStrings.length; r++) {
297
+ const rowParts = new Array(columnCount);
298
+ for (let c = 0; c < columnCount; c++) {
299
+ rowParts[c] = padding
300
+ ? " " + padCell(rowStrings[r][c], widths[c], alignments[c]) + " "
301
+ : " " + rowStrings[r][c] + " ";
302
+ }
303
+ lines[r + 2] = "|" + rowParts.join("|") + "|";
304
+ }
305
+ let result = lines.join("\n");
306
+ if (trailingNewline) {
307
+ result += "\n";
308
+ }
309
+ return result;
310
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ /**
3
+ * Markdown Module - Public API
4
+ *
5
+ * Pure Markdown table parsing/formatting functionality with no Excel dependencies.
6
+ * For Markdown-Worksheet integration, use Workbook.readMarkdown/writeMarkdown methods instead.
7
+ *
8
+ * Design principles:
9
+ * - Only export types and functions that are part of the PUBLIC API
10
+ * - Internal utilities are used internally but not exported
11
+ * - This reduces bundle size and simplifies the public interface
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.MarkdownParseError = exports.MarkdownError = exports.formatMarkdown = exports.parseMarkdownAll = exports.parseMarkdown = void 0;
15
+ // =============================================================================
16
+ // Core Functions
17
+ // =============================================================================
18
+ // Parser
19
+ var index_1 = require("./parse/index");
20
+ Object.defineProperty(exports, "parseMarkdown", { enumerable: true, get: function () { return index_1.parseMarkdown; } });
21
+ Object.defineProperty(exports, "parseMarkdownAll", { enumerable: true, get: function () { return index_1.parseMarkdownAll; } });
22
+ // Formatter
23
+ var index_2 = require("./format/index");
24
+ Object.defineProperty(exports, "formatMarkdown", { enumerable: true, get: function () { return index_2.formatMarkdown; } });
25
+ // =============================================================================
26
+ // Errors
27
+ // =============================================================================
28
+ var errors_1 = require("./errors");
29
+ Object.defineProperty(exports, "MarkdownError", { enumerable: true, get: function () { return errors_1.MarkdownError; } });
30
+ Object.defineProperty(exports, "MarkdownParseError", { enumerable: true, get: function () { return errors_1.MarkdownParseError; } });