javascript-excel 2.0.0-alpha.1 → 2.0.0-alpha.2

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.
@@ -0,0 +1,57 @@
1
+ // src/internal/iso-date.ts
2
+ /** ISO 8601 date text and parsing, shared by the CSV writer, `cellText`, `fromTable` and `fromDataset`. */
3
+ /** YYYY-MM-DD, or YYYY-MM-DDTHH:mm:ss(.sss) when there is a time of day, in `zone`. */
4
+ function isoDate(d, zone) {
5
+ const utc = zone === "utc";
6
+ const y = utc ? d.getUTCFullYear() : d.getFullYear();
7
+ const mo = (utc ? d.getUTCMonth() : d.getMonth()) + 1;
8
+ const day = utc ? d.getUTCDate() : d.getDate();
9
+ const h = utc ? d.getUTCHours() : d.getHours();
10
+ const mi = utc ? d.getUTCMinutes() : d.getMinutes();
11
+ const s = utc ? d.getUTCSeconds() : d.getSeconds();
12
+ const ms = utc ? d.getUTCMilliseconds() : d.getMilliseconds();
13
+ const date = `${y >= 0 && y <= 9999 ? pad(y, 4) : (y < 0 ? "-" : "+") + pad(Math.abs(y), 6)}-${pad(mo, 2)}-${pad(day, 2)}`;
14
+ if (h === 0 && mi === 0 && s === 0 && ms === 0) return date;
15
+ const time = `${pad(h, 2)}:${pad(mi, 2)}:${pad(s, 2)}`;
16
+ return ms === 0 ? `${date}T${time}` : `${date}T${time}.${pad(ms, 3)}`;
17
+ }
18
+ function pad(n, w) {
19
+ return String(n).padStart(w, "0");
20
+ }
21
+ const ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3})\d*)?)?(Z|[+-]\d{2}:?\d{2})?)?$/;
22
+ /** ISO 8601 date or date-time. Without a zone, the local wall-clock time (dateZone 'local'). */
23
+ function parseIsoDate(s) {
24
+ const m = ISO_DATE.exec(s.trim());
25
+ if (!m) return void 0;
26
+ const [y, mo, d, h = "0", mi = "0", sec = "0", ms = "0", zone] = m.slice(1);
27
+ const parts = [
28
+ Number(y),
29
+ Number(mo) - 1,
30
+ Number(d),
31
+ Number(h),
32
+ Number(mi),
33
+ Number(sec),
34
+ Number(ms.padEnd(3, "0"))
35
+ ];
36
+ let date;
37
+ if (zone) {
38
+ const off = zone === "Z" ? 0 : (zone[0] === "-" ? -1 : 1) * (Number(zone.slice(1, 3)) * 60 + Number(zone.slice(-2)));
39
+ date = /* @__PURE__ */ new Date(Date.UTC(...parts) - off * 6e4);
40
+ } else date = new Date(...parts);
41
+ const wall = zone ? new Date(Date.UTC(...parts)) : date;
42
+ const [month, day] = zone ? [wall.getUTCMonth(), wall.getUTCDate()] : [wall.getMonth(), wall.getDate()];
43
+ if (month !== parts[1] || day !== parts[2]) return void 0;
44
+ return Number.isFinite(date.getTime()) ? date : void 0;
45
+ }
46
+ Object.defineProperty(exports, "isoDate", {
47
+ enumerable: true,
48
+ get: function() {
49
+ return isoDate;
50
+ }
51
+ });
52
+ Object.defineProperty(exports, "parseIsoDate", {
53
+ enumerable: true,
54
+ get: function() {
55
+ return parseIsoDate;
56
+ }
57
+ });
@@ -0,0 +1,46 @@
1
+ // src/internal/iso-date.ts
2
+ /** ISO 8601 date text and parsing, shared by the CSV writer, `cellText`, `fromTable` and `fromDataset`. */
3
+ /** YYYY-MM-DD, or YYYY-MM-DDTHH:mm:ss(.sss) when there is a time of day, in `zone`. */
4
+ function isoDate(d, zone) {
5
+ const utc = zone === "utc";
6
+ const y = utc ? d.getUTCFullYear() : d.getFullYear();
7
+ const mo = (utc ? d.getUTCMonth() : d.getMonth()) + 1;
8
+ const day = utc ? d.getUTCDate() : d.getDate();
9
+ const h = utc ? d.getUTCHours() : d.getHours();
10
+ const mi = utc ? d.getUTCMinutes() : d.getMinutes();
11
+ const s = utc ? d.getUTCSeconds() : d.getSeconds();
12
+ const ms = utc ? d.getUTCMilliseconds() : d.getMilliseconds();
13
+ const date = `${y >= 0 && y <= 9999 ? pad(y, 4) : (y < 0 ? "-" : "+") + pad(Math.abs(y), 6)}-${pad(mo, 2)}-${pad(day, 2)}`;
14
+ if (h === 0 && mi === 0 && s === 0 && ms === 0) return date;
15
+ const time = `${pad(h, 2)}:${pad(mi, 2)}:${pad(s, 2)}`;
16
+ return ms === 0 ? `${date}T${time}` : `${date}T${time}.${pad(ms, 3)}`;
17
+ }
18
+ function pad(n, w) {
19
+ return String(n).padStart(w, "0");
20
+ }
21
+ const ISO_DATE = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3})\d*)?)?(Z|[+-]\d{2}:?\d{2})?)?$/;
22
+ /** ISO 8601 date or date-time. Without a zone, the local wall-clock time (dateZone 'local'). */
23
+ function parseIsoDate(s) {
24
+ const m = ISO_DATE.exec(s.trim());
25
+ if (!m) return void 0;
26
+ const [y, mo, d, h = "0", mi = "0", sec = "0", ms = "0", zone] = m.slice(1);
27
+ const parts = [
28
+ Number(y),
29
+ Number(mo) - 1,
30
+ Number(d),
31
+ Number(h),
32
+ Number(mi),
33
+ Number(sec),
34
+ Number(ms.padEnd(3, "0"))
35
+ ];
36
+ let date;
37
+ if (zone) {
38
+ const off = zone === "Z" ? 0 : (zone[0] === "-" ? -1 : 1) * (Number(zone.slice(1, 3)) * 60 + Number(zone.slice(-2)));
39
+ date = /* @__PURE__ */ new Date(Date.UTC(...parts) - off * 6e4);
40
+ } else date = new Date(...parts);
41
+ const wall = zone ? new Date(Date.UTC(...parts)) : date;
42
+ const [month, day] = zone ? [wall.getUTCMonth(), wall.getUTCDate()] : [wall.getMonth(), wall.getDate()];
43
+ if (month !== parts[1] || day !== parts[2]) return void 0;
44
+ return Number.isFinite(date.getTime()) ? date : void 0;
45
+ }
46
+ export { parseIsoDate as n, isoDate as t };
@@ -38,6 +38,32 @@ var ByteWriter = class {
38
38
  this.pos += b.length;
39
39
  }
