javascript-excel 1.0.3 → 2.0.0-alpha.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 (50) hide show
  1. package/README.md +50 -2
  2. package/dist/auto-width-2oBzjeNG.d.cts +11 -0
  3. package/dist/auto-width-CT-BihTW.d.ts +10 -0
  4. package/dist/csv.cjs +623 -0
  5. package/dist/csv.d.cts +89 -0
  6. package/dist/csv.d.ts +89 -0
  7. package/dist/csv.js +618 -0
  8. package/dist/dom.cjs +464 -0
  9. package/dist/dom.d.cts +78 -0
  10. package/dist/dom.d.ts +78 -0
  11. package/dist/dom.js +461 -0
  12. package/dist/errors-CvGlfw0E.js +28 -0
  13. package/dist/errors-DOPrrGS4.cjs +69 -0
  14. package/dist/index.cjs +1424 -0
  15. package/dist/index.d.cts +140 -0
  16. package/dist/index.d.ts +140 -0
  17. package/dist/index.js +1397 -0
  18. package/dist/infer-BR-J9BZa.js +88 -0
  19. package/dist/infer-D9EC9vJ-.cjs +105 -0
  20. package/dist/iso-date-B22IvwL1.js +21 -0
  21. package/dist/iso-date-BSUsPSWO.cjs +26 -0
  22. package/dist/numfmt-B-TlCgr7.cjs +476 -0
  23. package/dist/numfmt-DEm0sYBJ.js +399 -0
  24. package/dist/read.cjs +2899 -0
  25. package/dist/read.d.cts +88 -0
  26. package/dist/read.d.ts +88 -0
  27. package/dist/read.js +2895 -0
  28. package/dist/sheet-names-CDsVK15c.js +831 -0
  29. package/dist/sheet-names-Cm-IhbBk.cjs +1004 -0
  30. package/dist/signal-Dz0ZYe6d.d.cts +13 -0
  31. package/dist/signal-Dz0ZYe6d.d.ts +13 -0
  32. package/dist/warnings-C_12PT4t.d.cts +410 -0
  33. package/dist/warnings-DXJKe1lx.d.ts +410 -0
  34. package/dist/worker.cjs +11 -0
  35. package/dist/worker.d.cts +8 -0
  36. package/dist/worker.d.ts +8 -0
  37. package/dist/worker.js +10 -0
  38. package/dist/write-C5JgeIar.d.cts +29 -0
  39. package/dist/write-CYpLGuzl.d.ts +29 -0
  40. package/package.json +67 -51
  41. package/.babelrc +0 -10
  42. package/.circleci/config.yml +0 -39
  43. package/lib/index.d.ts +0 -1
  44. package/lib/index.js +0 -22
  45. package/lib/javascript-excel.js +0 -17703
  46. package/src/index.tsx +0 -11
  47. package/test/unit/sample.spec.js +0 -5
  48. package/tsconfig.json +0 -54
  49. package/webpack.config.js +0 -19
  50. /package/{LICENSE.md → LICENSE} +0 -0
