javascript-excel 2.2.0 → 2.4.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.
@@ -1,7 +1,7 @@
1
- import { r as readCsv } from "./csv-CrWF7JS1.js";
2
- import { i as toDatasetDetailed } from "./dataset-BLo9CwB-.js";
1
+ import { r as readCsv } from "./csv-Dn4MIESx.js";
2
+ import { i as toDatasetDetailed } from "./dataset-CyEHVht4.js";
3
3
  import { a as LimitError, i as FormatError, o as ValidationError } from "./errors-CvGlfw0E.js";
4
- import { i as readSheetNames, n as detectFormat, r as read } from "./read-CFmZioBG.js";
4
+ import { a as readImpl, c as DEFAULT_LIMITS, i as readSheetNames, n as detectFormat, o as excerpt, s as quoted } from "./read-cvxu6O-H.js";
5
5
  import { a as toBytes, i as checkMaxWarnings, n as parseIsoDate } from "./infer-DcUyZ1T-.js";
6
6
  import { a as decodeRange } from "./address-DUn7-5OS.js";
7
7
  import { t as isoDate } from "./iso-date-CXnn-9mT.js";
@@ -99,11 +99,24 @@ function parseNumber(text, options = {}) {
99
99
  if (!fraction && !exponent && !Number.isSafeInteger(value)) return void 0;
100
100
  return value;
101
101
  }