40
40
  };
41
+ const clock = (() => {
42
+ const perf = globalThis.performance;
43
+ return perf && typeof perf.now === "function" ? () => perf.now() : () => Date.now();
44
+ })();
45
+ /** Wraps a progress callback: monotonic ratio, throttled. Returns undefined when there is no callback. */
46
+ function throttleProgress(cb, intervalMs = 50) {
47
+ if (!cb) return void 0;
48
+ let last = -1e400;
49
+ let lastPhase;
50
+ let lastSheet;
51
+ let ratio = 0;
52
+ return (p) => {
53
+ ratio = Math.min(1, Math.max(ratio, p.ratio));
54
+ const now = clock();
55
+ if (!(p.phase !== lastPhase || p.sheet !== lastSheet) && ratio < 1 && now - last < intervalMs) return;
56
+ last = now;
57
+ lastPhase = p.phase;
58
+ lastSheet = p.sheet;
59
+ const out = {
60
+ phase: p.phase,
61
+ ratio
62
+ };
63
+ if (p.sheet !== void 0) out.sheet = p.sheet;
64
+ cb(out);
65
+ };
66
+ }
41
67
  // src/internal/platform.ts
42
68
  /**
43
69
  * Minimal structural types for the web platform APIs the engine uses.
@@ -168,12 +194,13 @@ const nativeCodec = {
168
194
  * CRC-32 as used by zip (ISO 3309 / ITU-T V.42): reflected polynomial
169
195
  * 0xEDB88320, initial value and final XOR 0xFFFFFFFF.
170
196
  *
171
- * Slicing-by-4 tables: 4 KiB, built lazily on first use so importing the
172
- * module costs nothing.
197
+ * Slicing-by-8 tables: 8 KiB, built lazily on first use so importing the
198
+ * module costs nothing. Slicing-by-8 measured 1,150–1,270 MB/s against
199
+ * 850–940 MB/s for slicing-by-4 on Node 24 (JSE-8).
173
200
  */
