@jupyterlab/csvviewer 4.7.0-alpha.2 → 4.7.0-beta.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.
package/lib/model.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { CellGroup } from '@lumino/datagrid';
1
2
  import { DataModel } from '@lumino/datagrid';
2
3
  import type { IDisposable } from '@lumino/disposable';
3
4
  /**
@@ -76,6 +77,30 @@ export declare class DSVModel extends DataModel implements IDisposable {
76
77
  * @returns - The data value for the specified cell.
77
78
  */
78
79
  data(region: DataModel.CellRegion, row: number, column: number): string;
80
+ /**
81
+ * Get the count of merged cell groups: one per merged comment row.
82
+ *
83
+ * #### Notes
84
+ * The grid scans every group for each cell it paints, so only the first
85
+ * comment rows are merged.
86
+ */
87
+ groupCount(region: DataModel.CellRegion): number;
88
+ /**
89
+ * Get the merged cell group which spans a comment row.
90
+ */
91
+ group(region: DataModel.CellRegion, groupIndex: number): CellGroup | null;
92
+ /**
93
+ * Get the metadata for a cell: `{ comment: true }` for comment rows, with
94
+ * `merged: true` when the row is merged across the columns.
95
+ */
96
+ metadata(region: DataModel.CellRegion, row: number, column: number): DataModel.Metadata;
97
+ /**
98
+ * Get the body row of a row number, as shown in the row header.
99
+ *
100
+ * #### Notes
101
+ * Rows above the first data row and comment rows have no number.
102
+ */
103
+ bodyRowForNumber(rowNumber: number): number;
79
104
  /**
80
105
  * Dispose the resources held by this model.
81
106
  */
