@jupyterlab/csvviewer 4.7.0-alpha.1 → 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 +94 -0
- package/lib/model.js +318 -15
- package/lib/model.js.map +1 -1
- package/lib/parse.d.ts +5 -1
- package/lib/parse.js +16 -8
- package/lib/parse.js.map +1 -1
- package/lib/toolbar.d.ts +26 -0
- package/lib/toolbar.js +85 -22
- package/lib/toolbar.js.map +1 -1
- package/lib/widget.d.ts +49 -0
- package/lib/widget.js +269 -25
- package/lib/widget.js.map +1 -1
- package/package.json +8 -8
- package/src/model.ts +371 -14
- package/src/parse.ts +24 -10
- package/src/toolbar.ts +103 -26
- package/src/widget.ts +320 -27
- package/style/base.css +8 -4
package/src/model.ts
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
// Distributed under the terms of the Modified BSD License.
|
|
3
3
|
|
|
4
4
|
import { PromiseDelegate } from '@lumino/coreutils';
|
|
5
|
+
import type { CellGroup } from '@lumino/datagrid';
|
|
6
|
+
// The viewer loads this model on demand; its base class must be available then.
|
|
7
|
+
// eslint-disable-next-line jupyter/prefer-lazy-imports
|
|
5
8
|
import { DataModel } from '@lumino/datagrid';
|
|
6
9
|
import type { IDisposable } from '@lumino/disposable';
|
|
7
10
|
import type { IParser } from './parse';
|
|
@@ -45,6 +48,7 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
45
48
|
let {
|
|
46
49
|
data,
|
|
47
50
|
delimiter = ',',
|
|
51
|
+
comment = undefined,
|
|
48
52
|
rowDelimiter = undefined,
|
|
49
53
|
quote = '"',
|
|
50
54
|
quoteParser = undefined,
|
|
@@ -53,9 +57,11 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
53
57
|
} = options;
|
|
54
58
|
this._rawData = data;
|
|
55
59
|
this._delimiter = delimiter;
|
|
60
|
+
this._comment = comment;
|
|
56
61
|
this._quote = quote;
|
|
57
62
|
this._quoteEscaped = new RegExp(quote + quote, 'g');
|
|
58
63
|
this._initialRows = initialRows;
|
|
64
|
+
this._hasHeader = header;
|
|
59
65
|
|
|
60
66
|
// Guess the row delimiter if it was not supplied. This will be fooled if a
|
|
61
67
|
// different line delimiter possibility appears in the first row.
|
|
@@ -81,10 +87,14 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
81
87
|
this.parseAsync();
|
|
82
88
|
|
|
83
89
|
// Cache the header row.
|
|
84
|
-
if (
|
|
90
|
+
if (
|
|
91
|
+
header === true &&
|
|
92
|
+
this._columnCount! > 0 &&
|
|
93
|
+
this._headerRow < this._rowCount!
|
|
94
|
+
) {
|
|
85
95
|
const h = [];
|
|
86
96
|
for (let c = 0; c < this._columnCount!; c++) {
|
|
87
|
-
h.push(this._getField(
|
|
97
|
+
h.push(this._getField(this._headerRow, c));
|
|
88
98
|
}
|
|
89
99
|
this._header = h;
|
|
90
100
|
}
|
|
@@ -132,6 +142,8 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
132
142
|
}
|
|
133
143
|
set header(value: string[]) {
|
|
134
144
|
this._header = value;
|
|
145
|
+
this._hasHeader = value.length > 0;
|
|
146
|
+
this._groups = [];
|
|
135
147
|
}
|
|
136
148
|
|
|
137
149
|
/**
|
|
@@ -164,10 +176,10 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
164
176
|
*/
|
|
165
177
|
rowCount(region: DataModel.RowRegion): number {
|
|
166
178
|
if (region === 'body') {
|
|
167
|
-
if (this.
|
|
179
|
+
if (this._hasHeader === false) {
|
|
168
180
|
return this._rowCount!;
|
|
169
181
|
} else {
|
|
170
|
-
return this._rowCount! - 1;
|
|
182
|
+
return Math.max(this._rowCount! - 1, 0);
|
|
171
183
|
}
|
|
172
184
|
}
|
|
173
185
|
return 1;
|
|
@@ -203,23 +215,28 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
203
215
|
|
|
204
216
|
// Look up the field and value for the region.
|
|
205
217
|
switch (region) {
|
|
206
|
-
case 'body':
|
|
207
|
-
|
|
208
|
-
|
|
218
|
+
case 'body': {
|
|
219
|
+
const rawRow = this._bodyRowToRawRow(row);
|
|
220
|
+
if (this._isCommentRow(rawRow) && !this._isSplitComment(rawRow)) {
|
|
221
|
+
// The first cell holds the whole line, merged across the row.
|
|
222
|
+
value = column === 0 ? this._getLine(rawRow) : '';
|
|
209
223
|
} else {
|
|
210
|
-
value = this._getField(
|
|
224
|
+
value = this._getField(rawRow, column);
|
|
211
225
|
}
|
|
212
226
|
break;
|
|
227
|
+
}
|
|
213
228
|
case 'column-header':
|
|
214
|
-
if (this.
|
|
229
|
+
if (this._hasHeader === false) {
|
|
215
230
|
value = (column + 1).toString();
|
|
216
231
|
} else {
|
|
217
232
|
value = this._header[column];
|
|
218
233
|
}
|
|
219
234
|
break;
|
|
220
|
-
case 'row-header':
|
|
221
|
-
|
|
235
|
+
case 'row-header': {
|
|
236
|
+
const rowNumber = this._rowNumber(this._bodyRowToRawRow(row));
|
|
237
|
+
value = rowNumber === null ? '' : rowNumber.toString();
|
|
222
238
|
break;
|
|
239
|
+
}
|
|
223
240
|
case 'corner-header':
|
|
224
241
|
value = '';
|
|
225
242
|
break;
|
|
@@ -231,6 +248,75 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
231
248
|
return value;
|
|
232
249
|
}
|
|
233
250
|
|
|
251
|
+
/**
|
|
252
|
+
* Get the count of merged cell groups: one per merged comment row.
|
|
253
|
+
*
|
|
254
|
+
* #### Notes
|
|
255
|
+
* The grid scans every group for each cell it paints, so only the first
|
|
256
|
+
* comment rows are merged.
|
|
257
|
+
*/
|
|
258
|
+
groupCount(region: DataModel.CellRegion): number {
|
|
259
|
+
if (region !== 'body' || this._columnCount! < 2) {
|
|
260
|
+
return 0;
|
|
261
|
+
}
|
|
262
|
+
return Math.min(this._getMergedRows().length, Private.MAX_MERGED_ROWS);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Get the merged cell group which spans a comment row.
|
|
267
|
+
*/
|
|
268
|
+
group(region: DataModel.CellRegion, groupIndex: number): CellGroup | null {
|
|
269
|
+
if (region !== 'body') {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
// The grid asks for every group once per painted cell, so reuse them.
|
|
273
|
+
const mergedRows = this._getMergedRows();
|
|
274
|
+
let group = this._groups[groupIndex];
|
|
275
|
+
if (group === undefined) {
|
|
276
|
+
const row = this._rawRowToBodyRow(mergedRows[groupIndex]);
|
|
277
|
+
const c2 = this._columnCount! - 1;
|
|
278
|
+
group = this._groups[groupIndex] = { r1: row, r2: row, c1: 0, c2 };
|
|
279
|
+
}
|
|
280
|
+
return group;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Get the metadata for a cell: `{ comment: true }` for comment rows, with
|
|
285
|
+
* `merged: true` when the row is merged across the columns.
|
|
286
|
+
*/
|
|
287
|
+
metadata(
|
|
288
|
+
region: DataModel.CellRegion,
|
|
289
|
+
row: number,
|
|
290
|
+
column: number
|
|
291
|
+
): DataModel.Metadata {
|
|
292
|
+
const rawRow = this._bodyRowToRawRow(row);
|
|
293
|
+
if (region !== 'body' || !this._isCommentRow(rawRow)) {
|
|
294
|
+
return DataModel.emptyMetadata;
|
|
295
|
+
}
|
|
296
|
+
return this._isSplitComment(rawRow)
|
|
297
|
+
? Private.COMMENT_METADATA
|
|
298
|
+
: Private.MERGED_COMMENT_METADATA;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Get the body row of a row number, as shown in the row header.
|
|
303
|
+
*
|
|
304
|
+
* #### Notes
|
|
305
|
+
* Rows above the first data row and comment rows have no number.
|
|
306
|
+
*/
|
|
307
|
+
bodyRowForNumber(rowNumber: number): number {
|
|
308
|
+
let row = this._firstNumberedRow() + rowNumber - 1;
|
|
309
|
+
for (const comment of this._commentRows) {
|
|
310
|
+
if (comment > row) {
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
if (comment >= this._firstNumberedRow()) {
|
|
314
|
+
row++;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return this._rawRowToBodyRow(row);
|
|
318
|
+
}
|
|
319
|
+
|
|
234
320
|
/**
|
|
235
321
|
* Dispose the resources held by this model.
|
|
236
322
|
*/
|
|
@@ -300,6 +386,7 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
300
386
|
delimiter: this._delimiter,
|
|
301
387
|
rowDelimiter: this._rowDelimiter,
|
|
302
388
|
quote: this._quote,
|
|
389
|
+
comment: this._comment,
|
|
303
390
|
columnOffsets: true,
|
|
304
391
|
maxRows: maxRows,
|
|
305
392
|
ncols: ncols,
|
|
@@ -402,6 +489,16 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
402
489
|
* parsed again.
|
|
403
490
|
*/
|
|
404
491
|
private _computeRowOffsets(endRow = 4294967295): void {
|
|
492
|
+
if (this._columnCount === undefined && this._comment) {
|
|
493
|
+
const firstDataRow = this._getFirstDataRow();
|
|
494
|
+
this._preferredColumnCount = firstDataRow.ncols;
|
|
495
|
+
this._headerRow = firstDataRow.row;
|
|
496
|
+
if (this._hasHeader) {
|
|
497
|
+
// The next row offset bounds the last header field.
|
|
498
|
+
endRow = Math.max(endRow, this._headerRow + 2);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
405
502
|
// If we've already parsed up to endRow, or if we've already parsed the
|
|
406
503
|
// entire data set, return early.
|
|
407
504
|
if (this._rowCount! >= endRow || this._doneParsing === true) {
|
|
@@ -419,6 +516,7 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
419
516
|
delimiter: this._delimiter,
|
|
420
517
|
rowDelimiter: this._rowDelimiter,
|
|
421
518
|
quote: this._quote,
|
|
519
|
+
comment: this._comment,
|
|
422
520
|
columnOffsets: false,
|
|
423
521
|
maxRows: endRow - this._rowCount! + reparse
|
|
424
522
|
});
|
|
@@ -440,7 +538,11 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
440
538
|
this._rowCount = oldRowCount + nrows - duplicateRows;
|
|
441
539
|
|
|
442
540
|
const oldColumnCount = this._columnCount ?? 0;
|
|
443
|
-
this._columnCount = Math.max(
|
|
541
|
+
this._columnCount = Math.max(
|
|
542
|
+
oldColumnCount,
|
|
543
|
+
maxNcols ?? 0,
|
|
544
|
+
this._preferredColumnCount
|
|
545
|
+
);
|
|
444
546
|
const columnCountChanged = this._columnCount !== oldColumnCount;
|
|
445
547
|
|
|
446
548
|
// If we didn't reach the requested row, we must be done.
|
|
@@ -457,6 +559,27 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
457
559
|
this._rowOffsets.set(offsets, oldRowCount - duplicateRows);
|
|
458
560
|
}
|
|
459
561
|
|
|
562
|
+
if (this._comment !== undefined) {
|
|
563
|
+
for (let row = oldRowCount; row < this._rowCount; row++) {
|
|
564
|
+
if (
|
|
565
|
+
this._rawData[this._rowOffsets[row]] === this._comment &&
|
|
566
|
+
!(this._hasHeader && row === this._headerRow)
|
|
567
|
+
) {
|
|
568
|
+
const fields = this._getLine(row).split(this._delimiter);
|
|
569
|
+
let longest = 0;
|
|
570
|
+
let text = false;
|
|
571
|
+
for (let i = 0; i < fields.length; i++) {
|
|
572
|
+
longest = Math.max(longest, fields[i].length);
|
|
573
|
+
text = text || (i > 0 && fields[i].trim() !== '');
|
|
574
|
+
}
|
|
575
|
+
this._commentRows.push(row);
|
|
576
|
+
// A line padded with empty fields is text, not a row of cells.
|
|
577
|
+
this._commentFields.push(text ? fields.length : 0);
|
|
578
|
+
this._commentLongest.push(longest);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
460
583
|
// Expand the column offsets array if needed
|
|
461
584
|
|
|
462
585
|
// If the full column offsets array is small enough, build a cache big
|
|
@@ -478,7 +601,7 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
478
601
|
|
|
479
602
|
if (this._header.length > 0) {
|
|
480
603
|
for (let c = this._header.length; c < this._columnCount; c++) {
|
|
481
|
-
this._header.push(this._getField(
|
|
604
|
+
this._header.push(this._getField(this._headerRow, c));
|
|
482
605
|
}
|
|
483
606
|
}
|
|
484
607
|
|
|
@@ -592,7 +715,10 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
592
715
|
}
|
|
593
716
|
|
|
594
717
|
// Check to see if the field begins with a quote. If it does, trim a quote on either side.
|
|
595
|
-
if (
|
|
718
|
+
if (
|
|
719
|
+
this._rawData[index] === this._quote &&
|
|
720
|
+
this._rawData[this._rowOffsets[row]] !== this._comment
|
|
721
|
+
) {
|
|
596
722
|
trimLeft += 1;
|
|
597
723
|
trimRight += 1;
|
|
598
724
|
}
|
|
@@ -609,6 +735,155 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
609
735
|
return value;
|
|
610
736
|
}
|
|
611
737
|
|
|
738
|
+
/**
|
|
739
|
+
* Find the first row that is neither a comment nor empty, and use it to
|
|
740
|
+
* determine the header row and column count.
|
|
741
|
+
*/
|
|
742
|
+
private _getFirstDataRow(): { row: number; ncols: number } {
|
|
743
|
+
const comment = this._comment;
|
|
744
|
+
const parser = PARSERS[this._parser];
|
|
745
|
+
|
|
746
|
+
let row = 0;
|
|
747
|
+
let startIndex = 0;
|
|
748
|
+
while (startIndex < this._rawData.length) {
|
|
749
|
+
if (
|
|
750
|
+
this._rawData[startIndex] !== comment &&
|
|
751
|
+
!this._rawData.startsWith(this._rowDelimiter, startIndex)
|
|
752
|
+
) {
|
|
753
|
+
const { ncols } = parser({
|
|
754
|
+
data: this._rawData,
|
|
755
|
+
startIndex,
|
|
756
|
+
delimiter: this._delimiter,
|
|
757
|
+
rowDelimiter: this._rowDelimiter,
|
|
758
|
+
quote: this._quote,
|
|
759
|
+
columnOffsets: true,
|
|
760
|
+
maxRows: 1
|
|
761
|
+
});
|
|
762
|
+
return { row, ncols };
|
|
763
|
+
}
|
|
764
|
+
const nextRow = this._rawData.indexOf(this._rowDelimiter, startIndex);
|
|
765
|
+
if (nextRow === -1) {
|
|
766
|
+
break;
|
|
767
|
+
}
|
|
768
|
+
startIndex = nextRow + this._rowDelimiter.length;
|
|
769
|
+
row++;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
const { ncols } = parser({
|
|
773
|
+
data: this._rawData,
|
|
774
|
+
delimiter: this._delimiter,
|
|
775
|
+
rowDelimiter: this._rowDelimiter,
|
|
776
|
+
quote: this._quote,
|
|
777
|
+
comment: this._comment,
|
|
778
|
+
columnOffsets: true,
|
|
779
|
+
maxRows: 1
|
|
780
|
+
});
|
|
781
|
+
return { row: 0, ncols };
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
/**
|
|
785
|
+
* Map a displayed body row to the underlying raw row, skipping the header.
|
|
786
|
+
*/
|
|
787
|
+
private _bodyRowToRawRow(row: number): number {
|
|
788
|
+
if (this._hasHeader === false) {
|
|
789
|
+
return row;
|
|
790
|
+
}
|
|
791
|
+
return row < this._headerRow ? row : row + 1;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/**
|
|
795
|
+
* Map a raw row to the displayed body row.
|
|
796
|
+
*/
|
|
797
|
+
private _rawRowToBodyRow(row: number): number {
|
|
798
|
+
if (this._hasHeader === false) {
|
|
799
|
+
return row;
|
|
800
|
+
}
|
|
801
|
+
return row < this._headerRow ? row : row - 1;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* The first raw row which gets a row number: the row after the header.
|
|
806
|
+
*/
|
|
807
|
+
private _firstNumberedRow(): number {
|
|
808
|
+
return this._hasHeader ? this._headerRow + 1 : this._headerRow;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* The number of a raw row among the data rows, or `null` for rows above the
|
|
813
|
+
* first data row and for comment rows.
|
|
814
|
+
*/
|
|
815
|
+
private _rowNumber(row: number): number | null {
|
|
816
|
+
const first = this._firstNumberedRow();
|
|
817
|
+
if (row < first || this._isCommentRow(row)) {
|
|
818
|
+
return null;
|
|
819
|
+
}
|
|
820
|
+
const comments =
|
|
821
|
+
Private.lowerBound(this._commentRows, row) -
|
|
822
|
+
Private.lowerBound(this._commentRows, first);
|
|
823
|
+
return row - first + 1 - comments;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* Whether a raw row starts with the comment character.
|
|
828
|
+
*/
|
|
829
|
+
private _isCommentRow(row: number): boolean {
|
|
830
|
+
return (
|
|
831
|
+
this._comment !== undefined &&
|
|
832
|
+
this._rawData[this._rowOffsets[row]] === this._comment
|
|
833
|
+
);
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
/**
|
|
837
|
+
* Whether a comment row is shown split into the columns: it has as many
|
|
838
|
+
* fields as the data, and in a file with two columns its fields are short.
|
|
839
|
+
*/
|
|
840
|
+
private _isSplitComment(row: number): boolean {
|
|
841
|
+
const index = Private.lowerBound(this._commentRows, row);
|
|
842
|
+
return this._commentRows[index] === row && this._isSplitCommentAt(index);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
/**
|
|
846
|
+
* Whether the comment row at an index of `_commentRows` is shown split.
|
|
847
|
+
*/
|
|
848
|
+
private _isSplitCommentAt(index: number): boolean {
|
|
849
|
+
const columns = this._columnCount!;
|
|
850
|
+
return (
|
|
851
|
+
this._commentFields[index] === columns &&
|
|
852
|
+
(columns > 2 ||
|
|
853
|
+
this._commentLongest[index] <= Private.MAX_TWO_COLUMN_FIELD)
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* The comment rows merged across the columns, for the current column count.
|
|
859
|
+
*/
|
|
860
|
+
private _getMergedRows(): number[] {
|
|
861
|
+
if (
|
|
862
|
+
this._mergedRowsColumns !== this._columnCount ||
|
|
863
|
+
this._mergedRowsComments !== this._commentRows.length
|
|
864
|
+
) {
|
|
865
|
+
this._mergedRows = this._commentRows.filter(
|
|
866
|
+
(row, index) => !this._isSplitCommentAt(index)
|
|
867
|
+
);
|
|
868
|
+
this._mergedRowsColumns = this._columnCount;
|
|
869
|
+
this._mergedRowsComments = this._commentRows.length;
|
|
870
|
+
this._groups = [];
|
|
871
|
+
}
|
|
872
|
+
return this._mergedRows;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Get the text of a raw row without the row delimiter.
|
|
877
|
+
*
|
|
878
|
+
* #### Notes
|
|
879
|
+
* Quotes in comment rows are literal, so the row ends at the next delimiter.
|
|
880
|
+
*/
|
|
881
|
+
private _getLine(row: number): string {
|
|
882
|
+
const start = this._rowOffsets[row];
|
|
883
|
+
const end = this._rawData.indexOf(this._rowDelimiter, start);
|
|
884
|
+
return this._rawData.slice(start, end === -1 ? undefined : end);
|
|
885
|
+
}
|
|
886
|
+
|
|
612
887
|
/**
|
|
613
888
|
* Reset the parser state.
|
|
614
889
|
*/
|
|
@@ -618,6 +893,13 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
618
893
|
this._rowOffsets = new Uint32Array(0);
|
|
619
894
|
this._rowCount = 0;
|
|
620
895
|
this._startedParsing = false;
|
|
896
|
+
this._commentRows = [];
|
|
897
|
+
this._commentFields = [];
|
|
898
|
+
this._commentLongest = [];
|
|
899
|
+
this._mergedRows = [];
|
|
900
|
+
this._mergedRowsColumns = undefined;
|
|
901
|
+
this._mergedRowsComments = 0;
|
|
902
|
+
this._groups = [];
|
|
621
903
|
|
|
622
904
|
this._columnOffsets = new Uint32Array(0);
|
|
623
905
|
|
|
@@ -642,6 +924,7 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
642
924
|
|
|
643
925
|
// Parser settings
|
|
644
926
|
private _delimiter: string;
|
|
927
|
+
private _comment: string | undefined;
|
|
645
928
|
private _quote: string;
|
|
646
929
|
private _quoteEscaped: RegExp;
|
|
647
930
|
private _parser: 'quotes' | 'noquotes';
|
|
@@ -651,12 +934,29 @@ export class DSVModel extends DataModel implements IDisposable {
|
|
|
651
934
|
private _rawData: string;
|
|
652
935
|
private _rowCount: number | undefined = 0;
|
|
653
936
|
private _columnCount: number | undefined;
|
|
937
|
+
private _preferredColumnCount = 0;
|
|
654
938
|
|
|
655
939
|
// Cache information
|
|
656
940
|
/**
|
|
657
941
|
* The header strings.
|
|
658
942
|
*/
|
|
659
943
|
private _header: string[] = [];
|
|
944
|
+
private _headerRow = 0;
|
|
945
|
+
private _hasHeader = true;
|
|
946
|
+
/**
|
|
947
|
+
* The raw rows which start with the comment character, in order.
|
|
948
|
+
*/
|
|
949
|
+
private _commentRows: number[] = [];
|
|
950
|
+
/**
|
|
951
|
+
* The field count of each comment row, or 0 when only empty fields follow
|
|
952
|
+
* the first one.
|
|
953
|
+
*/
|
|
954
|
+
private _commentFields: number[] = [];
|
|
955
|
+
private _commentLongest: number[] = [];
|
|
956
|
+
private _mergedRows: number[] = [];
|
|
957
|
+
private _mergedRowsColumns: number | undefined = undefined;
|
|
958
|
+
private _mergedRowsComments = 0;
|
|
959
|
+
private _groups: CellGroup[] = [];
|
|
660
960
|
/**
|
|
661
961
|
* The column offset cache, starting with row _columnOffsetsStartingRow
|
|
662
962
|
*
|
|
@@ -707,6 +1007,12 @@ export namespace DSVModel {
|
|
|
707
1007
|
*/
|
|
708
1008
|
delimiter: string;
|
|
709
1009
|
|
|
1010
|
+
/**
|
|
1011
|
+
* Optional comment character used to ignore header/column detection for
|
|
1012
|
+
* matching rows while still rendering those rows in the grid.
|
|
1013
|
+
*/
|
|
1014
|
+
comment?: string;
|
|
1015
|
+
|
|
710
1016
|
/**
|
|
711
1017
|
* The data source for the data model.
|
|
712
1018
|
*/
|
|
@@ -753,3 +1059,54 @@ export namespace DSVModel {
|
|
|
753
1059
|
initialRows?: number;
|
|
754
1060
|
}
|
|
755
1061
|
}
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* The namespace for module private data.
|
|
1065
|
+
*/
|
|
1066
|
+
namespace Private {
|
|
1067
|
+
/**
|
|
1068
|
+
* The metadata of the cells in a comment row.
|
|
1069
|
+
*/
|
|
1070
|
+
export const COMMENT_METADATA: DataModel.Metadata = Object.freeze({
|
|
1071
|
+
comment: true
|
|
1072
|
+
});
|
|
1073
|
+
|
|
1074
|
+
/**
|
|
1075
|
+
* The metadata of the cells in a comment row merged across the columns.
|
|
1076
|
+
*/
|
|
1077
|
+
export const MERGED_COMMENT_METADATA: DataModel.Metadata = Object.freeze({
|
|
1078
|
+
comment: true,
|
|
1079
|
+
merged: true
|
|
1080
|
+
});
|
|
1081
|
+
|
|
1082
|
+
/**
|
|
1083
|
+
* The maximum number of comment rows merged across the columns.
|
|
1084
|
+
*/
|
|
1085
|
+
export const MAX_MERGED_ROWS = 1000;
|
|
1086
|
+
|
|
1087
|
+
/**
|
|
1088
|
+
* The longest field of a comment line split in a file with two columns.
|
|
1089
|
+
*
|
|
1090
|
+
* #### Notes
|
|
1091
|
+
* Prose with one delimiter also has two fields; its fields are longer than
|
|
1092
|
+
* column names or units.
|
|
1093
|
+
*/
|
|
1094
|
+
export const MAX_TWO_COLUMN_FIELD = 30;
|
|
1095
|
+
|
|
1096
|
+
/**
|
|
1097
|
+
* The number of items in a sorted array which are less than a value.
|
|
1098
|
+
*/
|
|
1099
|
+
export function lowerBound(sorted: number[], value: number): number {
|
|
1100
|
+
let low = 0;
|
|
1101
|
+
let high = sorted.length;
|
|
1102
|
+
while (low < high) {
|
|
1103
|
+
const middle = (low + high) >>> 1;
|
|
1104
|
+
if (sorted[middle] < value) {
|
|
1105
|
+
low = middle + 1;
|
|
1106
|
+
} else {
|
|
1107
|
+
high = middle;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
return low;
|
|
1111
|
+
}
|
|
1112
|
+
}
|
package/src/parse.ts
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
Possible options to add to the parser:
|
|
6
6
|
|
|
7
7
|
- Optional offsets array to modify, so we don't need to create a new offsets list (we would need to be careful not to overwrite things if a row needs to be truncated.)
|
|
8
|
-
- Comment character at the start of the line
|
|
9
8
|
- Skip empty whitespace lines
|
|
10
9
|
- Skip rows with empty columns
|
|
11
10
|
- Logging an error for too many or too few fields on a line
|
|
@@ -55,6 +54,11 @@ export namespace IParser {
|
|
|
55
54
|
*/
|
|
56
55
|
delimiter?: string;
|
|
57
56
|
|
|
57
|
+
/**
|
|
58
|
+
* Optional comment character. Quotes in matching rows are treated literally.
|
|
59
|
+
*/
|
|
60
|
+
comment?: string;
|
|
61
|
+
|
|
58
62
|
/**
|
|
59
63
|
* The row delimiter to use. Defaults to '\r\n'.
|
|
60
64
|
*/
|
|
@@ -109,7 +113,7 @@ export namespace IParser {
|
|
|
109
113
|
ncols: number;
|
|
110
114
|
|
|
111
115
|
/**
|
|
112
|
-
* The maximum number of columns observed while parsing.
|
|
116
|
+
* The maximum number of columns observed in non-comment rows while parsing.
|
|
113
117
|
*/
|
|
114
118
|
maxNcols?: number;
|
|
115
119
|
/**
|
|
@@ -165,7 +169,8 @@ export function parseDSV(options: IParser.IOptions): IParser.IResults {
|
|
|
165
169
|
startIndex = 0,
|
|
166
170
|
maxRows = 0xffffffff,
|
|
167
171
|
rowDelimiter = '\r\n',
|
|
168
|
-
quote = '"'
|
|
172
|
+
quote = '"',
|
|
173
|
+
comment
|
|
169
174
|
} = options;
|
|
170
175
|
|
|
171
176
|
// ncols will be set automatically if it is undefined.
|
|
@@ -208,6 +213,7 @@ export function parseDSV(options: IParser.IOptions): IParser.IResults {
|
|
|
208
213
|
// We initialize to 0 just in case we are asked to parse past the end of the
|
|
209
214
|
// string. In that case, we want the number of columns to be 0.
|
|
210
215
|
let col = 0;
|
|
216
|
+
let isComment = false;
|
|
211
217
|
|
|
212
218
|
// Declare some useful temporaries
|
|
213
219
|
let char;
|
|
@@ -224,6 +230,7 @@ export function parseDSV(options: IParser.IOptions): IParser.IResults {
|
|
|
224
230
|
// Start a new row and reset the column counter.
|
|
225
231
|
offsets.push(i);
|
|
226
232
|
col = 1;
|
|
233
|
+
isComment = data[i] === comment;
|
|
227
234
|
}
|
|
228
235
|
|
|
229
236
|
// Below, we handle this character, modify the parser state and increment the index to be consistent.
|
|
@@ -243,7 +250,7 @@ export function parseDSV(options: IParser.IOptions): IParser.IResults {
|
|
|
243
250
|
switch (char) {
|
|
244
251
|
// If we have a quote, we are starting an escaped field.
|
|
245
252
|
case CH_QUOTE:
|
|
246
|
-
state = QUOTED_FIELD;
|
|
253
|
+
state = isComment ? UNQUOTED_FIELD : QUOTED_FIELD;
|
|
247
254
|
break;
|
|
248
255
|
|
|
249
256
|
// A field delimiter means we are starting a new field.
|
|
@@ -406,7 +413,9 @@ export function parseDSV(options: IParser.IOptions): IParser.IResults {
|
|
|
406
413
|
switch (state) {
|
|
407
414
|
case NEW_ROW:
|
|
408
415
|
nrows++;
|
|
409
|
-
|
|
416
|
+
if (!isComment) {
|
|
417
|
+
maxNcols = Math.max(maxNcols, col);
|
|
418
|
+
}
|
|
410
419
|
|
|
411
420
|
// If ncols is undefined, set it to the number of columns in this row (first row implied).
|
|
412
421
|
if (ncols === undefined) {
|
|
@@ -457,7 +466,9 @@ export function parseDSV(options: IParser.IOptions): IParser.IResults {
|
|
|
457
466
|
// defined.
|
|
458
467
|
if (state !== NEW_ROW) {
|
|
459
468
|
nrows++;
|
|
460
|
-
|
|
469
|
+
if (!isComment) {
|
|
470
|
+
maxNcols = Math.max(maxNcols, col);
|
|
471
|
+
}
|
|
461
472
|
if (columnOffsets === true) {
|
|
462
473
|
// If ncols is *still* undefined, then we only parsed one row and didn't
|
|
463
474
|
// have a newline, so set it to the number of columns we found.
|
|
@@ -467,9 +478,9 @@ export function parseDSV(options: IParser.IOptions): IParser.IResults {
|
|
|
467
478
|
|
|
468
479
|
if (col < ncols) {
|
|
469
480
|
// We didn't have enough columns, so add some more column offsets that
|
|
470
|
-
// point to
|
|
481
|
+
// point to the end of the data.
|
|
471
482
|
for (; col < ncols; col++) {
|
|
472
|
-
offsets.push(
|
|
483
|
+
offsets.push(endIndex);
|
|
473
484
|
}
|
|
474
485
|
} else if (col > ncols) {
|
|
475
486
|
// We had too many columns, so truncate them.
|
|
@@ -501,7 +512,8 @@ export function parseDSVNoQuotes(options: IParser.IOptions): IParser.IResults {
|
|
|
501
512
|
delimiter = ',',
|
|
502
513
|
rowDelimiter = '\r\n',
|
|
503
514
|
startIndex = 0,
|
|
504
|
-
maxRows = 0xffffffff
|
|
515
|
+
maxRows = 0xffffffff,
|
|
516
|
+
comment
|
|
505
517
|
} = options;
|
|
506
518
|
|
|
507
519
|
// ncols will be set automatically if it is undefined.
|
|
@@ -553,7 +565,9 @@ export function parseDSVNoQuotes(options: IParser.IOptions): IParser.IResults {
|
|
|
553
565
|
col++;
|
|
554
566
|
}
|
|
555
567
|
|
|
556
|
-
|
|
568
|
+
if (data[currRow] !== comment) {
|
|
569
|
+
maxNcols = Math.max(maxNcols, col);
|
|
570
|
+
}
|
|
557
571
|
|
|
558
572
|
if (ncols === undefined) {
|
|
559
573
|
// Set ncols to the number of fields we found.
|