174
201
  let tables;
175
202
  function buildTables() {
176
- const t = /* @__PURE__ */ new Uint32Array(1024);
203
+ const t = /* @__PURE__ */ new Uint32Array(2048);
177
204
  for (let n = 0; n < 256; n++) {
178
205
  let c = n;
179
206
  for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
@@ -181,7 +208,7 @@ function buildTables() {
181
208
  }
182
209
  for (let n = 0; n < 256; n++) {
183
210
  let c = t[n];
184
- for (let k = 1; k < 4; k++) {
211
+ for (let k = 1; k < 8; k++) {
185
212
  c = t[c & 255] ^ c >>> 8;
186
213
  t[k * 256 + n] = c >>> 0;
187
214
  }
@@ -197,9 +224,10 @@ function crc32(data, crc = 0) {
197
224
  let c = ~crc >>> 0;
198
225
  const n = data.length;
199
226
  let i = 0;
200
- for (const end = n - n % 4; i < end; i += 4) {
227
+ for (const end = n - n % 8; i < end; i += 8) {
201
228
  c ^= data[i] | data[i + 1] << 8 | data[i + 2] << 16 | data[i + 3] << 24;
202
- c = t[768 + (c & 255)] ^ t[512 + (c >>> 8 & 255)] ^ t[256 + (c >>> 16 & 255)] ^ t[c >>> 24];
229
+ const hi = data[i + 4] | data[i + 5] << 8 | data[i + 6] << 16 | data[i + 7] << 24;
230
+ c = t[1792 + (c & 255)] ^ t[1536 + (c >>> 8 & 255)] ^ t[1280 + (c >>> 16 & 255)] ^ t[1024 + (c >>> 24)] ^ t[768 + (hi & 255)] ^ t[512 + (hi >>> 8 & 255)] ^ t[256 + (hi >>> 16 & 255)] ^ t[hi >>> 24];
203
231
  }
204
232
  for (; i < n; i++) c = t[(c ^ data[i]) & 255] ^ c >>> 8;
205
233
  return ~c >>> 0;
@@ -241,7 +269,8 @@ const EPOCH_1900 = Date.UTC(1899, 11, 30);
241
269
  const EPOCH_1900_EARLY = Date.UTC(1899, 11, 31);
242
270
  const LEAP_BUG_START = Date.UTC(1900, 2, 1);
243
271
  const EPOCH_1904 = Date.UTC(1904, 0, 1);
244
- const MIN_1900 = Date.UTC(1900, 0, 1);
272
+ /** Excel's day 0 ("1900-01-00", 1899-12-31): time-only values live there, serials 0 to <1. */
273
+ const MIN_1900 = Date.UTC(1899, 11, 31);
245
274
  const MAX = Date.UTC(9999, 11, 31, 23, 59, 59, 999);
246
275
  /** Wall-clock time of `d` in the chosen zone, as UTC milliseconds. */
247
276
  function wallClock(d, zone) {
@@ -253,7 +282,8 @@ function wallClock(d, zone) {
253
282
  /**
254
283
  * Converts a Date to an Excel serial number.
255
284
  * Throws `INVALID_DATE` for an invalid Date or one outside the date system's
256
- * range (1900-01-01 or 1904-01-01 to 9999-12-31).
285
+ * range: 1899-12-31 (Excel's day 0, where time-only values live, as serials
286
+ * 0 to <1; serialToDate returns those) or 1904-01-01, to 9999-12-31.
257
287
  */
258
288
  function dateToSerial(d, options) {
259
289
  const ms = Number.isFinite(d.getTime()) ? wallClock(d, options?.dateZone ?? "local") : NaN;
@@ -396,4 +426,4 @@ function detect(code) {
396
426
  s = s.replace(/general/gi, "").replace(/e[+-]/gi, "");
397
427
  return /[ymdhs]|am\/pm|a\/p|[egb]/i.test(s);
398
428
  }
399
- export { fromDosDateTime as a, nativeCodec as c, ByteWriter as d, concatBytes as f, serialToDate as i, decodeUtf8Strict as l, isDateFormat as n, toDosDateTime as o, throwIfAborted as p, dateToSerial as r, crc32 as s, BUILTIN_NUM_FMTS as t, encodeUtf8 as u };
429
+ export { fromDosDateTime as a, nativeCodec as c, throttleProgress as d, ByteWriter as f, serialToDate as i, decodeUtf8Strict as l, throwIfAborted as m, isDateFormat as n, toDosDateTime as o, concatBytes as p, dateToSerial as r, crc32 as s, BUILTIN_NUM_FMTS as t, encodeUtf8 as u };
@@ -38,6 +38,32 @@ var ByteWriter = class {
38
38
  this.pos += b.length;
39
39
  }
40
40
  };
41
+ const clock = (() => {
42
+ const perf = globalThis.performance;
43
+ return perf && typeof perf.now === "function" ? () => perf.now() : () => Date.now();
44
+ })();
45
+ /** Wraps a progress callback: monotonic ratio, throttled. Returns undefined when there is no callback. */
46
+ function throttleProgress(cb, intervalMs = 50) {
47
+ if (!cb) return void 0;
48
+ let last = -1e400;
49
+ let lastPhase;
50
+ let lastSheet;
51
+ let ratio = 0;
52
+ return (p) => {
53
+ ratio = Math.min(1, Math.max(ratio, p.ratio));
54
+ const now = clock();
55
+ if (!(p.phase !== lastPhase || p.sheet !== lastSheet) && ratio < 1 && now - last < intervalMs) return;
56
+ last = now;
57
+ lastPhase = p.phase;
58
+ lastSheet = p.sheet;
59
+ const out = {
60
+ phase: p.phase,
61
+ ratio
62
+ };
63
+ if (p.sheet !== void 0) out.sheet = p.sheet;
64
+ cb(out);
65
+ };
66
+ }
41
67
  // src/internal/platform.ts
42
68
  /**
43
69
  * Minimal structural types for the web platform APIs the engine uses.
@@ -168,12 +194,13 @@ const nativeCodec = {
168
194
  * CRC-32 as used by zip (ISO 3309 / ITU-T V.42): reflected polynomial
169
195
  * 0xEDB88320, initial value and final XOR 0xFFFFFFFF.
170
196
  *
171
- * Slicing-by-4 tables: 4 KiB, built lazily on first use so importing the
172
- * module costs nothing.
197
+ * Slicing-by-8 tables: 8 KiB, built lazily on first use so importing the
198
+ * module costs nothing. Slicing-by-8 measured 1,150–1,270 MB/s against
199
+ * 850–940 MB/s for slicing-by-4 on Node 24 (JSE-8).
173
200
  */
174
201
  let tables;
175
202
  function buildTables() {
176
- const t = /* @__PURE__ */ new Uint32Array(1024);
203
+ const t = /* @__PURE__ */ new Uint32Array(2048);
177
204
  for (let n = 0; n < 256; n++) {
178
205
  let c = n;
179
206
  for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
@@ -181,7 +208,7 @@ function buildTables() {
181
208
  }
182
209
  for (let n = 0; n < 256; n++) {
183
210
  let c = t[n];
184
- for (let k = 1; k < 4; k++) {
211
+ for (let k = 1; k < 8; k++) {
185
212
  c = t[c & 255] ^ c >>> 8;
186
213
  t[k * 256 + n] = c >>> 0;
187
214
  }
@@ -197,9 +224,10 @@ function crc32(data, crc = 0) {
197
224
  let c = ~crc >>> 0;
198
225
  const n = data.length;
199
226
  let i = 0;
200
- for (const end = n - n % 4; i < end; i += 4) {
227
+ for (const end = n - n % 8; i < end; i += 8) {
201
228
  c ^= data[i] | data[i + 1] << 8 | data[i + 2] << 16 | data[i + 3] << 24;
202
- c = t[768 + (c & 255)] ^ t[512 + (c >>> 8 & 255)] ^ t[256 + (c >>> 16 & 255)] ^ t[c >>> 24];
229
+ const hi = data[i + 4] | data[i + 5] << 8 | data[i + 6] << 16 | data[i + 7] << 24;
230
+ c = t[1792 + (c & 255)] ^ t[1536 + (c >>> 8 & 255)] ^ t[1280 + (c >>> 16 & 255)] ^ t[1024 + (c >>> 24)] ^ t[768 + (hi & 255)] ^ t[512 + (hi >>> 8 & 255)] ^ t[256 + (hi >>> 16 & 255)] ^ t[hi >>> 24];
203
231
  }
204
232
  for (; i < n; i++) c = t[(c ^ data[i]) & 255] ^ c >>> 8;
205
233
  return ~c >>> 0;
@@ -241,7 +269,8 @@ const EPOCH_1900 = Date.UTC(1899, 11, 30);
241
269
  const EPOCH_1900_EARLY = Date.UTC(1899, 11, 31);
242
270
  const LEAP_BUG_START = Date.UTC(1900, 2, 1);
243
271
  const EPOCH_1904 = Date.UTC(1904, 0, 1);
244
- const MIN_1900 = Date.UTC(1900, 0, 1);
272
+ /** Excel's day 0 ("1900-01-00", 1899-12-31): time-only values live there, serials 0 to <1. */
273
+ const MIN_1900 = Date.UTC(1899, 11, 31);
245
274
  const MAX = Date.UTC(9999, 11, 31, 23, 59, 59, 999);
246
275
  /** Wall-clock time of `d` in the chosen zone, as UTC milliseconds. */
247
276
  function wallClock(d, zone) {
@@ -253,7 +282,8 @@ function wallClock(d, zone) {
253
282
  /**
254
283
  * Converts a Date to an Excel serial number.
255
284
  * Throws `INVALID_DATE` for an invalid Date or one outside the date system's
256
- * range (1900-01-01 or 1904-01-01 to 9999-12-31).
285
+ * range: 1899-12-31 (Excel's day 0, where time-only values live, as serials
286
+ * 0 to <1; serialToDate returns those) or 1904-01-01, to 9999-12-31.
257
287
  */
258
288
  function dateToSerial(d, options) {
259
289
  const ms = Number.isFinite(d.getTime()) ? wallClock(d, options?.dateZone ?? "local") : NaN;
@@ -462,6 +492,12 @@ Object.defineProperty(exports, "serialToDate", {
462
492
  return serialToDate;
463
493
  }
464
494
  });
495
+ Object.defineProperty(exports, "throttleProgress", {
496
+ enumerable: true,
497
+ get: function() {
498
+ return throttleProgress;
499
+ }
500
+ });
465
501
  Object.defineProperty(exports, "throwIfAborted", {
466
502
  enumerable: true,
467
503
  get: function() {
@@ -0,0 +1,78 @@
1
+ import { I as Workbook, r as BinaryInput, t as ExcelWarning, u as Cell } from "./warnings-C_12PT4t.cjs";
2
+ import { t as AbortSignalLike } from "./signal-Dz0ZYe6d.cjs";
3
+ import { t as Progress } from "./write-C5JgeIar.cjs";
4
+ /**
5
+ * Format detection tables for the reader (docs/api.md §5.2). Internal.
6
+ *
7
+ * Detection is data: container signatures first, then, for OPC packages, the
8
+ * workbook part's content type. Supporting another format later (.xlsb, .xls)
9
+ * is one more row plus a decoder; the reader dispatches on `decoder`.
10
+ */
11
+ /** Every format `detectFormat` can name. */
12
+ type FormatName = "xlsx" | "xlsm" | "xltx" | "xltm" | "xlsb" | "ods" | "cfb" | "zip" | "unknown";
13
+ /** Result of `read`. */
14
+ interface ReadResult {
15
+ workbook: Workbook<Cell>;
16
+ warnings: ExcelWarning[];
17
+ }
18
+ /** Options for `read`. */
19
+ interface ReadOptions {
20
+ /** Only parse these sheets (by name or index); the others are listed with empty rows. Default: all. */
21
+ sheets?: Array<string | number> | undefined;
22
+ /** Parse styles (default true). false is faster: no `cell.style`, but date detection still uses the number format. */
23
+ styles?: boolean | undefined;
24
+ /** Return formula text (default true). false: cached values only. */
25
+ formulas?: boolean | undefined;
26
+ /** `'date'` (default): date-formatted numbers become `Date`. `'serial'`: leave them as numbers. */
27
+ dates?: "date" | "serial" | undefined;
28
+ /** Default `'local'`. */
29
+ dateZone?: "local" | "utc" | undefined;
30
+ /** Rich text as runs (default) or flattened to a plain string. */
31
+ richText?: "runs" | "plain" | undefined;
32
+ limits?: Partial<ReadLimits> | undefined;
33
+ /** Rejects with `signal.reason` when aborted. */
34
+ signal?: AbortSignalLike | undefined;
35
+ onProgress?: ((p: Progress) => void) | undefined;
36
+ /** Called as warnings occur, in addition to `ReadResult.warnings`. */
37
+ onWarning?: ((w: ExcelWarning) => void) | undefined;
38
+ }
39
+ /** Limits for untrusted input. Exceeding one throws `LimitError` (`LIMIT_EXCEEDED`, `details.limit` names it). */
40
+ interface ReadLimits {
41
+ maxInputBytes: number;
42
+ maxEntries: number;
43
+ /** Decompressed bytes of one part. */
44
+ maxEntryBytes: number;
45
+ /** Decompressed bytes of all parts read. */
46
+ maxTotalBytes: number;
47
+ /** Decompressed / compressed, per entry, checked once an entry has produced more than 1 MiB. */
48
+ maxRatio: number;
49
+ maxXmlDepth: number;
50
+ /** All sheets together. */
51
+ maxCells: number;
52
+ maxSharedStrings: number;
53
+ /** Then a single `WARNINGS_TRUNCATED`. */
54
+ maxWarnings: number;
55
+ }
56
+ /** Default limits (docs/api.md §5.2; the measurements behind each value are listed there). */
57
+ declare const DEFAULT_READ_LIMITS: Readonly<ReadLimits>;
58
+ /** A sheet as listed by `readSheetNames`. */
59
+ interface SheetInfo {
60
+ name: string;
61
+ index: number;
62
+ state: "visible" | "hidden" | "veryHidden";
63
+ }
64
+ /**
65
+ * What `detectFormat` recognises. `'cfb'` is a legacy `.xls` or an encrypted OOXML file.
66
+ * `read` reads xlsx, xlsm, xltx and xltm; the others are rejected with UNSUPPORTED_FORMAT.
67
+ */
68
+ type DetectedFormat = FormatName;
69
+ /** Detects the file format from the bytes. Never throws for unreadable input; returns `'unknown'` or `'zip'` instead. */
70
+ declare function detectFormat(input: BinaryInput): Promise<DetectedFormat>;
71
+ /** Lists the worksheets (name, index, visibility) without parsing any sheet. */
72
+ declare function readSheetNames(input: BinaryInput, options?: {
73
+ limits?: Partial<ReadLimits> | undefined;
74
+ signal?: AbortSignalLike | undefined;
75
+ }): Promise<SheetInfo[]>;
76
+ /** Reads an .xlsx, .xlsm, .xltx or .xltm file into the model. */
77
+ declare function read(input: BinaryInput, options?: ReadOptions): Promise<ReadResult>;
78
+ export { ReadResult as a, read as c, ReadOptions as i, readSheetNames as l, DetectedFormat as n, SheetInfo as o, ReadLimits as r, detectFormat as s, DEFAULT_READ_LIMITS as t };