@@ -0,0 +1,399 @@
1
+ import { a as LimitError, i as FormatError, n as EnvironmentError, o as ValidationError } from "./errors-CvGlfw0E.js";
2
+ // src/internal/zip/bytes.ts
3
+ /** Rejects with `signal.reason`, the same as fetch. */
4
+ function throwIfAborted(signal) {
5
+ if (signal?.aborted) throw signal.reason ?? /* @__PURE__ */ new Error("The operation was aborted.");
6
+ }
7
+ function concatBytes(parts) {
8
+ if (parts.length === 1) return parts[0];
9
+ let total = 0;
10
+ for (const p of parts) total += p.length;
11
+ const out = new Uint8Array(total);
12
+ let offset = 0;
13
+ for (const p of parts) {
14
+ out.set(p, offset);
15
+ offset += p.length;
16
+ }
17
+ return out;
18
+ }
19
+ /** Little-endian writer into a fixed-size buffer. */
20
+ var ByteWriter = class {
21
+ bytes;
22
+ view;
23
+ pos = 0;
24
+ constructor(size) {
25
+ this.bytes = new Uint8Array(size);
26
+ this.view = new DataView(this.bytes.buffer);
27
+ }
28
+ u16(v) {
29
+ this.view.setUint16(this.pos, v, true);
30
+ this.pos += 2;
31
+ }
32
+ u32(v) {
33
+ this.view.setUint32(this.pos, v >>> 0, true);
34
+ this.pos += 4;
35
+ }
36
+ raw(b) {
37
+ this.bytes.set(b, this.pos);
38
+ this.pos += b.length;
39
+ }
40
+ };
41
+ // src/internal/platform.ts
42
+ /**
43
+ * Minimal structural types for the web platform APIs the engine uses.
44
+ *
45
+ * The package compiles against `lib: ES2022` with no DOM or Node types, so the
46
+ * published declarations never depend on a consumer's `lib` setting. These
47
+ * interfaces describe only the members the engine calls. They are internal and
48
+ * never appear in a public signature.
49
+ */
50
+ const g = globalThis;
51
+ function missing(api) {
52
+ return new EnvironmentError("ENVIRONMENT_UNSUPPORTED", `${api} is not available in this runtime. javascript-excel needs Node.js 22 or later, or a browser with CompressionStream('deflate-raw') support.`, { details: { api } });
53
+ }
54
+ /** Creates a raw-DEFLATE compression or decompression stream, or throws ENVIRONMENT_UNSUPPORTED. */
55
+ function createStream(kind) {
56
+ const name = kind === "compress" ? "CompressionStream" : "DecompressionStream";
57
+ const Ctor = kind === "compress" ? g.CompressionStream : g.DecompressionStream;
58
+ if (typeof Ctor !== "function") throw missing(name);
59
+ try {
60
+ return new Ctor("deflate-raw");
61
+ } catch (cause) {
62
+ const err = missing(`${name}('deflate-raw')`);
63
+ err.cause = cause;
64
+ throw err;
65
+ }
66
+ }
67
+ let encoder;
68
+ let strictDecoder;
69
+ function encodeUtf8(text) {
70
+ if (!encoder) {
71
+ if (typeof g.TextEncoder !== "function") throw missing("TextEncoder");
72
+ encoder = new g.TextEncoder();
73
+ }
74
+ return encoder.encode(text);
75
+ }
76
+ /** Decodes UTF-8 and throws a TypeError on invalid bytes. */
77
+ function decodeUtf8Strict(bytes) {
78
+ if (!strictDecoder) {
79
+ if (typeof g.TextDecoder !== "function") throw missing("TextDecoder");
80
+ strictDecoder = new g.TextDecoder("utf-8", { fatal: true });
81
+ }
82
+ return strictDecoder.decode(bytes);
83
+ }
84
+ const ignore = () => {};
85
+ async function deflateRaw(chunks, signal) {
86
+ throwIfAborted(signal);
87
+ const stream = createStream("compress");
88
+ const writer = stream.writable.getWriter();
89
+ const reader = stream.readable.getReader();
90
+ const out = [];
91
+ const reading = (async () => {
92
+ for (;;) {
93
+ const r = await reader.read();
94
+ if (r.done) return;
95
+ if (r.value) out.push(r.value);
96
+ }
97
+ })();
98
+ try {
99
+ for await (const chunk of chunks) {
100
+ throwIfAborted(signal);
101
+ if (chunk.length > 0) await writer.write(chunk);
102
+ }
103
+ throwIfAborted(signal);
104
+ await writer.close();
105
+ await reading;
106
+ } catch (err) {
107
+ writer.abort(err).catch(ignore);
108
+ reader.cancel(err).catch(ignore);
109
+ await reading.catch(ignore);
110
+ throw err;
111
+ }
112
+ return concatBytes(out);
113
+ }
114
+ async function* inflateRaw(data, limits, signal) {
115
+ throwIfAborted(signal);
116
+ const stream = createStream("decompress");
117
+ const writer = stream.writable.getWriter();
118
+ const reader = stream.readable.getReader();
119
+ const writing = writer.write(data).then(() => writer.close());
120
+ writing.catch(ignore);
121
+ let produced = 0;
122
+ let finished = false;
123
+ try {
124
+ for (;;) {
125
+ throwIfAborted(signal);
126
+ let r;
127
+ try {
128
+ r = await reader.read();
129
+ } catch (cause) {
130
+ throw new FormatError("ZIP_INVALID", "Invalid or truncated DEFLATE data.", { cause });
131
+ }
132
+ if (r.done) break;
133
+ const chunk = r.value;
134
+ if (!chunk || chunk.length === 0) continue;
135
+ produced += chunk.length;
136
+ if (produced > limits.maxOutputBytes) throw new LimitError("LIMIT_EXCEEDED", `Decompressed entry exceeds ${limits.maxOutputBytes} bytes.`, { details: {
137
+ limit: "maxEntryBytes",
138
+ value: produced,
139
+ max: limits.maxOutputBytes
140
+ } });
141
+ if (produced > 1048576 && produced / Math.max(1, data.length) > limits.maxRatio) throw new LimitError("LIMIT_EXCEEDED", `Compression ratio exceeds ${limits.maxRatio}.`, { details: {
142
+ limit: "maxRatio",
143
+ value: produced / Math.max(1, data.length),
144
+ max: limits.maxRatio
145
+ } });
146
+ yield chunk;
147
+ }
148
+ try {
149
+ await writing;
150
+ } catch (cause) {
151
+ throw new FormatError("ZIP_INVALID", "Invalid or truncated DEFLATE data.", { cause });
152
+ }
153
+ finished = true;
154
+ } finally {
155
+ if (!finished) {
156
+ reader.cancel().catch(ignore);
157
+ writer.abort().catch(ignore);
158
+ }
159
+ }
160
+ }
161
+ /** The default codec: native CompressionStream / DecompressionStream('deflate-raw'). */
162
+ const nativeCodec = {
163
+ deflateRaw,
164
+ inflateRaw
165
+ };
166
+ // src/internal/zip/crc32.ts
167
+ /**
168
+ * CRC-32 as used by zip (ISO 3309 / ITU-T V.42): reflected polynomial
169
+ * 0xEDB88320, initial value and final XOR 0xFFFFFFFF.
170
+ *
171
+ * Slicing-by-4 tables: 4 KiB, built lazily on first use so importing the
172
+ * module costs nothing.
173
+ */
174
+ let tables;
175
+ function buildTables() {
176
+ const t = /* @__PURE__ */ new Uint32Array(1024);
177
+ for (let n = 0; n < 256; n++) {
178
+ let c = n;
179
+ for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
180
+ t[n] = c >>> 0;
181
+ }
182
+ for (let n = 0; n < 256; n++) {
183
+ let c = t[n];
184
+ for (let k = 1; k < 4; k++) {
185
+ c = t[c & 255] ^ c >>> 8;
186
+ t[k * 256 + n] = c >>> 0;
187
+ }
188
+ }
189
+ return t;
190
+ }
191
+ /**
192
+ * Returns the CRC-32 of `data`. Pass the previous result as `crc` to continue
193
+ * over several chunks: `crc32(b, crc32(a))` equals `crc32(a + b)`.
194
+ */
195
+ function crc32(data, crc = 0) {
196
+ const t = tables ??= buildTables();
197
+ let c = ~crc >>> 0;
198
+ const n = data.length;
199
+ let i = 0;
200
+ for (const end = n - n % 4; i < end; i += 4) {
201
+ 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];
203
+ }
204
+ for (; i < n; i++) c = t[(c ^ data[i]) & 255] ^ c >>> 8;
205
+ return ~c >>> 0;
206
+ }
207
+ // src/internal/zip/dos-time.ts
208
+ const MIN_MS = Date.UTC(1980, 0, 1);
209
+ const MAX_MS = Date.UTC(2107, 11, 31, 23, 59, 58);
210
+ function toDosDateTime(d) {
211
+ let ms = d.getTime();
212
+ if (!Number.isFinite(ms) || ms < MIN_MS) ms = MIN_MS;
213
+ else if (ms > MAX_MS) ms = MAX_MS;
214
+ const u = new Date(ms);
215
+ return {
216
+ date: u.getUTCFullYear() - 1980 << 9 | u.getUTCMonth() + 1 << 5 | u.getUTCDate(),
217
+ time: u.getUTCHours() << 11 | u.getUTCMinutes() << 5 | u.getUTCSeconds() >> 1
218
+ };
219
+ }
220
+ function fromDosDateTime(date, time) {
221
+ const year = 1980 + (date >>> 9);
222
+ const month = Math.max(1, date >>> 5 & 15) - 1;
223
+ const day = Math.max(1, date & 31);
224
+ return new Date(Date.UTC(year, month, day, time >>> 11, time >>> 5 & 63, (time & 31) * 2));
225
+ }
226
+ // src/dates.ts
227
+ /**
228
+ * Date ↔ serial conversion (docs/api.md §3.4.1, §4.5).
229
+ *
230
+ * 1900 system: serial 1 = 1900-01-01. Excel treats 1900 as a leap year, so
231
+ * serial 60 is the non-existent 1900-02-29: dates before 1900-03-01 use the
232
+ * epoch 1899-12-31, later dates 1899-12-30.
233
+ * 1904 system: serial 0 = 1904-01-01.
234
+ *
235
+ * `dateZone: 'local'` (default) reads and builds dates from their local
236
+ * calendar fields, so `new Date(2024, 0, 15)` is serial 45306 in every zone.
237
+ * `'utc'` uses the UTC fields.
238
+ */
239
+ const DAY_MS = 864e5;
240
+ const EPOCH_1900 = Date.UTC(1899, 11, 30);
241
+ const EPOCH_1900_EARLY = Date.UTC(1899, 11, 31);
242
+ const LEAP_BUG_START = Date.UTC(1900, 2, 1);
243
+ const EPOCH_1904 = Date.UTC(1904, 0, 1);
244
+ const MIN_1900 = Date.UTC(1900, 0, 1);
245
+ const MAX = Date.UTC(9999, 11, 31, 23, 59, 59, 999);
246
+ /** Wall-clock time of `d` in the chosen zone, as UTC milliseconds. */
247
+ function wallClock(d, zone) {
248
+ if (zone === "utc") return d.getTime();
249
+ const year = d.getFullYear();
250
+ if (year < 1e3) return NaN;
251
+ return Date.UTC(year, d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
252
+ }
253
+ /**
254
+ * Converts a Date to an Excel serial number.
255
+ * 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).
257
+ */
258
+ function dateToSerial(d, options) {
259
+ const ms = Number.isFinite(d.getTime()) ? wallClock(d, options?.dateZone ?? "local") : NaN;
260
+ const min = options?.date1904 ? EPOCH_1904 : MIN_1900;
261
+ if (!Number.isFinite(ms) || ms < min || ms > MAX) throw new ValidationError("INVALID_DATE", `Date ${String(d)} is outside the range Excel can store.`, { details: { value: Number.isFinite(d.getTime()) ? d.toISOString() : "Invalid Date" } });
262
+ if (options?.date1904) return (ms - EPOCH_1904) / DAY_MS;
263
+ return (ms - (ms < LEAP_BUG_START ? EPOCH_1900_EARLY : EPOCH_1900)) / DAY_MS;
264
+ }
265
+ /**
266
+ * Converts an Excel serial number to a Date (inverse of `dateToSerial`).
267
+ * - 1900 system: serials 0 to <1 are Excel's day 0 ("1900-01-00", i.e.
268
+ * 1899-12-31), which is how time-only values such as 0.5 (12:00) are
269
+ * stored. Serial 60 (the fictitious 1900-02-29) has no real date and gives
270
+ * 1900-03-01; callers that must flag it (the reader) check for it themselves.
271
+ * - 1904 system: serial 0 = 1904-01-01.
272
+ * Throws `INVALID_DATE` for negative, non-finite or out-of-range serials.
273
+ */
274
+ function serialToDate(serial, options) {
275
+ const max = options?.date1904 ? (MAX - EPOCH_1904) / DAY_MS : (MAX - EPOCH_1900) / DAY_MS;
276
+ if (!Number.isFinite(serial) || serial < 0 || serial > max) throw new ValidationError("INVALID_DATE", `Serial ${serial} is not a valid date.`, { details: { value: serial } });
277
+ let ms;
278
+ if (options?.date1904) ms = EPOCH_1904 + Math.round(serial * DAY_MS);
279
+ else if (serial < 60) ms = EPOCH_1900_EARLY + Math.round(serial * DAY_MS);
280
+ else if (serial < 61) ms = EPOCH_1900 + Math.round((serial + 1) * DAY_MS);
281
+ else ms = EPOCH_1900 + Math.round(serial * DAY_MS);
282
+ if ((options?.dateZone ?? "local") === "utc") return new Date(ms);
283
+ const u = new Date(ms);
284
+ return new Date(u.getUTCFullYear(), u.getUTCMonth(), u.getUTCDate(), u.getUTCHours(), u.getUTCMinutes(), u.getUTCSeconds(), u.getUTCMilliseconds());
285
+ }
286
+ // src/numfmt.ts
287
+ /** The built-in number formats ECMA-376 §18.8.30 defines (§11.2). Ids 14–17 and 22 render in the user's locale in Excel. */
288
+ const BUILTIN_NUM_FMTS = Object.freeze({
289
+ 0: "General",
290
+ 1: "0",
291
+ 2: "0.00",
292
+ 3: "#,##0",
293
+ 4: "#,##0.00",
294
+ 9: "0%",
295
+ 10: "0.00%",
296
+ 11: "0.00E+00",
297
+ 12: "# ?/?",
298
+ 13: "# ??/??",
299
+ 14: "mm-dd-yy",
300
+ 15: "d-mmm-yy",
301
+ 16: "d-mmm",
302
+ 17: "mmm-yy",
303
+ 18: "h:mm AM/PM",
304
+ 19: "h:mm:ss AM/PM",
305
+ 20: "h:mm",
306
+ 21: "h:mm:ss",
307
+ 22: "m/d/yy h:mm",
308
+ 37: "#,##0 ;(#,##0)",
309
+ 38: "#,##0 ;[Red](#,##0)",
310
+ 39: "#,##0.00;(#,##0.00)",
311
+ 40: "#,##0.00;[Red](#,##0.00)",
312
+ 45: "mm:ss",
313
+ 46: "[h]:mm:ss",
314
+ 47: "mmss.0",
315
+ 48: "##0.0E+0",
316
+ 49: "@"
317
+ });
318
+ /** Built-in ids that are dates or times: 14–22, 45 and 47, and the East Asian locale ids 27–36 and 50–58. 46 is `[h]:mm:ss`, a duration. */
319
+ const DATE_IDS = /* @__PURE__ */ new Set([
320
+ 14,
321
+ 15,
322
+ 16,
323
+ 17,
324
+ 18,
325
+ 19,
326
+ 20,
327
+ 21,
328
+ 22,
329
+ 27,
330
+ 28,
331
+ 29,
332
+ 30,
333
+ 31,
334
+ 32,
335
+ 33,
336
+ 34,
337
+ 35,
338
+ 36,
339
+ 45,
340
+ 47,
341
+ 50,
342
+ 51,
343
+ 52,
344
+ 53,
345
+ 54,
346
+ 55,
347
+ 56,
348
+ 57,
349
+ 58
350
+ ]);
351
+ const cache = /* @__PURE__ */ new Map();
352
+ /**
353
+ * True when a number format shows a date or a time of day (§3.4.1): built-in
354
+ * ids 14–22, 45, 47, 27–36 and 50–58, or a custom code that contains a date or
355
+ * time token outside quoted text, escapes and brackets. Elapsed-time formats
356
+ * (`[h]:mm:ss`, built-in 46) are durations and return false.
357
+ */
358
+ function isDateFormat(fmt) {
359
+ if (fmt === void 0) return false;
360
+ if (typeof fmt === "number") return DATE_IDS.has(fmt);
361
+ let hit = cache.get(fmt);
362
+ if (hit === void 0) {
363
+ hit = detect(fmt);
364
+ if (cache.size < 1e3) cache.set(fmt, hit);
365
+ }
366
+ return hit;
367
+ }
368
+ function detect(code) {
369
+ let s = "";
370
+ let inQuote = false;
371
+ for (let i = 0; i < code.length; i++) {
372
+ const ch = code[i];
373
+ if (inQuote) {
374
+ if (ch === "\"") inQuote = false;
375
+ continue;
376
+ }
377
+ if (ch === "\"") {
378
+ inQuote = true;
379
+ continue;
380
+ }
381
+ if (ch === "\\" || ch === "_" || ch === "*") {
382
+ i++;
383
+ continue;
384
+ }
385
+ if (ch === ";") break;
386
+ if (ch === "[") {
387
+ const end = code.indexOf("]", i + 1);
388
+ if (end === -1) break;
389
+ const inner = code.slice(i + 1, end).toLowerCase();
390
+ if (/^(h+|m+|s+)$/.test(inner)) return false;
391
+ i = end;
392
+ continue;
393
+ }
394
+ s += ch;
395
+ }
396
+ s = s.replace(/general/gi, "").replace(/e[+-]/gi, "");
397
+ return /[ymdhs]|am\/pm|a\/p|[egb]/i.test(s);
398
+ }
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 };