javascript-excel 2.1.0 → 2.3.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
- const require_csv = require("./csv-DzGOFyHs.cjs");
1
+ const require_csv = require("./csv-f4P3fGs-.cjs");
2
2
  const require_dataset = require("./dataset-DzoMeOG1.cjs");
3
3
  const require_errors = require("./errors-DOPrrGS4.cjs");
4
- const require_read = require("./read-DYDDLxUE.cjs");
4
+ const require_read = require("./read-BYI7EVMo.cjs");
5
5
  const require_infer = require("./infer-BniOucP6.cjs");
6
6
  const require_address = require("./address-DZBHchLv.cjs");
7
7
  const require_iso_date = require("./iso-date-3TY1OlGd.cjs");
@@ -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 && require_infer.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 ? require_address.decodeRange(options.range) : sheet.dimension ? require_address.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,22 @@ 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
+ }
543
615
  function textOf(v) {
544
616
  if (typeof v === "string") return v;
545
617
  if (v && typeof v === "object" && "richText" in v) return v.richText.map((r) => r.text).join("");
@@ -553,22 +625,26 @@ function numberOptions(locale) {
553
625
  /**
554
626
  * The date order of a column of numeric date texts, decided from the data
555
627
  * only: undefined when nothing proves an order, or the evidence conflicts.
628
+ * With a two-digit-year pivot, a two-digit year makes the column ambiguous:
629
+ * 31.10.26 may be D.M.YY or YY.MM.DD, so 'auto' never guesses it.
556
630
  */
557
- function dateOrderOf(texts, requested) {
631
+ function dateOrderOf(texts, requested, pivot) {
558
632
  if (requested && requested !== "auto") return {
559
633
  order: requested,
560
634
  ambiguous: false
561
635
  };
562
- let ymd = 0, dmy = 0, mdy = 0, any = 0;
636
+ let ymd = 0, dmy = 0, mdy = 0, any = 0, twoDigit = 0;
563
637
  for (const t of texts) {
564
- const p = dateParts(t);
638
+ const p = dateParts(t, pivot !== void 0);
565
639
  if (!p) continue;
566
640
  any++;
567
- if (p.aLen === 4) ymd++;
641
+ if (pivot !== void 0 && p.aLen !== 4 && p.cLen === 2) twoDigit++;
642
+ else if (p.aLen === 4) ymd++;
568
643
  else if (p.a > 12) dmy++;
569
644
  else if (p.b > 12) mdy++;
570
645
  }
571
646
  if (any === 0) return { ambiguous: false };
647
+ if (twoDigit > 0) return { ambiguous: true };
572
648
  if (ymd === any) return {
573
649
  order: "YMD",
574
650
  ambiguous: false
@@ -584,15 +660,15 @@ function dateOrderOf(texts, requested) {
584
660
  };
585
661
  return { ambiguous: true };
586
662
  }
587
- function textToDate(t, order, zone) {
663
+ function textToDate(t, order, zone, pivot) {
588
664
  const s = t.trim();
589
665
  if (s.length >= 10 && s.charCodeAt(4) === 45) {
590
666
  const iso = require_infer.parseIsoDate(s, zone);
591
667
  if (iso) return iso;
592
668
  }
593
669
  if (!order) return void 0;
594
- const p = dateParts(s);
595
- return p ? dateFromParts(p, order, zone) : void 0;
670
+ const p = dateParts(s, pivot !== void 0);
671
+ return p ? dateFromParts(p, order, zone, pivot) : void 0;
596
672
  }
597
673
  function inferOne(key, values, locale, zone, convertText) {
598
674
  const present = values.filter((v) => !isEmpty(v) && !(v && typeof v === "object" && "error" in v));
@@ -627,11 +703,12 @@ function inferOne(key, values, locale, zone, convertText) {
627
703
  const no = numberOptions(locale);
628
704
  const textNumbers = texts.length > 0 && texts.every((t) => parseNumber(t, no) !== void 0);
629
705
  const textBools = texts.length > 0 && texts.every((t) => parseBoolean(t) !== void 0);
630
- const { order, ambiguous } = dateOrderOf(texts, locale.dateOrder);
706
+ const pivot = locale.twoDigitYearPivot;
707
+ const { order, ambiguous } = dateOrderOf(texts, locale.dateOrder, pivot);
631
708
  let textDates = false;
632
709
  let textTimed = false;
633
710
  if (texts.length > 0 && !textNumbers) textDates = texts.every((t) => {
634
- const d = textToDate(t, order, zone);
711
+ const d = textToDate(t, order, zone, pivot);
635
712
  if (d && hasTime(d, zone)) textTimed = true;
636
713
  return d !== void 0;
637
714
  });
@@ -665,7 +742,7 @@ function inferOne(key, values, locale, zone, convertText) {
665
742
  if (typedKinds === 0) return {
666
743
  key,
667
744
  type: "string",
668
- ambiguousDates: ambiguous && texts.every((t) => dateParts(t) !== void 0 || t.trim().length >= 10 && require_infer.parseIsoDate(t.trim(), zone))
745
+ ambiguousDates: ambiguous && texts.every((t) => dateParts(t, pivot !== void 0) !== void 0 || t.trim().length >= 10 && require_infer.parseIsoDate(t.trim(), zone))
669
746
  };
670
747
  return {
671
748
  key,
@@ -686,6 +763,7 @@ function inferColumnTypes(dataset, options = {}) {
686
763
  /** @internal inferColumnTypes plus the ambiguity flag the pipeline reports. */
687
764
  function inferDetailed(dataset, options = {}) {
688
765
  const locale = options.locale ?? {};
766
+ checkTwoDigitYearPivot("locale.twoDigitYearPivot", locale.twoDigitYearPivot);
689
767
  const zone = options.dateZone ?? "local";
690
768
  const convertText = options.convertText ?? true;
691
769
  return dataset.columns.map(({ key }) => inferOne(key, dataset.data.map((row) => row[key]), locale, zone, convertText));
@@ -742,16 +820,23 @@ function convert(v, col, locale, zone) {
742
820
  };
743
821
  }
744
822
  case "date":
745
- case "datetime": {
746
- if (v instanceof Date) return {
823
+ case "datetime":
824
+ case "day": {
825
+ let d;
826
+ if (v instanceof Date) d = v;
827
+ else if (t !== void 0) {
828
+ const order = col.dateOrder ?? (locale.dateOrder && locale.dateOrder !== "auto" ? locale.dateOrder : void 0);
829
+ d = order ? textToDate(t, order, zone, locale.twoDigitYearPivot) : parseDate(t, { zone });
830
+ }
831
+ if (d === void 0) return { ok: false };
832
+ if (col.type !== "day") return {
747
833
  ok: true,
748
- value: v
834
+ value: d
749
835
  };
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 } : {
836
+ if (v instanceof Date && isExcelDayZero(v, zone)) return { ok: false };
837
+ return {
753
838
  ok: true,
754
- value: d
839
+ value: startOfDay(d, zone)
755
840
  };
756
841
  }
757
842
  default: return {
@@ -761,9 +846,21 @@ function convert(v, col, locale, zone) {
761
846
  }
762
847
  }
763
848
  function coerceColumns(dataset, types, options = {}) {
849
+ const issues = [];
850
+ return {
851
+ data: coerceInto(dataset, types, options, (issue) => issues.push(issue)),
852
+ issues
853
+ };
854
+ }
855
+ /**
856
+ * @internal coerceColumns, handing each issue to `onIssue` with the index of
857
+ * its data row instead of collecting them, so importDataset can bound the
858
+ * list while it is built (JSE-58).
859
+ */
860
+ function coerceInto(dataset, types, options, onIssue) {
764
861
  const locale = options.locale ?? {};
862
+ checkTwoDigitYearPivot("locale.twoDigitYearPivot", locale.twoDigitYearPivot);
765
863
  const zone = options.dateZone ?? "local";
766
- const issues = [];
767
864
  const plan = dataset.columns.map((c) => {
768
865
  const t = Object.hasOwn(types, c.key) ? types[c.key] : void 0;
769
866
  if (t === void 0) return void 0;
@@ -775,33 +872,57 @@ function coerceColumns(dataset, types, options = {}) {
775
872
  } : t
776
873
  };
777
874
  }).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
- }
875
+ return dataset.data.map((row, i) => {
876
+ const out = Object.create(null);
877
+ for (const k of Object.keys(row)) out[k] = row[k];
878
+ for (const { column, spec } of plan) {
879
+ const r = convert(row[column.key], spec, locale, zone);
880
+ if (r.ok) out[column.key] = r.value;
881
+ else {
882
+ const rowNumber = dataset.rowNumbers?.[i] ?? i + 2;
883
+ out[column.key] = null;
884
+ onIssue({
885
+ row: rowNumber,
886
+ field: column.key,
887
+ column: column.key,
888
+ ...column.letter ? { ref: `${column.letter}${rowNumber}` } : {},
889
+ code: "TYPE",
890
+ message: `${column.letter ? `${column.letter}${rowNumber}` : `Row ${rowNumber}`}: ${require_read.quoted(String(textOf(row[column.key]) ?? row[column.key]))} is not a ${spec.type}.`,
891
+ severity: "error"
892
+ }, i);
798
893
  }
799
- return out;
800
- }),
801
- issues
802
- };
894
+ }
895
+ return out;
896
+ });
803
897
  }
804
- // src/internal/import/pipeline.ts
898
+ /**
899
+ * The issue list of one import, bounded (JSE-58). Every issue is kept until
900
+ * `max` are; after that an issue is kept only if it is the first of its code,
901
+ * and the others are counted. A code's severity never varies, so a list cut
902
+ * short still has an `error` issue whenever one was dropped.
903
+ */
904
+ var IssueCap = class {
905
+ max;
906
+ issues = [];
907
+ dropped;
908
+ codes = /* @__PURE__ */ new Set();
909
+ constructor(max) {
910
+ this.max = max;
911
+ }
912
+ add(issue) {
913
+ if (this.issues.length < this.max || !this.codes.has(issue.code)) {
914
+ this.issues.push(issue);
915
+ this.codes.add(issue.code);
916
+ return;
917
+ }
918
+ const d = this.dropped ??= {
919
+ total: 0,
920
+ byCode: {}
921
+ };
922
+ d.total++;
923
+ d.byCode[issue.code] = (d.byCode[issue.code] ?? 0) + 1;
924
+ }
925
+ };
805
926
  const CSV_NAME = /\.(csv|tsv|txt)$/i;
806
927
  const CSV_MIME = /* @__PURE__ */ new Set([
807
928
  "text/csv",
@@ -833,28 +954,33 @@ function readCap(options) {
833
954
  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
955
  }
835
956
  /**
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).
957
+ * Takes each issue as it occurs, in the order 2.2 listed them, and keeps it
958
+ * subject to the cap. With a mapping (`sources`, from source column key to
959
+ * its key in the rows): `field` becomes the key in the rows, issues on
960
+ * columns the mapping drops go, and a cell with a TYPE issue gets no REQUIRED
961
+ * issue as well (its value didn't convert; it wasn't empty), also when the
962
+ * cap dropped the TYPE issue. Before JSE-58 the whole list was built first
963
+ * and then filtered; now nothing unbounded is built.
839
964
  */
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;
965
+ function issueSink(cap, sources, rows) {
966
+ const typed = /* @__PURE__ */ new Map();
967
+ return (issue, index = -1) => {
968
+ if (sources && issue.column !== void 0) {
969
+ const field = sources.get(issue.column);
970
+ if (field === void 0) return;
971
+ if (index >= 0 && issue.code === "TYPE") {
972
+ let marks = typed.get(issue.column);
973
+ if (!marks) typed.set(issue.column, marks = new Uint8Array(rows));
974
+ marks[index] = 1;
975
+ }
976
+ if (index >= 0 && issue.code === "REQUIRED" && typed.get(issue.column)?.[index] === 1) return;
977
+ if (issue.field !== field) issue = {
978
+ ...issue,
979
+ field
980
+ };
848
981
  }
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;
982
+ cap.add(issue);
983
+ };
858
984
  }
859
985
  async function importDataset(input, options = {}) {
860
986
  const o = options;
@@ -873,9 +999,13 @@ async function importDataset(input, options = {}) {
873
999
  "DMY",
874
1000
  "MDY"
875
1001
  ]);
1002
+ checkTwoDigitYearPivot("locale.twoDigitYearPivot", o.locale?.twoDigitYearPivot);
876
1003
  checkCount("maxRows", o.maxRows, 0);
877
1004
  if (o.limits?.maxWarnings !== void 0) require_infer.checkMaxWarnings("limits.maxWarnings", o.limits.maxWarnings);
878
1005
  checkCount("preview", o.preview, 0);
1006
+ if (o.maxIssues !== Number.POSITIVE_INFINITY) checkCount("maxIssues", o.maxIssues, 1);
1007
+ const maxText = o.limits?.maxCellTextLength;
1008
+ if (maxText !== void 0 && (typeof maxText !== "number" || !(maxText > 0))) throw new TypeError("limits.maxCellTextLength must be a positive number");
879
1009
  if (typeof o.header === "object" && o.header !== null) {
880
1010
  checkCount("header.rows", o.header.rows, 1);
881
1011
  checkCount("header.row", o.header.row, 0);
@@ -928,6 +1058,8 @@ async function importDataset(input, options = {}) {
928
1058
  encoding: o.encoding,
929
1059
  maxRows: cap,
930
1060
  maxWarnings: o.limits?.maxWarnings,
1061
+ maxCellTextLength: maxText,
1062
+ maxFieldBytes: maxText !== void 0 && maxText > 1048576 ? 3 * maxText : void 0,
931
1063
  signal: o.signal,
932
1064
  inferTypes: false
933
1065
  });
@@ -1014,7 +1146,8 @@ async function importDataset(input, options = {}) {
1014
1146
  range: o.range,
1015
1147
  skipTotals: o.skipTotals ?? "auto"
1016
1148
  });
1017
- const issues = [];
1149
+ const issueCap = new IssueCap(o.maxIssues ?? 1e5);
1150
+ const issues = issueCap.issues;
1018
1151
  const base = {
1019
1152
  header: {
1020
1153
  rows: headerRows,
@@ -1025,7 +1158,7 @@ async function importDataset(input, options = {}) {
1025
1158
  totalsRowSkipped: ds.totalsSkipped
1026
1159
  };
1027
1160
  if (ds.range === void 0 || ds.columns.length === 0) {
1028
- issues.push({
1161
+ issueCap.add({
1029
1162
  row: 0,
1030
1163
  code: "EMPTY",
1031
1164
  message: "The file has no data.",
@@ -1057,12 +1190,24 @@ async function importDataset(input, options = {}) {
1057
1190
  value: data.length,
1058
1191
  max: o.maxRows
1059
1192
  } });
1060
- if (data.length === 0) issues.push({
1193
+ if (data.length === 0) issueCap.add({
1061
1194
  row: 0,
1062
1195
  code: "EMPTY",
1063
1196
  message: "The file has a header but no data rows.",
1064
1197
  severity: "warning"
1065
1198
  });
1199
+ throwIfAborted(o.signal);
1200
+ const keyOf = (name) => {
1201
+ const byKey = ds.columns.find((c) => c.key === name);
1202
+ if (byKey) return byKey.key;
1203
+ const byTitle = ds.columns.find((c) => c.title === name);
1204
+ if (byTitle) return byTitle.key;
1205
+ throw new TypeError(`types: no column with key or title ${JSON.stringify(name)}`);
1206
+ };
1207
+ const forced = /* @__PURE__ */ new Map();
1208
+ if (typeof o.types === "object" && o.types !== null) for (const name of Object.keys(o.types)) forced.set(keyOf(name), o.types[name]);
1209
+ const mapped = o.schema && o.preview === void 0 ? mapColumns(ds.columns, o.schema, o.mapping) : void 0;
1210
+ const report = issueSink(issueCap, mapped ? mappingPlan(ds.columns, mapped.mapping, o.schema).sources : void 0, data.length);
1066
1211
  if ((o.errorCells ?? "null") === "null") for (let i = 0; i < data.length; i++) {
1067
1212
  const row = data[i];
1068
1213
  for (const c of ds.columns) {
@@ -1070,28 +1215,18 @@ async function importDataset(input, options = {}) {
1070
1215
  if (!isError(v)) continue;
1071
1216
  row[c.key] = null;
1072
1217
  const ref = `${c.letter}${rowNumbers[i]}`;
1073
- issues.push({
1218
+ report({
1074
1219
  row: rowNumbers[i],
1075
1220
  field: c.key,
1076
1221
  column: c.key,
1077
1222
  ref,
1078
1223
  code: "ERROR_CELL",
1079
- message: `${ref} holds the error ${v.error}; read as empty.`,
1224
+ message: `${ref} holds the error ${require_read.excerpt(String(v.error))}; read as empty.`,
1080
1225
  severity: "warning"
1081
1226
  });
1082
1227
  }
1083
1228
  }
1084
1229
  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
1230
  if (mapped) for (const [field, spec] of Object.entries(o.schema.fields)) {
1096
1231
  const col = mapped.mapping[field];
1097
1232
  if (spec.type && col) forced.set(col, spec.type);
@@ -1121,21 +1256,20 @@ async function importDataset(input, options = {}) {
1121
1256
  } else {
1122
1257
  if (mode === "infer") plan[inf.key] = inf;
1123
1258
  typeOf.set(inf.key, inf.type);
1124
- if (inf.ambiguousDates) issues.push({
1259
+ if (inf.ambiguousDates) report({
1125
1260
  row: 0,
1126
1261
  field: inf.key,
1127
1262
  column: inf.key,
1128
1263
  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.`,
1264
+ message: `Column "${require_read.excerpt(inf.key)}" looks like dates, but the day/month order can't be told from the data; set locale.dateOrder.`,
1130
1265
  severity: "warning"
1131
1266
  });
1132
1267
  }
1133
1268
  }
1134
- const coerced = coerceColumns(dsLike, plan, {
1269
+ const coerced = { data: coerceInto(dsLike, plan, {
1135
1270
  locale,
1136
1271
  dateZone: zone
1137
- });
1138
- issues.push(...coerced.issues);
1272
+ }, report) };
1139
1273
  const typedColumns = ds.columns.map((c) => ({
1140
1274
  key: c.key,
1141
1275
  title: c.title,
@@ -1145,14 +1279,12 @@ async function importDataset(input, options = {}) {
1145
1279
  progress("dataset", .97);
1146
1280
  let result;
1147
1281
  if (mapped) {
1148
- const { sources, ...m } = applyMappingDetailed({
1282
+ const { sources: _sources, issues: _none, ...m } = applyMappingDetailed({
1149
1283
  columns: typedColumns,
1150
1284
  data: coerced.data,
1151
1285
  rowNumbers
1152
- }, mapped.mapping, o.schema);
1153
- issues.push(...m.issues);
1286
+ }, mapped.mapping, o.schema, report);
1154
1287
  result = m;
1155
- issues.splice(0, issues.length, ...renameIssues(issues, sources));
1156
1288
  meta.mapping = mapped.mapping;
1157
1289
  meta.unmappedColumns = mapped.unmapped;
1158
1290
  } else result = {
@@ -1161,6 +1293,7 @@ async function importDataset(input, options = {}) {
1161
1293
  rowNumbers
1162
1294
  };
1163
1295
  progress("dataset", 1);
1296
+ if (issueCap.dropped) meta.droppedIssues = issueCap.dropped;
1164
1297
  return {
1165
1298
  ...result,
1166
1299
  issues,