102
- /** Splits "15/01/2024", "2024.01.15" or "1-2-2024 13:45:10" into numbers; one separator throughout. */
103
- function dateParts(text) {
102
+ /**
103
+ * Checks a two-digit-year pivot: undefined (two-digit years off) or an
104
+ * integer from 0 to 100. Throws TypeError otherwise.
105
+ */
106
+ function checkTwoDigitYearPivot(name, pivot) {
107
+ if (pivot === void 0) return void 0;
108
+ if (typeof pivot !== "number" || !Number.isInteger(pivot) || pivot < 0 || pivot > 100) throw new TypeError(`${name} must be an integer from 0 to 100, got ${typeof pivot === "number" ? String(pivot) : JSON.stringify(pivot)}`);
109
+ return pivot;
110
+ }
111
+ /**
112
+ * Splits "15/01/2024", "2024.01.15" or "1-2-2024 13:45:10" into numbers; one separator throughout.
113
+ * `short`: also texts of 6 or 7 characters whose last part has two digits ("1.3.26"),
114
+ * which only a two-digit year makes a date; not "10.2.1" or "1.2.333".
115
+ */
116
+ function dateParts(text, short = false) {
104
117
  const s = text.trim();
105
118
  const n = s.length;
106
- if (n < 8 || n > 24) return void 0;
119
+ if (n < (short ? 6 : 8) || n > 24) return void 0;
107
120
  const nums = [];
108
121
  const lens = [];
109
122
  let i = 0;
@@ -137,6 +150,7 @@ function dateParts(text) {
137
150
  if (h > 23 || mi > 59 || sec > 59) return void 0;
138
151
  hasTime = true;
139
152
  }
153
+ if (n < 8 && lens[2] !== 2) return void 0;
140
154
  return {
141
155
  a: nums[0],
142
156
  b: nums[1],
@@ -153,8 +167,12 @@ function daysInMonth(y, m) {
153
167
  if (m === 2) return y % 4 === 0 && y % 100 !== 0 || y % 400 === 0 ? 29 : 28;
154
168
  return m === 4 || m === 6 || m === 9 || m === 11 ? 30 : 31;
155
169
  }
156
- /** Builds the date for parts in `order`; undefined if the parts don't fit it. Years must have four digits. */
157
- function dateFromParts(p, order, zone) {
170
+ /**
171
+ * Builds the date for parts in `order`; undefined if the parts don't fit it.
172
+ * Years have four digits. With `pivot` (DMY and MDY only), a two-digit year
173
+ * yy is 20yy below the pivot and 19yy from it on.
174
+ */
175
+ function dateFromParts(p, order, zone, pivot) {
158
176
  let y, mo, d;
159
177
  if (order === "YMD") {
160
178
  if (p.aLen !== 4) return void 0;
@@ -164,13 +182,14 @@ function dateFromParts(p, order, zone) {
164
182
  p.c
165
183
  ];
166
184
  } else {
167
- if (p.cLen !== 4) return void 0;
185
+ if (p.cLen !== 4 && !(p.cLen === 2 && pivot !== void 0)) return void 0;
186
+ const year = p.cLen === 2 ? p.c < pivot ? 2e3 + p.c : 1900 + p.c : p.c;
168
187
  [y, mo, d] = order === "DMY" ? [
169
- p.c,
188
+ year,
170
189
  p.b,
171
190
  p.a
172
191
  ] : [
173
- p.c,
192
+ year,
174
193
  p.a,
175
194
  p.b
176
195
  ];
@@ -180,9 +199,13 @@ function dateFromParts(p, order, zone) {
180
199
  const date = new Date(t);
181
200
  return Number.isNaN(date.getTime()) ? void 0 : date;
182
201
  }
183
- /** ISO 8601 always; numeric dates (d/m/yyyy, …) only with `order`. Two-digit years are not accepted. */
202
+ /**
203
+ * ISO 8601 always; numeric dates (d/m/yyyy, …) only with `order`. Two-digit
204
+ * years only with `twoDigitYearPivot` and a DMY or MDY order.
205
+ */
184
206
  function parseDate(text, options = {}) {
185
207
  const zone = options.zone ?? "local";
208
+ const pivot = checkTwoDigitYearPivot("twoDigitYearPivot", options.twoDigitYearPivot);
186
209
  if (zone !== "local" && zone !== "utc") throw new TypeError(`zone must be 'local' or 'utc', got ${JSON.stringify(zone)}`);
187
210
  if (options.order !== void 0 && options.order !== "YMD" && options.order !== "DMY" && options.order !== "MDY") throw new TypeError(`order must be 'YMD', 'DMY' or 'MDY', got ${JSON.stringify(options.order)}`);
188
211
  const s = text.trim();
@@ -191,8 +214,8 @@ function parseDate(text, options = {}) {
191
214
  if (iso) return iso;
192
215
  }
193
216
  if (!options.order) return void 0;
194
- const p = dateParts(s);
195
- return p ? dateFromParts(p, options.order, zone) : void 0;
217
+ const p = dateParts(s, pivot !== void 0);
218
+ return p ? dateFromParts(p, options.order, zone, pivot) : void 0;
196
219
  }
197
220
  /** TRUE/FALSE in any case. */
198
221
  function parseBoolean(text) {
@@ -231,9 +254,22 @@ function textKind(text, locale) {
231
254
  }) !== void 0) return 2;
232
255
  if (parseBoolean(s) !== void 0) return 2;
233
256
  if (s.length >= 10 && s.charCodeAt(4) === 45 && parseIsoDate(s, "utc")) return 2;
234
- if (dateParts(s)) return 2;
257
+ if (s.length >= 8) {
258
+ if (dateParts(s)) return 2;
259
+ } else if (shortTwoDigitYearDate(s, locale)) return 2;
235
260
  return 1;
236
261
  }
262
+ /**
263
+ * A 6- or 7-character text such as "1.3.26" is a date only as a valid
264
+ * two-digit-year date under the locale: a pivot and a DMY or MDY order.
265
+ */
266
+ function shortTwoDigitYearDate(s, locale) {
267
+ const pivot = locale.twoDigitYearPivot;
268
+ const order = locale.dateOrder;
269
+ if (pivot === void 0 || order !== "DMY" && order !== "MDY") return false;
270
+ const p = dateParts(s, true);
271
+ return p !== void 0 && dateFromParts(p, order, "utc", pivot) !== void 0;
272
+ }
237
273
  function cellKind(c, locale) {
238
274
  if (c === void 0 || c === null) return 0;
239
275
  if (typeof c === "string") return textKind(c, locale);
@@ -251,6 +287,7 @@ function detectHeader(sheet, options = {}) {
251
287
  const maxScan = options.maxScan ?? 25;
252
288
  if (!Number.isInteger(maxScan) || maxScan < 1) throw new TypeError(`maxScan must be a positive integer, got ${String(maxScan)}`);
253
289
  const locale = options.locale ?? {};
290
+ checkTwoDigitYearPivot("locale.twoDigitYearPivot", locale.twoDigitYearPivot);
254
291
  const rng = options.range ? decodeRange(options.range) : sheet.dimension ? decodeRange(sheet.dimension) : usedRange(sheet);
255
292
  if (!rng) return {
256
293
  rows: [0],
@@ -426,18 +463,58 @@ function applyMapping(dataset, mapping, schema) {
426
463
  const { sources: _s, ...result } = applyMappingDetailed(dataset, mapping, schema);
427
464
  return result;
428
465
  }
429
- /** @internal applyMapping plus each source column key's key in the rows (columns the mapping drops are absent). */
430
- function applyMappingDetailed(dataset, mapping, schema) {
466
+ /**
467
+ * @internal applyMapping plus each source column key's key in the rows (columns the mapping drops are absent).
468
+ * With `onIssue`, issues go there with the index of their data row (-1 for field-level ones) instead of into
469
+ * `issues` (JSE-58).
470
+ */
471
+ function applyMappingDetailed(dataset, mapping, schema, onIssue) {
472
+ const { columns, plan, issues: fieldIssues, sources, byKey, fields } = mappingPlan(dataset.columns, mapping, schema);
473
+ const rowNumbers = dataset.data.map((_, i) => dataset.rowNumbers?.[i] ?? i + 2);
474
+ const issues = [];
475
+ const report = onIssue ?? ((issue) => void issues.push(issue));
476
+ for (const issue of fieldIssues) report(issue, -1);
477
+ const required = fields.filter(([name, spec]) => spec.required && plan.some((p) => p.out === name && p.from !== null)).map(([name]) => name);
478
+ return {
479
+ columns,
480
+ data: dataset.data.map((row, i) => {
481
+ const out = Object.create(null);
482
+ for (const { out: k, from } of plan) out[k] = from === null ? null : row[from] ?? null;
483
+ for (const name of required) {
484
+ const v = out[name];
485
+ if (v === null || v === void 0 || typeof v === "string" && v.trim() === "") {
486
+ const from = plan.find((p) => p.out === name).from;
487
+ const letter = byKey.get(from)?.letter;
488
+ report({
489
+ row: rowNumbers[i],
490
+ field: name,
491
+ column: from,
492
+ ...letter ? { ref: `${letter}${rowNumbers[i]}` } : {},
493
+ code: "REQUIRED",
494
+ message: `Row ${rowNumbers[i]}: "${name}" is required.`,
495
+ severity: "error"
496
+ }, i);
497
+ }
498
+ }
499
+ return out;
500
+ }),
501
+ rowNumbers,
502
+ issues,
503
+ sources
504
+ };
505
+ }
506
+ /**
507
+ * @internal What a mapping does, without the rows: the output columns, where each comes from, the
508
+ * MAPPING_MISSING issues and each kept source column's key in the rows. importDataset needs `sources`
509
+ * before it converts any value, to drop the issues of columns the mapping drops as they occur (JSE-58).
510
+ */
511
+ function mappingPlan(sourceColumns, mapping, schema) {
431
512
  const fields = fieldsOf(schema);
432
- const byKey = new Map(dataset.columns.map((c, i) => [c.key, {
433
- ...c,
434
- index: i
435
- }]));
513
+ const byKey = new Map(sourceColumns.map((c) => [c.key, c]));
436
514
  for (const [name] of fields) {
437
515
  const col = Object.hasOwn(mapping, name) ? mapping[name] : null;
438
516
  if (col !== null && col !== void 0 && !byKey.has(col)) throw new TypeError(`mapping.${name}: no column with key ${JSON.stringify(col)}`);
439
517
  }
440
- const rowNumbers = dataset.data.map((_, i) => dataset.rowNumbers?.[i] ?? i + 2);
441
518
  const issues = [];
442
519
  const columns = [];
443
520
  const plan = [];
@@ -480,7 +557,7 @@ function applyMappingDetailed(dataset, mapping, schema) {
480
557
  }
481
558
  if (schema.unmapped === "keep") {
482
559
  const mapped = new Set(plan.map((p) => p.from).filter((f) => f !== null));
483
- for (const c of dataset.columns) {
560
+ for (const c of sourceColumns) {
484
561
  if (mapped.has(c.key)) continue;
485
562
  let key = c.key;
486
563
  for (let n = 2; used.has(key); n++) key = `${c.key}_${n}`;
@@ -497,36 +574,15 @@ function applyMappingDetailed(dataset, mapping, schema) {
497
574
  });
498
575
  }
499
576
  }
500
- const required = fields.filter(([name, spec]) => spec.required && plan.some((p) => p.out === name && p.from !== null)).map(([name]) => name);
501
- const data = dataset.data.map((row, i) => {
502
- const out = Object.create(null);
503
- for (const { out: k, from } of plan) out[k] = from === null ? null : row[from] ?? null;
504
- for (const name of required) {
505
- const v = out[name];
506
- if (v === null || v === void 0 || typeof v === "string" && v.trim() === "") {
507
- const from = plan.find((p) => p.out === name).from;
508
- const letter = byKey.get(from)?.letter;
509
- issues.push({
510
- row: rowNumbers[i],
511
- field: name,
512
- column: from,
513
- ...letter ? { ref: `${letter}${rowNumbers[i]}` } : {},
514
- code: "REQUIRED",
515
- message: `Row ${rowNumbers[i]}: "${name}" is required.`,
516
- severity: "error"
517
- });
518
- }
519
- }
520
- return out;
521
- });
522
577
  const sources = /* @__PURE__ */ new Map();
523
578
  for (const p of plan) if (p.from !== null) sources.set(p.from, p.out);
524
579
  return {
525
580
  columns,
526
- data,
527
- rowNumbers,
581
+ plan,
528
582
  issues,
529
- sources
583
+ sources,
584
+ byKey,
585
+ fields
530
586
  };
531
587
  }
532
588
  // src/internal/import/types.ts
@@ -540,6 +596,36 @@ function isEmpty(v) {
540
596
  function hasTime(d, zone) {
541
597
  return zone === "utc" ? d.getUTCHours() !== 0 || d.getUTCMinutes() !== 0 || d.getUTCSeconds() !== 0 || d.getUTCMilliseconds() !== 0 : d.getHours() !== 0 || d.getMinutes() !== 0 || d.getSeconds() !== 0 || d.getMilliseconds() !== 0;
542
598
  }
599
+ /** 00:00 of `d`'s calendar day in `zone`. Setters, not the Date constructor, so years 0–99 stay themselves. An invalid Date is returned as is. */
600
+ function startOfDay(d, zone) {
601
+ if (Number.isNaN(d.getTime())) return d;
602
+ const out = new Date(d.getTime());
603
+ if (zone === "utc") out.setUTCHours(0, 0, 0, 0);
604
+ else out.setHours(0, 0, 0, 0);
605
+ return out;
606
+ }
607
+ /**
608
+ * 1899-12-31 in `zone`: Excel's day 0 ("1900-01-00") in the 1900 system. The
609
+ * reader puts time-only cells (serials 0 to <1) there; no stored date has that
610
+ * day, so a Date on it has a time and no day.
611
+ */
612
+ function isExcelDayZero(d, zone) {
613
+ return zone === "utc" ? d.getUTCFullYear() === 1899 && d.getUTCMonth() === 11 && d.getUTCDate() === 31 : d.getFullYear() === 1899 && d.getMonth() === 11 && d.getDate() === 31;
614
+ }
615
+ /**
616
+ * For 'day' (JSE-69): ISO text with an explicit offset or `Z` names its day in
617
+ * its date part, so that day, at 00:00 in `zone`; the offset does not move it.
618
+ * Undefined for anything else, including text the ISO parser refuses (an
619
+ * invalid date, time or offset), which then goes the usual way.
620
+ */
621
+ function isoDayAsWritten(text, zone) {
622
+ const s = text.trim();
623
+ if (s.length <= 16 || s.charCodeAt(4) !== 45 || parseIsoDate(s, zone) === void 0) return void 0;
624
+ for (let i = 11; i < s.length; i++) {
625
+ const c = s.charCodeAt(i);
626
+ if (c === 90 || c === 43 || c === 45) return parseIsoDate(s.slice(0, 10), zone);
627
+ }
628
+ }
543
629
  function textOf(v) {
544
630
  if (typeof v === "string") return v;
545
631
  if (v && typeof v === "object" && "richText" in v) return v.richText.map((r) => r.text).join("");
@@ -553,22 +639,26 @@ function numberOptions(locale) {
553
639
  /**
554
640
  * The date order of a column of numeric date texts, decided from the data
555
641
  * only: undefined when nothing proves an order, or the evidence conflicts.
642
+ * With a two-digit-year pivot, a two-digit year makes the column ambiguous:
643
+ * 31.10.26 may be D.M.YY or YY.MM.DD, so 'auto' never guesses it.
556
644
  */
557
- function dateOrderOf(texts, requested) {
645
+ function dateOrderOf(texts, requested, pivot) {
558
646
  if (requested && requested !== "auto") return {
559
647
  order: requested,
560
648
  ambiguous: false
561
649
  };
562
- let ymd = 0, dmy = 0, mdy = 0, any = 0;
650
+ let ymd = 0, dmy = 0, mdy = 0, any = 0, twoDigit = 0;
563
651
  for (const t of texts) {
564
- const p = dateParts(t);
652
+ const p = dateParts(t, pivot !== void 0);
565
653
  if (!p) continue;
566
654
  any++;
567
- if (p.aLen === 4) ymd++;
655
+ if (pivot !== void 0 && p.aLen !== 4 && p.cLen === 2) twoDigit++;
656
+ else if (p.aLen === 4) ymd++;
568
657
  else if (p.a > 12) dmy++;
569
658
  else if (p.b > 12) mdy++;
570
659
  }
571
660
  if (any === 0) return { ambiguous: false };
661
+ if (twoDigit > 0) return { ambiguous: true };
572
662
  if (ymd === any) return {
573
663
  order: "YMD",
574
664
  ambiguous: false
@@ -584,15 +674,15 @@ function dateOrderOf(texts, requested) {
584
674
  };
585
675
  return { ambiguous: true };
586
676
  }
587
- function textToDate(t, order, zone) {
677
+ function textToDate(t, order, zone, pivot) {
588
678
  const s = t.trim();
589
679
  if (s.length >= 10 && s.charCodeAt(4) === 45) {
590
680
  const iso = parseIsoDate(s, zone);
591
681
  if (iso) return iso;
592
682
  }
593
683
  if (!order) return void 0;
594
- const p = dateParts(s);
595
- return p ? dateFromParts(p, order, zone) : void 0;
684
+ const p = dateParts(s, pivot !== void 0);
685
+ return p ? dateFromParts(p, order, zone, pivot) : void 0;
596
686
  }
597
687
  function inferOne(key, values, locale, zone, convertText) {
598
688
  const present = values.filter((v) => !isEmpty(v) && !(v && typeof v === "object" && "error" in v));
@@ -627,11 +717,12 @@ function inferOne(key, values, locale, zone, convertText) {
627
717
  const no = numberOptions(locale);
628
718
  const textNumbers = texts.length > 0 && texts.every((t) => parseNumber(t, no) !== void 0);
629
719
  const textBools = texts.length > 0 && texts.every((t) => parseBoolean(t) !== void 0);
630
- const { order, ambiguous } = dateOrderOf(texts, locale.dateOrder);
720
+ const pivot = locale.twoDigitYearPivot;
721
+ const { order, ambiguous } = dateOrderOf(texts, locale.dateOrder, pivot);
631
722
  let textDates = false;
632
723
  let textTimed = false;
633
724
  if (texts.length > 0 && !textNumbers) textDates = texts.every((t) => {
634
- const d = textToDate(t, order, zone);
725
+ const d = textToDate(t, order, zone, pivot);
635
726
  if (d && hasTime(d, zone)) textTimed = true;
636
727
  return d !== void 0;
637
728
  });
@@ -665,7 +756,7 @@ function inferOne(key, values, locale, zone, convertText) {
665
756
  if (typedKinds === 0) return {
666
757
  key,
667
758
  type: "string",
668
- ambiguousDates: ambiguous && texts.every((t) => dateParts(t) !== void 0 || t.trim().length >= 10 && parseIsoDate(t.trim(), zone))
759
+ ambiguousDates: ambiguous && texts.every((t) => dateParts(t, pivot !== void 0) !== void 0 || t.trim().length >= 10 && parseIsoDate(t.trim(), zone))
669
760
  };
670
761
  return {
671
762
  key,
@@ -686,6 +777,7 @@ function inferColumnTypes(dataset, options = {}) {
686
777
  /** @internal inferColumnTypes plus the ambiguity flag the pipeline reports. */
687
778
  function inferDetailed(dataset, options = {}) {
688
779
  const locale = options.locale ?? {};
780
+ checkTwoDigitYearPivot("locale.twoDigitYearPivot", locale.twoDigitYearPivot);
689
781
  const zone = options.dateZone ?? "local";
690
782
  const convertText = options.convertText ?? true;
691
783
  return dataset.columns.map(({ key }) => inferOne(key, dataset.data.map((row) => row[key]), locale, zone, convertText));
@@ -742,16 +834,26 @@ function convert(v, col, locale, zone) {
742
834
  };
743
835
  }
744
836
  case "date":
745
- case "datetime": {
746
- if (v instanceof Date) return {
837
+ case "datetime":
838
+ case "day": {
839
+ let d;
840
+ if (v instanceof Date) d = v;
841
+ else if (t !== void 0) {
842
+ if (col.type === "day") d = isoDayAsWritten(t, zone);
843
+ if (d === void 0) {
844
+ const order = col.dateOrder ?? (locale.dateOrder && locale.dateOrder !== "auto" ? locale.dateOrder : void 0);
845
+ d = order ? textToDate(t, order, zone, locale.twoDigitYearPivot) : parseDate(t, { zone });
846
+ }
847
+ }
848
+ if (d === void 0) return { ok: false };
849
+ if (col.type !== "day") return {
747
850
  ok: true,
748
- value: v
851
+ value: d
749
852
  };
750
- const order = col.dateOrder ?? (locale.dateOrder && locale.dateOrder !== "auto" ? locale.dateOrder : void 0);
751
- const d = t === void 0 ? void 0 : order ? textToDate(t, order, zone) : parseDate(t, { zone });
752
- return d === void 0 ? { ok: false } : {
853
+ if (v instanceof Date && isExcelDayZero(v, zone)) return { ok: false };
854
+ return {
753
855
  ok: true,
754
- value: d
856
+ value: startOfDay(d, zone)
755
857
  };
756
858
  }
757
859
  default: return {
@@ -761,9 +863,21 @@ function convert(v, col, locale, zone) {
761
863
  }
762
864
  }
763
865
  function coerceColumns(dataset, types, options = {}) {
866
+ const issues = [];
867
+ return {
868
+ data: coerceInto(dataset, types, options, (issue) => issues.push(issue)),
869
+ issues
870
+ };
871
+ }
872
+ /**
873
+ * @internal coerceColumns, handing each issue to `onIssue` with the index of
874
+ * its data row instead of collecting them, so importDataset can bound the
875
+ * list while it is built (JSE-58).
876
+ */
877
+ function coerceInto(dataset, types, options, onIssue) {
764
878
  const locale = options.locale ?? {};
879
+ checkTwoDigitYearPivot("locale.twoDigitYearPivot", locale.twoDigitYearPivot);
765
880
  const zone = options.dateZone ?? "local";
766
- const issues = [];
767
881
  const plan = dataset.columns.map((c) => {
768
882
  const t = Object.hasOwn(types, c.key) ? types[c.key] : void 0;
769
883
  if (t === void 0) return void 0;
@@ -775,33 +889,57 @@ function coerceColumns(dataset, types, options = {}) {
775
889
  } : t
776
890
  };
777
891
  }).filter((x) => x !== void 0);
778
- return {
779
- data: dataset.data.map((row, i) => {
780
- const out = Object.create(null);
781
- for (const k of Object.keys(row)) out[k] = row[k];
782
- for (const { column, spec } of plan) {
783
- const r = convert(row[column.key], spec, locale, zone);
784
- if (r.ok) out[column.key] = r.value;
785
- else {
786
- const rowNumber = dataset.rowNumbers?.[i] ?? i + 2;
787
- out[column.key] = null;
788
- issues.push({
789
- row: rowNumber,
790
- field: column.key,
791
- column: column.key,
792
- ...column.letter ? { ref: `${column.letter}${rowNumber}` } : {},
793
- code: "TYPE",
794
- message: `${column.letter ? `${column.letter}${rowNumber}` : `Row ${rowNumber}`}: ${JSON.stringify(String(textOf(row[column.key]) ?? row[column.key]))} is not a ${spec.type}.`,
795
- severity: "error"
796
- });
797
- }
892
+ return dataset.data.map((row, i) => {
893
+ const out = Object.create(null);
894
+ for (const k of Object.keys(row)) out[k] = row[k];
895
+ for (const { column, spec } of plan) {
896
+ const r = convert(row[column.key], spec, locale, zone);
897
+ if (r.ok) out[column.key] = r.value;
898
+ else {
899
+ const rowNumber = dataset.rowNumbers?.[i] ?? i + 2;
900
+ out[column.key] = null;
901
+ onIssue({
902
+ row: rowNumber,
903
+ field: column.key,
904
+ column: column.key,
905
+ ...column.letter ? { ref: `${column.letter}${rowNumber}` } : {},
906
+ code: "TYPE",
907
+ message: `${column.letter ? `${column.letter}${rowNumber}` : `Row ${rowNumber}`}: ${quoted(String(textOf(row[column.key]) ?? row[column.key]))} is not a ${spec.type}.`,
908
+ severity: "error"
909
+ }, i);
798
910
  }
799
- return out;
800
- }),
801
- issues
802
- };
911
+ }
912
+ return out;
913
+ });
803
914
  }
804
- // src/internal/import/pipeline.ts
915
+ /**
916
+ * The issue list of one import, bounded (JSE-58). Every issue is kept until
917
+ * `max` are; after that an issue is kept only if it is the first of its code,
918
+ * and the others are counted. A code's severity never varies, so a list cut
919
+ * short still has an `error` issue whenever one was dropped.
920
+ */
921
+ var IssueCap = class {
922
+ max;
923
+ issues = [];
924
+ dropped;
925
+ codes = /* @__PURE__ */ new Set();
926
+ constructor(max) {
927
+ this.max = max;
928
+ }
929
+ add(issue) {
930
+ if (this.issues.length < this.max || !this.codes.has(issue.code)) {
931
+ this.issues.push(issue);
932
+ this.codes.add(issue.code);
933
+ return;
934
+ }
935
+ const d = this.dropped ??= {
936
+ total: 0,
937
+ byCode: {}
938
+ };
939
+ d.total++;
940
+ d.byCode[issue.code] = (d.byCode[issue.code] ?? 0) + 1;
941
+ }
942
+ };
805
943
  const CSV_NAME = /\.(csv|tsv|txt)$/i;
806
944
  const CSV_MIME = /* @__PURE__ */ new Set([
807
945
  "text/csv",
@@ -833,28 +971,33 @@ function readCap(options) {
833
971
  return (h === false ? 0 : typeof h === "number" ? h + 1 : typeof h === "object" ? (h.row === void 0 ? 25 : h.row) + h.rows : 25) + rows + 1;
834
972
  }
835
973
  /**
836
- * Issues after mapping: `field` becomes the key in the rows, issues on
837
- * columns the mapping drops go, and a cell with a TYPE issue gets no
838
- * REQUIRED issue as well (its value didn't convert; it wasn't empty).
974
+ * Takes each issue as it occurs, in the order 2.2 listed them, and keeps it
975
+ * subject to the cap. With a mapping (`sources`, from source column key to
976
+ * its key in the rows): `field` becomes the key in the rows, issues on
977
+ * columns the mapping drops go, and a cell with a TYPE issue gets no REQUIRED
978
+ * issue as well (its value didn't convert; it wasn't empty), also when the
979
+ * cap dropped the TYPE issue. Before JSE-58 the whole list was built first
980
+ * and then filtered; now nothing unbounded is built.
839
981
  */
840
- function renameIssues(issues, sources) {
841
- const cell = (row, column) => `${row}\u0000${column}`;
842
- const typed = new Set(issues.filter((i) => i.code === "TYPE" && i.column !== void 0).map((i) => cell(i.row, i.column)));
843
- const out = [];
844
- for (const i of issues) {
845
- if (i.column === void 0) {
846
- out.push(i);
847
- continue;
982
+ function issueSink(cap, sources, rows) {
983
+ const typed = /* @__PURE__ */ new Map();
984
+ return (issue, index = -1) => {
985
+ if (sources && issue.column !== void 0) {
986
+ const field = sources.get(issue.column);
987
+ if (field === void 0) return;
988
+ if (index >= 0 && issue.code === "TYPE") {
989
+ let marks = typed.get(issue.column);
990
+ if (!marks) typed.set(issue.column, marks = new Uint8Array(rows));
991
+ marks[index] = 1;
992
+ }
993
+ if (index >= 0 && issue.code === "REQUIRED" && typed.get(issue.column)?.[index] === 1) return;
994
+ if (issue.field !== field) issue = {
995
+ ...issue,
996
+ field
997
+ };
848
998
  }
849
- const field = sources.get(i.column);
850
- if (field === void 0) continue;
851
- if (i.code === "REQUIRED" && typed.has(cell(i.row, i.column))) continue;
852
- out.push(i.field === field ? i : {
853
- ...i,
854
- field
855
- });
856
- }
857
- return out;
999
+ cap.add(issue);
1000
+ };
858
1001
  }
859
1002
  async function importDataset(input, options = {}) {
860
1003
  const o = options;
@@ -873,9 +1016,15 @@ async function importDataset(input, options = {}) {
873
1016
  "DMY",
874
1017
  "MDY"
875
1018
  ]);
1019
+ checkTwoDigitYearPivot("locale.twoDigitYearPivot", o.locale?.twoDigitYearPivot);
876
1020
  checkCount("maxRows", o.maxRows, 0);
877
1021
  if (o.limits?.maxWarnings !== void 0) checkMaxWarnings("limits.maxWarnings", o.limits.maxWarnings);
878
1022
  checkCount("preview", o.preview, 0);
1023
+ if (o.maxIssues !== Number.POSITIVE_INFINITY) checkCount("maxIssues", o.maxIssues, 1);
1024
+ const maxText = o.limits?.maxCellTextLength;
1025
+ if (maxText !== void 0 && (typeof maxText !== "number" || !(maxText > 0))) throw new TypeError("limits.maxCellTextLength must be a positive number");
1026
+ const maxColumns = o.limits?.maxColumns;
1027
+ if (maxColumns !== void 0 && (typeof maxColumns !== "number" || !(maxColumns > 0))) throw new TypeError("limits.maxColumns must be a positive number");
879
1028
  if (typeof o.header === "object" && o.header !== null) {
880
1029
  checkCount("header.rows", o.header.rows, 1);
881
1030
  checkCount("header.row", o.header.row, 0);
@@ -928,6 +1077,9 @@ async function importDataset(input, options = {}) {
928
1077
  encoding: o.encoding,
929
1078
  maxRows: cap,
930
1079
  maxWarnings: o.limits?.maxWarnings,
1080
+ maxCellTextLength: maxText,
1081
+ maxColumns,
1082
+ maxFieldBytes: maxText !== void 0 && maxText > 1048576 ? 3 * maxText : void 0,
931
1083
  signal: o.signal,
932
1084
  inferTypes: false
933
1085
  });
@@ -953,7 +1105,7 @@ async function importDataset(input, options = {}) {
953
1105
  sheet: want,
954
1106
  sheets: sheets.map((s) => s.name)
955
1107
  } });
956
- const { workbook } = await read(bytes, {
1108
+ const { workbook } = await readImpl(bytes, {
957
1109
  sheets: [info.index],
958
1110
  styles: false,
959
1111
  richText: "plain",
@@ -968,7 +1120,7 @@ async function importDataset(input, options = {}) {
968
1120
  if (w.code === "ROWS_TRUNCATED") truncatedRead = true;
969
1121
  warn(w);
970
1122
  }
971
- });
1123
+ }, { sharedFormulaText: "master" });
972
1124
  sheet = workbook.sheets[info.index];
973
1125
  meta.sheet = {
974
1126
  name: info.name,
@@ -1007,14 +1159,17 @@ async function importDataset(input, options = {}) {
1007
1159
  headerRows = Array.from({ length: header.rows }, (_, i) => start + i);
1008
1160
  join = header.join;
1009
1161
  }
1162
+ const maxCells = o.limits?.maxCells;
1163
+ if (maxCells !== void 0 && (typeof maxCells !== "number" || !(maxCells > 0))) throw new TypeError("limits.maxCells must be a positive number");
1010
1164
  const ds = toDatasetDetailed(sheet, {
1011
1165
  headerRow: headerRows.length ? headerRows[0] : false,
1012
1166
  headerRows: Math.max(1, headerRows.length),
1013
1167
  join,
1014
1168
  range: o.range,
1015
1169
  skipTotals: o.skipTotals ?? "auto"
1016
- });
1017
- const issues = [];
1170
+ }, { maxCells: maxCells ?? DEFAULT_LIMITS.maxCells });
1171
+ const issueCap = new IssueCap(o.maxIssues ?? 1e5);
1172
+ const issues = issueCap.issues;
1018
1173
  const base = {
1019
1174
  header: {
1020
1175
  rows: headerRows,
@@ -1025,7 +1180,7 @@ async function importDataset(input, options = {}) {
1025
1180
  totalsRowSkipped: ds.totalsSkipped
1026
1181
  };
1027
1182
  if (ds.range === void 0 || ds.columns.length === 0) {
1028
- issues.push({
1183
+ issueCap.add({
1029
1184
  row: 0,
1030
1185
  code: "EMPTY",
1031
1186
  message: "The file has no data.",
@@ -1057,12 +1212,24 @@ async function importDataset(input, options = {}) {
1057
1212
  value: data.length,
1058
1213
  max: o.maxRows
1059
1214
  } });
1060
- if (data.length === 0) issues.push({
1215
+ if (data.length === 0) issueCap.add({
1061
1216
  row: 0,
1062
1217
  code: "EMPTY",
1063
1218
  message: "The file has a header but no data rows.",
1064
1219
  severity: "warning"
1065
1220
  });
1221
+ throwIfAborted(o.signal);
1222
+ const keyOf = (name) => {
1223
+ const byKey = ds.columns.find((c) => c.key === name);
1224
+ if (byKey) return byKey.key;
1225
+ const byTitle = ds.columns.find((c) => c.title === name);
1226
+ if (byTitle) return byTitle.key;
1227
+ throw new TypeError(`types: no column with key or title ${JSON.stringify(name)}`);
1228
+ };
1229
+ const forced = /* @__PURE__ */ new Map();
1230
+ if (typeof o.types === "object" && o.types !== null) for (const name of Object.keys(o.types)) forced.set(keyOf(name), o.types[name]);
1231
+ const mapped = o.schema && o.preview === void 0 ? mapColumns(ds.columns, o.schema, o.mapping) : void 0;
1232
+ const report = issueSink(issueCap, mapped ? mappingPlan(ds.columns, mapped.mapping, o.schema).sources : void 0, data.length);
1066
1233
  if ((o.errorCells ?? "null") === "null") for (let i = 0; i < data.length; i++) {
1067
1234
  const row = data[i];
1068
1235
  for (const c of ds.columns) {
@@ -1070,28 +1237,18 @@ async function importDataset(input, options = {}) {
1070
1237
  if (!isError(v)) continue;
1071
1238
  row[c.key] = null;
1072
1239
  const ref = `${c.letter}${rowNumbers[i]}`;
1073
- issues.push({
1240
+ report({
1074
1241
  row: rowNumbers[i],
1075
1242
  field: c.key,
1076
1243
  column: c.key,
1077
1244
  ref,
1078
1245
  code: "ERROR_CELL",
1079
- message: `${ref} holds the error ${v.error}; read as empty.`,
1246
+ message: `${ref} holds the error ${excerpt(String(v.error))}; read as empty.`,
1080
1247
  severity: "warning"
1081
1248
  });
1082
1249
  }
1083
1250
  }
1084
1251
  throwIfAborted(o.signal);
1085
- const keyOf = (name) => {
1086
- const byKey = ds.columns.find((c) => c.key === name);
1087
- if (byKey) return byKey.key;
1088
- const byTitle = ds.columns.find((c) => c.title === name);
1089
- if (byTitle) return byTitle.key;
1090
- throw new TypeError(`types: no column with key or title ${JSON.stringify(name)}`);
1091
- };
1092
- const forced = /* @__PURE__ */ new Map();
1093
- if (typeof o.types === "object" && o.types !== null) for (const name of Object.keys(o.types)) forced.set(keyOf(name), o.types[name]);
1094
- const mapped = o.schema && o.preview === void 0 ? mapColumns(ds.columns, o.schema, o.mapping) : void 0;
1095
1252
  if (mapped) for (const [field, spec] of Object.entries(o.schema.fields)) {
1096
1253
  const col = mapped.mapping[field];
1097
1254
  if (spec.type && col) forced.set(col, spec.type);
@@ -1121,21 +1278,20 @@ async function importDataset(input, options = {}) {
1121
1278
  } else {
1122
1279
  if (mode === "infer") plan[inf.key] = inf;
1123
1280
  typeOf.set(inf.key, inf.type);
1124
- if (inf.ambiguousDates) issues.push({
1281
+ if (inf.ambiguousDates) report({
1125
1282
  row: 0,
1126
1283
  field: inf.key,
1127
1284
  column: inf.key,
1128
1285
  code: "DATE_ORDER_AMBIGUOUS",
1129
- message: `Column "${inf.key}" looks like dates, but the day/month order can't be told from the data; set locale.dateOrder.`,
1286
+ message: `Column "${excerpt(inf.key)}" looks like dates, but the day/month order can't be told from the data; set locale.dateOrder.`,
1130
1287
  severity: "warning"
1131
1288
  });
1132
1289
  }
1133
1290
  }
1134
- const coerced = coerceColumns(dsLike, plan, {
1291
+ const coerced = { data: coerceInto(dsLike, plan, {
1135
1292
  locale,
1136
1293
  dateZone: zone
1137
- });
1138
- issues.push(...coerced.issues);
1294
+ }, report) };
1139
1295
  const typedColumns = ds.columns.map((c) => ({
1140
1296
  key: c.key,
1141
1297
  title: c.title,
@@ -1145,14 +1301,12 @@ async function importDataset(input, options = {}) {
1145
1301
  progress("dataset", .97);
1146
1302
  let result;
1147
1303
  if (mapped) {
1148
- const { sources, ...m } = applyMappingDetailed({
1304
+ const { sources: _sources, issues: _none, ...m } = applyMappingDetailed({
1149
1305
  columns: typedColumns,
1150
1306
  data: coerced.data,
1151
1307
  rowNumbers
1152
- }, mapped.mapping, o.schema);
1153
- issues.push(...m.issues);
1308
+ }, mapped.mapping, o.schema, report);
1154
1309
  result = m;
1155
- issues.splice(0, issues.length, ...renameIssues(issues, sources));
1156
1310
  meta.mapping = mapped.mapping;
1157
1311
  meta.unmappedColumns = mapped.unmapped;
1158
1312
  } else result = {
@@ -1161,6 +1315,7 @@ async function importDataset(input, options = {}) {
1161
1315
  rowNumbers
1162
1316
  };
1163
1317
  progress("dataset", 1);
1318
+ if (issueCap.dropped) meta.droppedIssues = issueCap.dropped;
1164
1319
  return {
1165
1320
  ...result,
1166
1321
  issues,