@@ -118,11 +143,58 @@ export declare class DSVModel extends DataModel implements IDisposable {
118
143
  * @returns The parsed string for the data item.
119
144
  */
120
145
  private _getField;
146
+ /**
147
+ * Find the first row that is neither a comment nor empty, and use it to
148
+ * determine the header row and column count.
149
+ */
150
+ private _getFirstDataRow;
151
+ /**
152
+ * Map a displayed body row to the underlying raw row, skipping the header.
153
+ */
154
+ private _bodyRowToRawRow;
155
+ /**
156
+ * Map a raw row to the displayed body row.
157
+ */
158
+ private _rawRowToBodyRow;
159
+ /**
160
+ * The first raw row which gets a row number: the row after the header.
161
+ */
162
+ private _firstNumberedRow;
163
+ /**
164
+ * The number of a raw row among the data rows, or `null` for rows above the
165
+ * first data row and for comment rows.
166
+ */
167
+ private _rowNumber;
168
+ /**
169
+ * Whether a raw row starts with the comment character.
170
+ */
171
+ private _isCommentRow;
172
+ /**
173
+ * Whether a comment row is shown split into the columns: it has as many
174
+ * fields as the data, and in a file with two columns its fields are short.
175
+ */
176
+ private _isSplitComment;
177
+ /**
178
+ * Whether the comment row at an index of `_commentRows` is shown split.
179
+ */
180
+ private _isSplitCommentAt;
181
+ /**
182
+ * The comment rows merged across the columns, for the current column count.
183
+ */
184
+ private _getMergedRows;
185
+ /**
186
+ * Get the text of a raw row without the row delimiter.
187
+ *
188
+ * #### Notes
189
+ * Quotes in comment rows are literal, so the row ends at the next delimiter.
190
+ */
191
+ private _getLine;
121
192
  /**
122
193
  * Reset the parser state.
123
194
  */
124
195
  private _resetParser;
125
196
  private _delimiter;
197
+ private _comment;
126
198
  private _quote;
127
199
  private _quoteEscaped;
128
200
  private _parser;
@@ -130,10 +202,27 @@ export declare class DSVModel extends DataModel implements IDisposable {
130
202
  private _rawData;
131
203
  private _rowCount;
132
204
  private _columnCount;
205
+ private _preferredColumnCount;
133
206
  /**
134
207
  * The header strings.
135
208
  */
136
209
  private _header;
210
+ private _headerRow;
211
+ private _hasHeader;
212
+ /**
213
+ * The raw rows which start with the comment character, in order.
214
+ */
215
+ private _commentRows;
216
+ /**
217
+ * The field count of each comment row, or 0 when only empty fields follow
218
+ * the first one.
219
+ */
220
+ private _commentFields;
221
+ private _commentLongest;
222
+ private _mergedRows;
223
+ private _mergedRowsColumns;
224
+ private _mergedRowsComments;
225
+ private _groups;
137
226
  /**
138
227
  * The column offset cache, starting with row _columnOffsetsStartingRow
139
228
  *
@@ -180,6 +269,11 @@ export declare namespace DSVModel {
180
269
  * The field delimiter must be a single character.
181
270
  */
182
271
  delimiter: string;
272
+ /**
273
+ * Optional comment character used to ignore header/column detection for
274
+ * matching rows while still rendering those rows in the grid.
275
+ */
276
+ comment?: string;
183
277
  /**
184
278
  * The data source for the data model.
185
279
  */
package/lib/model.js CHANGED
@@ -1,6 +1,8 @@
1
1
  // Copyright (c) Jupyter Development Team.
2
2
  // Distributed under the terms of the Modified BSD License.
3
3
  import { PromiseDelegate } from '@lumino/coreutils';
4
+ // The viewer loads this model on demand; its base class must be available then.
5
+ // eslint-disable-next-line jupyter/prefer-lazy-imports
4
6
  import { DataModel } from '@lumino/datagrid';
5
7
  import { parseDSV, parseDSVNoQuotes } from './parse';
6
8
  /*
@@ -37,11 +39,28 @@ export class DSVModel extends DataModel {
37
39
  constructor(options) {
38
40
  super();
39
41
  this._rowCount = 0;
42
+ this._preferredColumnCount = 0;
40
43
  // Cache information
41
44
  /**
42
45
  * The header strings.
43
46
  */
44
47
  this._header = [];
48
+ this._headerRow = 0;
49
+ this._hasHeader = true;
50
+ /**
51
+ * The raw rows which start with the comment character, in order.
52
+ */
53
+ this._commentRows = [];
54
+ /**
55
+ * The field count of each comment row, or 0 when only empty fields follow
56
+ * the first one.
57
+ */
58
+ this._commentFields = [];
59
+ this._commentLongest = [];
60
+ this._mergedRows = [];
61
+ this._mergedRowsColumns = undefined;
62
+ this._mergedRowsComments = 0;
63
+ this._groups = [];
45
64
  /**
46
65
  * The column offset cache, starting with row _columnOffsetsStartingRow
47
66
  *
@@ -68,12 +87,14 @@ export class DSVModel extends DataModel {
68
87
  this._doneParsing = false;
69
88
  this._isDisposed = false;
70
89
  this._ready = new PromiseDelegate();
71
- let { data, delimiter = ',', rowDelimiter = undefined, quote = '"', quoteParser = undefined, header = true, initialRows = 500 } = options;
90
+ let { data, delimiter = ',', comment = undefined, rowDelimiter = undefined, quote = '"', quoteParser = undefined, header = true, initialRows = 500 } = options;
72
91
  this._rawData = data;
73
92
  this._delimiter = delimiter;
93
+ this._comment = comment;
74
94
  this._quote = quote;
75
95
  this._quoteEscaped = new RegExp(quote + quote, 'g');
76
96
  this._initialRows = initialRows;
97
+ this._hasHeader = header;
77
98
  // Guess the row delimiter if it was not supplied. This will be fooled if a
78
99
  // different line delimiter possibility appears in the first row.
79
100
  if (rowDelimiter === undefined) {
@@ -97,10 +118,12 @@ export class DSVModel extends DataModel {
97
118
  // Parse the data.
98
119
  this.parseAsync();
99
120
  // Cache the header row.
100
- if (header === true && this._columnCount > 0) {
121
+ if (header === true &&
122
+ this._columnCount > 0 &&
123
+ this._headerRow < this._rowCount) {
101
124
  const h = [];
102
125
  for (let c = 0; c < this._columnCount; c++) {
103
- h.push(this._getField(0, c));
126
+ h.push(this._getField(this._headerRow, c));
104
127
  }
105
128
  this._header = h;
106
129
  }
@@ -143,6 +166,8 @@ export class DSVModel extends DataModel {
143
166
  }
144
167
  set header(value) {
145
168
  this._header = value;
169
+ this._hasHeader = value.length > 0;
170
+ this._groups = [];
146
171
  }
147
172
  /**
148
173
  * The delimiter between entries on the same row.
@@ -171,11 +196,11 @@ export class DSVModel extends DataModel {
171
196
  */
172
197
  rowCount(region) {
173
198
  if (region === 'body') {
174
- if (this._header.length === 0) {
199
+ if (this._hasHeader === false) {
175
200
  return this._rowCount;
176
201
  }
177
202
  else {
178
- return this._rowCount - 1;
203
+ return Math.max(this._rowCount - 1, 0);
179
204
  }
180
205
  }
181
206
  return 1;
@@ -208,25 +233,30 @@ export class DSVModel extends DataModel {
208
233
  let value;
209
234
  // Look up the field and value for the region.
210
235
  switch (region) {
211
- case 'body':
212
- if (this._header.length === 0) {
213
- value = this._getField(row, column);
236
+ case 'body': {
237
+ const rawRow = this._bodyRowToRawRow(row);
238
+ if (this._isCommentRow(rawRow) && !this._isSplitComment(rawRow)) {
239
+ // The first cell holds the whole line, merged across the row.
240
+ value = column === 0 ? this._getLine(rawRow) : '';
214
241
  }
215
242
  else {
216
- value = this._getField(row + 1, column);
243
+ value = this._getField(rawRow, column);
217
244
  }
218
245
  break;
246
+ }
219
247
  case 'column-header':
220
- if (this._header.length === 0) {
248
+ if (this._hasHeader === false) {
221
249
  value = (column + 1).toString();
222
250
  }
223
251
  else {
224
252
  value = this._header[column];
225
253
  }
226
254
  break;
227
- case 'row-header':
228
- value = (row + 1).toString();
255
+ case 'row-header': {
256
+ const rowNumber = this._rowNumber(this._bodyRowToRawRow(row));
257
+ value = rowNumber === null ? '' : rowNumber.toString();
229
258
  break;
259
+ }
230
260
  case 'corner-header':
231
261
  value = '';
232
262
  break;
@@ -236,6 +266,67 @@ export class DSVModel extends DataModel {
236
266
  // Return the final value.
237
267
  return value;
238
268
  }
269
+ /**
270
+ * Get the count of merged cell groups: one per merged comment row.
271
+ *
272
+ * #### Notes
273
+ * The grid scans every group for each cell it paints, so only the first
274
+ * comment rows are merged.
275
+ */
276
+ groupCount(region) {
277
+ if (region !== 'body' || this._columnCount < 2) {
278
+ return 0;
279
+ }
280
+ return Math.min(this._getMergedRows().length, Private.MAX_MERGED_ROWS);
281
+ }
282
+ /**
283
+ * Get the merged cell group which spans a comment row.
284
+ */
285
+ group(region, groupIndex) {
286
+ if (region !== 'body') {
287
+ return null;
288
+ }
289
+ // The grid asks for every group once per painted cell, so reuse them.
290
+ const mergedRows = this._getMergedRows();
291
+ let group = this._groups[groupIndex];
292
+ if (group === undefined) {
293
+ const row = this._rawRowToBodyRow(mergedRows[groupIndex]);
294
+ const c2 = this._columnCount - 1;
295
+ group = this._groups[groupIndex] = { r1: row, r2: row, c1: 0, c2 };
296
+ }
297
+ return group;
298
+ }
299
+ /**
300
+ * Get the metadata for a cell: `{ comment: true }` for comment rows, with
301
+ * `merged: true` when the row is merged across the columns.
302
+ */
303
+ metadata(region, row, column) {
304
+ const rawRow = this._bodyRowToRawRow(row);
305
+ if (region !== 'body' || !this._isCommentRow(rawRow)) {
306
+ return DataModel.emptyMetadata;
307
+ }
308
+ return this._isSplitComment(rawRow)
309
+ ? Private.COMMENT_METADATA
310
+ : Private.MERGED_COMMENT_METADATA;
311
+ }
312
+ /**
313
+ * Get the body row of a row number, as shown in the row header.
314
+ *
315
+ * #### Notes
316
+ * Rows above the first data row and comment rows have no number.
317
+ */
318
+ bodyRowForNumber(rowNumber) {
319
+ let row = this._firstNumberedRow() + rowNumber - 1;
320
+ for (const comment of this._commentRows) {
321
+ if (comment > row) {
322
+ break;
323
+ }
324
+ if (comment >= this._firstNumberedRow()) {
325
+ row++;
326
+ }
327
+ }
328
+ return this._rawRowToBodyRow(row);
329
+ }
239
330
  /**
240
331
  * Dispose the resources held by this model.
241
332
  */
@@ -296,6 +387,7 @@ export class DSVModel extends DataModel {
296
387
  delimiter: this._delimiter,
297
388
  rowDelimiter: this._rowDelimiter,
298
389
  quote: this._quote,
390
+ comment: this._comment,
299
391
  columnOffsets: true,
300
392
  maxRows: maxRows,
301
393
  ncols: ncols,
@@ -388,6 +480,15 @@ export class DSVModel extends DataModel {
388
480
  */
389
481
  _computeRowOffsets(endRow = 4294967295) {
390
482
  var _a, _b;
483
+ if (this._columnCount === undefined && this._comment) {
484
+ const firstDataRow = this._getFirstDataRow();
485
+ this._preferredColumnCount = firstDataRow.ncols;
486
+ this._headerRow = firstDataRow.row;
487
+ if (this._hasHeader) {
488
+ // The next row offset bounds the last header field.
489
+ endRow = Math.max(endRow, this._headerRow + 2);
490
+ }
491
+ }
391
492
  // If we've already parsed up to endRow, or if we've already parsed the
392
493
  // entire data set, return early.
393
494
  if (this._rowCount >= endRow || this._doneParsing === true) {
@@ -404,6 +505,7 @@ export class DSVModel extends DataModel {
404
505
  delimiter: this._delimiter,
405
506
  rowDelimiter: this._rowDelimiter,
406
507
  quote: this._quote,
508
+ comment: this._comment,
407
509
  columnOffsets: false,
408
510
  maxRows: endRow - this._rowCount + reparse
409
511
  });
@@ -421,7 +523,7 @@ export class DSVModel extends DataModel {
421
523
  const duplicateRows = Math.min(nrows, reparse);
422
524
  this._rowCount = oldRowCount + nrows - duplicateRows;
423
525
  const oldColumnCount = (_b = this._columnCount) !== null && _b !== void 0 ? _b : 0;
424
- this._columnCount = Math.max(oldColumnCount, maxNcols !== null && maxNcols !== void 0 ? maxNcols : 0);
526
+ this._columnCount = Math.max(oldColumnCount, maxNcols !== null && maxNcols !== void 0 ? maxNcols : 0, this._preferredColumnCount);
425
527
  const columnCountChanged = this._columnCount !== oldColumnCount;
426
528
  // If we didn't reach the requested row, we must be done.
427
529
  if (this._rowCount < endRow) {
@@ -435,6 +537,24 @@ export class DSVModel extends DataModel {
435
537
  this._rowOffsets.set(oldRowOffsets);
436
538
  this._rowOffsets.set(offsets, oldRowCount - duplicateRows);
437
539
  }
540
+ if (this._comment !== undefined) {
541
+ for (let row = oldRowCount; row < this._rowCount; row++) {
542
+ if (this._rawData[this._rowOffsets[row]] === this._comment &&
543
+ !(this._hasHeader && row === this._headerRow)) {
544
+ const fields = this._getLine(row).split(this._delimiter);
545
+ let longest = 0;
546
+ let text = false;
547
+ for (let i = 0; i < fields.length; i++) {
548
+ longest = Math.max(longest, fields[i].length);
549
+ text = text || (i > 0 && fields[i].trim() !== '');
550
+ }
551
+ this._commentRows.push(row);
552
+ // A line padded with empty fields is text, not a row of cells.
553
+ this._commentFields.push(text ? fields.length : 0);
554
+ this._commentLongest.push(longest);
555
+ }
556
+ }
557
+ }
438
558
  // Expand the column offsets array if needed
439
559
  // If the full column offsets array is small enough, build a cache big
440
560
  // enough for all column offsets. We allocate up to 128 megabytes:
@@ -449,7 +569,7 @@ export class DSVModel extends DataModel {
449
569
  this._columnOffsetsStartingRow = 0;
450
570
  if (this._header.length > 0) {
451
571
  for (let c = this._header.length; c < this._columnCount; c++) {
452
- this._header.push(this._getField(0, c));
572
+ this._header.push(this._getField(this._headerRow, c));
453
573
  }
454
574
  }
455
575
  if (oldColumnCount > 0) {
@@ -545,7 +665,8 @@ export class DSVModel extends DataModel {
545
665
  }
546
666
  }
547
667
  // Check to see if the field begins with a quote. If it does, trim a quote on either side.
548
- if (this._rawData[index] === this._quote) {
668
+ if (this._rawData[index] === this._quote &&
669
+ this._rawData[this._rowOffsets[row]] !== this._comment) {
549
670
  trimLeft += 1;
550
671
  trimRight += 1;
551
672
  }
@@ -558,6 +679,132 @@ export class DSVModel extends DataModel {
558
679
  // Return the value.
559
680
  return value;
560
681
  }
682
+ /**
683
+ * Find the first row that is neither a comment nor empty, and use it to
684
+ * determine the header row and column count.
685
+ */
686
+ _getFirstDataRow() {
687
+ const comment = this._comment;
688
+ const parser = PARSERS[this._parser];
689
+ let row = 0;
690
+ let startIndex = 0;
691
+ while (startIndex < this._rawData.length) {
692
+ if (this._rawData[startIndex] !== comment &&
693
+ !this._rawData.startsWith(this._rowDelimiter, startIndex)) {
694
+ const { ncols } = parser({
695
+ data: this._rawData,
696
+ startIndex,
697
+ delimiter: this._delimiter,
698
+ rowDelimiter: this._rowDelimiter,
699
+ quote: this._quote,
700
+ columnOffsets: true,
701
+ maxRows: 1
702
+ });
703
+ return { row, ncols };
704
+ }
705
+ const nextRow = this._rawData.indexOf(this._rowDelimiter, startIndex);
706
+ if (nextRow === -1) {
707
+ break;
708
+ }
709
+ startIndex = nextRow + this._rowDelimiter.length;
710
+ row++;
711
+ }
712
+ const { ncols } = parser({
713
+ data: this._rawData,
714
+ delimiter: this._delimiter,
715
+ rowDelimiter: this._rowDelimiter,
716
+ quote: this._quote,
717
+ comment: this._comment,
718
+ columnOffsets: true,
719
+ maxRows: 1
720
+ });
721
+ return { row: 0, ncols };
722
+ }
723
+ /**
724
+ * Map a displayed body row to the underlying raw row, skipping the header.
725
+ */
726
+ _bodyRowToRawRow(row) {
727
+ if (this._hasHeader === false) {
728
+ return row;
729
+ }
730
+ return row < this._headerRow ? row : row + 1;
731
+ }
732
+ /**
733
+ * Map a raw row to the displayed body row.
734
+ */
735
+ _rawRowToBodyRow(row) {
736
+ if (this._hasHeader === false) {
737
+ return row;
738
+ }
739
+ return row < this._headerRow ? row : row - 1;
740
+ }
741
+ /**
742
+ * The first raw row which gets a row number: the row after the header.
743
+ */
744
+ _firstNumberedRow() {
745
+ return this._hasHeader ? this._headerRow + 1 : this._headerRow;
746
+ }
747
+ /**
748
+ * The number of a raw row among the data rows, or `null` for rows above the
749
+ * first data row and for comment rows.
750
+ */
751
+ _rowNumber(row) {
752
+ const first = this._firstNumberedRow();
753
+ if (row < first || this._isCommentRow(row)) {
754
+ return null;
755
+ }
756
+ const comments = Private.lowerBound(this._commentRows, row) -
757
+ Private.lowerBound(this._commentRows, first);
758
+ return row - first + 1 - comments;
759
+ }
760
+ /**
761
+ * Whether a raw row starts with the comment character.
762
+ */
763
+ _isCommentRow(row) {
764
+ return (this._comment !== undefined &&
765
+ this._rawData[this._rowOffsets[row]] === this._comment);
766
+ }
767
+ /**
768
+ * Whether a comment row is shown split into the columns: it has as many
769
+ * fields as the data, and in a file with two columns its fields are short.
770
+ */
771
+ _isSplitComment(row) {
772
+ const index = Private.lowerBound(this._commentRows, row);
773
+ return this._commentRows[index] === row && this._isSplitCommentAt(index);
774
+ }
775
+ /**
776
+ * Whether the comment row at an index of `_commentRows` is shown split.
777
+ */
778
+ _isSplitCommentAt(index) {
779
+ const columns = this._columnCount;
780
+ return (this._commentFields[index] === columns &&
781
+ (columns > 2 ||
782
+ this._commentLongest[index] <= Private.MAX_TWO_COLUMN_FIELD));
783
+ }
784
+ /**
785
+ * The comment rows merged across the columns, for the current column count.
786
+ */
787
+ _getMergedRows() {
788
+ if (this._mergedRowsColumns !== this._columnCount ||
789
+ this._mergedRowsComments !== this._commentRows.length) {
790
+ this._mergedRows = this._commentRows.filter((row, index) => !this._isSplitCommentAt(index));
791
+ this._mergedRowsColumns = this._columnCount;
792
+ this._mergedRowsComments = this._commentRows.length;
793
+ this._groups = [];
794
+ }
795
+ return this._mergedRows;
796
+ }
797
+ /**
798
+ * Get the text of a raw row without the row delimiter.
799
+ *
800
+ * #### Notes
801
+ * Quotes in comment rows are literal, so the row ends at the next delimiter.
802
+ */
803
+ _getLine(row) {
804
+ const start = this._rowOffsets[row];
805
+ const end = this._rawData.indexOf(this._rowDelimiter, start);
806
+ return this._rawData.slice(start, end === -1 ? undefined : end);
807
+ }
561
808
  /**
562
809
  * Reset the parser state.
563
810
  */
@@ -566,6 +813,13 @@ export class DSVModel extends DataModel {
566
813
  this._rowOffsets = new Uint32Array(0);
567
814
  this._rowCount = 0;
568
815
  this._startedParsing = false;
816
+ this._commentRows = [];
817
+ this._commentFields = [];
818
+ this._commentLongest = [];
819
+ this._mergedRows = [];
820
+ this._mergedRowsColumns = undefined;
821
+ this._mergedRowsComments = 0;
822
+ this._groups = [];
569
823
  this._columnOffsets = new Uint32Array(0);
570
824
  // Clear out state associated with the asynchronous parsing.
571
825
  if (this._doneParsing === false) {
@@ -585,4 +839,53 @@ export class DSVModel extends DataModel {
585
839
  this.emitChanged({ type: 'model-reset' });
586
840
  }
587
841
  }
842
+ /**
843
+ * The namespace for module private data.
844
+ */
845
+ var Private;
846
+ (function (Private) {
847
+ /**
848
+ * The metadata of the cells in a comment row.
849
+ */
850
+ Private.COMMENT_METADATA = Object.freeze({
851
+ comment: true
852
+ });
853
+ /**
854
+ * The metadata of the cells in a comment row merged across the columns.
855
+ */
856
+ Private.MERGED_COMMENT_METADATA = Object.freeze({
857
+ comment: true,
858
+ merged: true
859
+ });
860
+ /**
861
+ * The maximum number of comment rows merged across the columns.
862
+ */
863
+ Private.MAX_MERGED_ROWS = 1000;
864
+ /**
865
+ * The longest field of a comment line split in a file with two columns.
866
+ *
867
+ * #### Notes
868
+ * Prose with one delimiter also has two fields; its fields are longer than
869
+ * column names or units.
870
+ */
871
+ Private.MAX_TWO_COLUMN_FIELD = 30;
872
+ /**
873
+ * The number of items in a sorted array which are less than a value.
874
+ */
875
+ function lowerBound(sorted, value) {
876
+ let low = 0;
877
+ let high = sorted.length;
878
+ while (low < high) {
879
+ const middle = (low + high) >>> 1;
880
+ if (sorted[middle] < value) {
881
+ low = middle + 1;
882
+ }
883
+ else {
884
+ high = middle;
885
+ }
886
+ }
887
+ return low;
888
+ }
889
+ Private.lowerBound = lowerBound;
890
+ })(Private || (Private = {}));
588
891
  //# sourceMappingURL=model.js.map