@loaders.gl/csv 4.3.2 → 4.4.0-alpha.2
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/dist/csv-arrow-loader.d.ts +37 -0
- package/dist/csv-arrow-loader.d.ts.map +1 -0
- package/dist/csv-arrow-loader.js +23 -0
- package/dist/csv-format.d.ts +10 -0
- package/dist/csv-format.d.ts.map +1 -0
- package/dist/csv-format.js +12 -0
- package/dist/csv-loader.d.ts +6 -6
- package/dist/csv-loader.d.ts.map +1 -1
- package/dist/csv-loader.js +53 -20
- package/dist/csv-writer.d.ts +6 -5
- package/dist/csv-writer.d.ts.map +1 -1
- package/dist/csv-writer.js +2 -5
- package/dist/dist.dev.js +13318 -449
- package/dist/dist.min.js +23 -20
- package/dist/index.cjs +317 -262
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/lib/encoders/encode-csv.d.ts +1 -1
- package/dist/lib/encoders/encode-csv.d.ts.map +1 -1
- package/dist/lib/encoders/encode-csv.js +1 -1
- package/dist/papaparse/async-iterator-streamer.d.ts +1 -21
- package/dist/papaparse/async-iterator-streamer.d.ts.map +1 -1
- package/dist/papaparse/async-iterator-streamer.js +6 -6
- package/dist/papaparse/papa-constants.d.ts +12 -0
- package/dist/papaparse/papa-constants.d.ts.map +1 -0
- package/dist/papaparse/papa-constants.js +19 -0
- package/dist/papaparse/papa-parser.d.ts +110 -0
- package/dist/papaparse/papa-parser.d.ts.map +1 -0
- package/dist/papaparse/papa-parser.js +733 -0
- package/dist/papaparse/papa-writer.d.ts +22 -0
- package/dist/papaparse/papa-writer.d.ts.map +1 -0
- package/dist/papaparse/papa-writer.js +166 -0
- package/dist/papaparse/papaparse.d.ts +9 -113
- package/dist/papaparse/papaparse.d.ts.map +1 -1
- package/dist/papaparse/papaparse.js +13 -882
- package/package.json +5 -5
- package/src/csv-arrow-loader.ts +41 -0
- package/src/csv-format.ts +15 -0
- package/src/csv-loader.ts +58 -25
- package/src/csv-writer.ts +2 -5
- package/src/index.ts +3 -0
- package/src/lib/encoders/encode-csv.ts +2 -1
- package/src/papaparse/async-iterator-streamer.ts +6 -6
- package/src/papaparse/papa-constants.ts +23 -0
- package/src/papaparse/papa-parser.ts +872 -0
- package/src/papaparse/papa-writer.ts +219 -0
- package/src/papaparse/papaparse.ts +17 -1048
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type CSVWriterConfig = {
|
|
2
|
+
chunk?: boolean;
|
|
3
|
+
chunkSize?: number | null;
|
|
4
|
+
preview?: number;
|
|
5
|
+
newline?: string;
|
|
6
|
+
comments?: boolean;
|
|
7
|
+
skipEmptyLines?: boolean | 'greedy';
|
|
8
|
+
delimitersToGuess?: string[];
|
|
9
|
+
quotes?: string[] | boolean;
|
|
10
|
+
quoteChar?: string;
|
|
11
|
+
escapeChar?: string;
|
|
12
|
+
delimiter?: string;
|
|
13
|
+
fastMode?: boolean;
|
|
14
|
+
dynamicTyping?: boolean | {};
|
|
15
|
+
dynamicTypingFunction?: Function;
|
|
16
|
+
step?: Function;
|
|
17
|
+
transform?: Function;
|
|
18
|
+
header?: any;
|
|
19
|
+
columns?: any;
|
|
20
|
+
};
|
|
21
|
+
export declare function JsonToCsv(_input: any, _config?: CSVWriterConfig): string;
|
|
22
|
+
//# sourceMappingURL=papa-writer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"papa-writer.d.ts","sourceRoot":"","sources":["../../src/papaparse/papa-writer.ts"],"names":[],"mappings":"AAUA,MAAM,MAAM,eAAe,GAAG;IAC5B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,cAAc,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;IACpC,iBAAiB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAC5B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB,aAAa,CAAC,EAAE,OAAO,GAAG,EAAE,CAAC;IAC7B,qBAAqB,CAAC,EAAE,QAAQ,CAAC;IACjC,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,SAAS,CAAC,EAAE,QAAQ,CAAC;IAErB,MAAM,CAAC,EAAE,GAAG,CAAC;IACb,OAAO,CAAC,EAAE,GAAG,CAAC;CACf,CAAC;AAGF,wBAAgB,SAAS,CAAC,MAAM,KAAA,EAAE,OAAO,GAAE,eAAoB,UAkL9D"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// loaders.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
// Copyright (c) 2015 Matthew Holt
|
|
5
|
+
// This is a fork of papaparse v5.0.0-beta.0 under MIT license
|
|
6
|
+
// https://github.com/mholt/PapaParse
|
|
7
|
+
import { Papa } from "./papa-constants.js";
|
|
8
|
+
// eslint-disable-next-line complexity, max-statements
|
|
9
|
+
export function JsonToCsv(_input, _config = {}) {
|
|
10
|
+
// Default configuration
|
|
11
|
+
/** whether to surround every datum with quotes */
|
|
12
|
+
let _quotes = false;
|
|
13
|
+
/** whether to write headers */
|
|
14
|
+
let _writeHeader = true;
|
|
15
|
+
/** delimiting character(s) */
|
|
16
|
+
let _delimiter = ',';
|
|
17
|
+
/** newline character(s) */
|
|
18
|
+
let _newline = '\r\n';
|
|
19
|
+
/** quote character */
|
|
20
|
+
let _quoteChar = '"';
|
|
21
|
+
/** escaped quote character, either "" or <config.escapeChar>" */
|
|
22
|
+
let _escapedQuote = _quoteChar + _quoteChar;
|
|
23
|
+
/** whether to skip empty lines */
|
|
24
|
+
let _skipEmptyLines = false;
|
|
25
|
+
/** the columns (keys) we expect when we unparse objects */
|
|
26
|
+
let _columns = null;
|
|
27
|
+
unpackConfig();
|
|
28
|
+
const quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g');
|
|
29
|
+
if (typeof _input === 'string')
|
|
30
|
+
_input = JSON.parse(_input);
|
|
31
|
+
if (Array.isArray(_input)) {
|
|
32
|
+
if (!_input.length || Array.isArray(_input[0])) {
|
|
33
|
+
return serialize(null, _input, _skipEmptyLines);
|
|
34
|
+
}
|
|
35
|
+
else if (typeof _input[0] === 'object') {
|
|
36
|
+
return serialize(_columns || Object.keys(_input[0]), _input, _skipEmptyLines);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else if (typeof _input === 'object') {
|
|
40
|
+
if (typeof _input.data === 'string') {
|
|
41
|
+
_input.data = JSON.parse(_input.data);
|
|
42
|
+
}
|
|
43
|
+
if (Array.isArray(_input.data)) {
|
|
44
|
+
if (!_input.fields) {
|
|
45
|
+
_input.fields = _input.meta && _input.meta.fields;
|
|
46
|
+
}
|
|
47
|
+
if (!_input.fields) {
|
|
48
|
+
_input.fields = Array.isArray(_input.data[0]) ? _input.fields : Object.keys(_input.data[0]);
|
|
49
|
+
}
|
|
50
|
+
if (!Array.isArray(_input.data[0]) && typeof _input.data[0] !== 'object') {
|
|
51
|
+
_input.data = [_input.data]; // handles input like [1,2,3] or ['asdf']
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return serialize(_input.fields || [], _input.data || [], _skipEmptyLines);
|
|
55
|
+
}
|
|
56
|
+
// Default (any valid paths should return before this)
|
|
57
|
+
throw new Error('Unable to serialize unrecognized input');
|
|
58
|
+
// eslint-disable-next-line complexity
|
|
59
|
+
function unpackConfig() {
|
|
60
|
+
if (typeof _config !== 'object')
|
|
61
|
+
return;
|
|
62
|
+
if (typeof _config.delimiter === 'string' &&
|
|
63
|
+
!Papa.BAD_DELIMITERS.filter(function (value) {
|
|
64
|
+
return _config.delimiter?.indexOf(value) !== -1;
|
|
65
|
+
}).length) {
|
|
66
|
+
_delimiter = _config.delimiter;
|
|
67
|
+
}
|
|
68
|
+
if (typeof _config.quotes === 'boolean' || Array.isArray(_config.quotes))
|
|
69
|
+
_quotes = _config.quotes;
|
|
70
|
+
if (typeof _config.skipEmptyLines === 'boolean' || _config.skipEmptyLines === 'greedy')
|
|
71
|
+
_skipEmptyLines = _config.skipEmptyLines;
|
|
72
|
+
if (typeof _config.newline === 'string')
|
|
73
|
+
_newline = _config.newline;
|
|
74
|
+
if (typeof _config.quoteChar === 'string')
|
|
75
|
+
_quoteChar = _config.quoteChar;
|
|
76
|
+
if (typeof _config.header === 'boolean')
|
|
77
|
+
_writeHeader = _config.header;
|
|
78
|
+
if (Array.isArray(_config.columns)) {
|
|
79
|
+
if (_config.columns.length === 0)
|
|
80
|
+
throw new Error('Option columns is empty');
|
|
81
|
+
_columns = _config.columns;
|
|
82
|
+
}
|
|
83
|
+
if (_config.escapeChar !== undefined) {
|
|
84
|
+
_escapedQuote = _config.escapeChar + _quoteChar;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** The double for loop that iterates the data and writes out a CSV string including header row */
|
|
88
|
+
// eslint-disable-next-line complexity, max-statements
|
|
89
|
+
function serialize(fields, data, skipEmptyLines) {
|
|
90
|
+
let csv = '';
|
|
91
|
+
if (typeof fields === 'string')
|
|
92
|
+
fields = JSON.parse(fields);
|
|
93
|
+
if (typeof data === 'string')
|
|
94
|
+
data = JSON.parse(data);
|
|
95
|
+
const hasHeader = Array.isArray(fields) && fields.length > 0;
|
|
96
|
+
const dataKeyedByField = !Array.isArray(data[0]);
|
|
97
|
+
// If there a header row, write it first
|
|
98
|
+
if (hasHeader && _writeHeader) {
|
|
99
|
+
for (let i = 0; i < fields.length; i++) {
|
|
100
|
+
if (i > 0)
|
|
101
|
+
csv += _delimiter;
|
|
102
|
+
csv += safe(fields[i], i);
|
|
103
|
+
}
|
|
104
|
+
if (data.length > 0)
|
|
105
|
+
csv += _newline;
|
|
106
|
+
}
|
|
107
|
+
// Then write out the data
|
|
108
|
+
for (let row = 0; row < data.length; row++) {
|
|
109
|
+
const maxCol = hasHeader ? fields.length : data[row].length;
|
|
110
|
+
let emptyLine = false;
|
|
111
|
+
const nullLine = hasHeader ? Object.keys(data[row]).length === 0 : data[row].length === 0;
|
|
112
|
+
if (skipEmptyLines && !hasHeader) {
|
|
113
|
+
emptyLine =
|
|
114
|
+
skipEmptyLines === 'greedy'
|
|
115
|
+
? data[row].join('').trim() === ''
|
|
116
|
+
: data[row].length === 1 && data[row][0].length === 0;
|
|
117
|
+
}
|
|
118
|
+
if (skipEmptyLines === 'greedy' && hasHeader) {
|
|
119
|
+
const line = [];
|
|
120
|
+
for (let c = 0; c < maxCol; c++) {
|
|
121
|
+
const cx = dataKeyedByField ? fields[c] : c;
|
|
122
|
+
line.push(data[row][cx]);
|
|
123
|
+
}
|
|
124
|
+
emptyLine = line.join('').trim() === '';
|
|
125
|
+
}
|
|
126
|
+
if (!emptyLine) {
|
|
127
|
+
for (let col = 0; col < maxCol; col++) {
|
|
128
|
+
if (col > 0 && !nullLine)
|
|
129
|
+
csv += _delimiter;
|
|
130
|
+
const colIdx = hasHeader && dataKeyedByField ? fields[col] : col;
|
|
131
|
+
csv += safe(data[row][colIdx], col);
|
|
132
|
+
}
|
|
133
|
+
if (row < data.length - 1 && (!skipEmptyLines || (maxCol > 0 && !nullLine))) {
|
|
134
|
+
csv += _newline;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return csv;
|
|
139
|
+
}
|
|
140
|
+
/** Encloses a value around quotes if needed (makes a value safe for CSV insertion) */
|
|
141
|
+
// eslint-disable-next-line complexity
|
|
142
|
+
function safe(str, col) {
|
|
143
|
+
if (typeof str === 'undefined' || str === null)
|
|
144
|
+
return '';
|
|
145
|
+
if (str.constructor === Date)
|
|
146
|
+
return JSON.stringify(str).slice(1, 25);
|
|
147
|
+
str = str.toString().replace(quoteCharRegex, _escapedQuote);
|
|
148
|
+
const needsQuotes = (typeof _quotes === 'boolean' && _quotes) ||
|
|
149
|
+
(Array.isArray(_quotes) && _quotes[col]) ||
|
|
150
|
+
hasAny(str, Papa.BAD_DELIMITERS) ||
|
|
151
|
+
str.indexOf(_delimiter) > -1 ||
|
|
152
|
+
str.charAt(0) === ' ' ||
|
|
153
|
+
str.charAt(str.length - 1) === ' ';
|
|
154
|
+
return needsQuotes ? _quoteChar + str + _quoteChar : str;
|
|
155
|
+
}
|
|
156
|
+
function hasAny(str, substrings) {
|
|
157
|
+
for (let i = 0; i < substrings.length; i++)
|
|
158
|
+
if (str.indexOf(substrings[i]) > -1)
|
|
159
|
+
return true;
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/** https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions */
|
|
164
|
+
function escapeRegExp(string) {
|
|
165
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
|
166
|
+
}
|
|
@@ -1,114 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
step?: Function;
|
|
7
|
-
transform?: boolean;
|
|
8
|
-
preview?: number;
|
|
9
|
-
newline?: string;
|
|
10
|
-
comments?: boolean;
|
|
11
|
-
skipEmptyLines?: boolean | 'greedy';
|
|
12
|
-
delimitersToGuess?: string[];
|
|
13
|
-
quoteChar?: string;
|
|
14
|
-
escapeChar?: string;
|
|
15
|
-
delimiter?: string;
|
|
16
|
-
fastMode?: boolean;
|
|
17
|
-
};
|
|
18
|
-
declare function CsvToJson(_input: any, _config?: CSVParserConfig, Streamer?: any): any;
|
|
19
|
-
declare function JsonToCsv(_input: any, _config: any): string;
|
|
20
|
-
/** ChunkStreamer is the base prototype for various streamer implementations. */
|
|
21
|
-
declare class ChunkStreamer {
|
|
22
|
-
_handle: any;
|
|
23
|
-
_config: any;
|
|
24
|
-
_finished: boolean;
|
|
25
|
-
_completed: boolean;
|
|
26
|
-
_input: null;
|
|
27
|
-
_baseIndex: number;
|
|
28
|
-
_partialLine: string;
|
|
29
|
-
_rowCount: number;
|
|
30
|
-
_start: number;
|
|
31
|
-
isFirstChunk: boolean;
|
|
32
|
-
_completeResults: {
|
|
33
|
-
data: never[];
|
|
34
|
-
errors: never[];
|
|
35
|
-
meta: {};
|
|
36
|
-
};
|
|
37
|
-
constructor(config: CSVParserConfig);
|
|
38
|
-
parseChunk(chunk: any, isFakeChunk?: boolean): any;
|
|
39
|
-
_sendError(error: any): void;
|
|
40
|
-
}
|
|
41
|
-
declare class ParserHandle {
|
|
42
|
-
_config: any;
|
|
43
|
-
/** Number of times step was called (number of rows parsed) */
|
|
44
|
-
_stepCounter: number;
|
|
45
|
-
/** Number of rows that have been parsed so far */
|
|
46
|
-
_rowCounter: number;
|
|
47
|
-
/** The input being parsed */
|
|
48
|
-
_input: any;
|
|
49
|
-
/** The core parser being used */
|
|
50
|
-
_parser: any;
|
|
51
|
-
/** Whether we are paused or not */
|
|
52
|
-
_paused: boolean;
|
|
53
|
-
/** Whether the parser has aborted or not */
|
|
54
|
-
_aborted: boolean;
|
|
55
|
-
/** Temporary state between delimiter detection and processing results */
|
|
56
|
-
_delimiterError: boolean;
|
|
57
|
-
/** Fields are from the header row of the input, if there is one */
|
|
58
|
-
_fields: string[];
|
|
59
|
-
/** The last results returned from the parser */
|
|
60
|
-
_results: {
|
|
61
|
-
data: any[][] | Record<string, any>[];
|
|
62
|
-
errors: any[];
|
|
63
|
-
meta: Record<string, any>;
|
|
64
|
-
};
|
|
65
|
-
constructor(_config: CSVParserConfig);
|
|
66
|
-
/**
|
|
67
|
-
* Parses input. Most users won't need, and shouldn't mess with, the baseIndex
|
|
68
|
-
* and ignoreLastRow parameters. They are used by streamers (wrapper functions)
|
|
69
|
-
* when an input comes in multiple chunks, like from a file.
|
|
70
|
-
*/
|
|
71
|
-
parse(input: any, baseIndex: any, ignoreLastRow: any): {
|
|
72
|
-
data: any[][] | Record<string, any>[];
|
|
73
|
-
errors: any[];
|
|
74
|
-
meta: Record<string, any>;
|
|
75
|
-
} | {
|
|
76
|
-
meta: {
|
|
77
|
-
paused: boolean;
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
paused(): boolean;
|
|
81
|
-
pause(): void;
|
|
82
|
-
resume(): void;
|
|
83
|
-
aborted(): boolean;
|
|
84
|
-
abort(): void;
|
|
85
|
-
testEmptyLine(s: any): boolean;
|
|
86
|
-
processResults(): {
|
|
87
|
-
data: any[][] | Record<string, any>[];
|
|
88
|
-
errors: any[];
|
|
89
|
-
meta: Record<string, any>;
|
|
90
|
-
};
|
|
91
|
-
needsHeaderRow(): any;
|
|
92
|
-
fillHeaderFields(): void;
|
|
93
|
-
shouldApplyDynamicTyping(field: any): boolean;
|
|
94
|
-
parseDynamic(field: any, value: any): any;
|
|
95
|
-
applyHeaderAndDynamicTypingAndTransformation(): {
|
|
96
|
-
data: any[][] | Record<string, any>[];
|
|
97
|
-
errors: any[];
|
|
98
|
-
meta: Record<string, any>;
|
|
99
|
-
};
|
|
100
|
-
processRow(rowSource: any, i: any): any[] | Record<string, any>;
|
|
101
|
-
guessDelimiter(input: any, newline: any, skipEmptyLines: any, comments: any, delimitersToGuess: any): {
|
|
102
|
-
successful: boolean;
|
|
103
|
-
bestDelimiter: any;
|
|
104
|
-
};
|
|
105
|
-
addError(type: any, code: any, msg: any, row?: any): void;
|
|
106
|
-
}
|
|
107
|
-
/** The core parser implements speedy and correct CSV parsing */
|
|
108
|
-
declare function Parser(config: any): void;
|
|
109
|
-
declare const Papa: {
|
|
1
|
+
import { CsvToJson, Parser, ParserHandle, ChunkStreamer } from "./papa-parser.js";
|
|
2
|
+
import { JsonToCsv } from "./papa-writer.js";
|
|
3
|
+
export type { CSVParserConfig } from "./papa-parser.js";
|
|
4
|
+
export type { CSVWriterConfig } from "./papa-writer.js";
|
|
5
|
+
declare const _default: {
|
|
110
6
|
parse: typeof CsvToJson;
|
|
111
7
|
unparse: typeof JsonToCsv;
|
|
8
|
+
ChunkStreamer: typeof ChunkStreamer;
|
|
9
|
+
Parser: typeof Parser;
|
|
10
|
+
ParserHandle: typeof ParserHandle;
|
|
112
11
|
RECORD_SEP: string;
|
|
113
12
|
UNIT_SEP: string;
|
|
114
13
|
BYTE_ORDER_MARK: string;
|
|
@@ -118,9 +17,6 @@ declare const Papa: {
|
|
|
118
17
|
LocalChunkSize: number;
|
|
119
18
|
RemoteChunkSize: number;
|
|
120
19
|
DefaultDelimiter: string;
|
|
121
|
-
Parser: typeof Parser;
|
|
122
|
-
ParserHandle: typeof ParserHandle;
|
|
123
|
-
ChunkStreamer: typeof ChunkStreamer;
|
|
124
20
|
};
|
|
125
|
-
export default
|
|
21
|
+
export default _default;
|
|
126
22
|
//# sourceMappingURL=papaparse.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"papaparse.d.ts","sourceRoot":"","sources":["../../src/papaparse/papaparse.ts"],"names":[],"mappings":"AAeA,
|
|
1
|
+
{"version":3,"file":"papaparse.d.ts","sourceRoot":"","sources":["../../src/papaparse/papaparse.ts"],"names":[],"mappings":"AAeA,OAAO,EAAC,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,aAAa,EAAC,yBAAsB;AAC7E,OAAO,EAAC,SAAS,EAAC,yBAAsB;AAGxC,YAAY,EAAC,eAAe,EAAC,yBAAsB;AACnD,YAAY,EAAC,eAAe,EAAC,yBAAsB;;;;;;;;;;;;;;;;;AAEnD,wBAWE"}
|