1 | // Type definitions for PapaParse 5.3
|
2 | // Project: https://github.com/mholt/PapaParse
|
3 | // Definitions by: Pedro Flemming <https://github.com/torpedro>
|
4 | // Rain Shen <https://github.com/rainshen49>
|
5 | // João Loff <https://github.com/jfloff>
|
6 | // John Reilly <https://github.com/johnnyreilly>
|
7 | // Alberto Restifo <https://github.com/albertorestifo>
|
8 | // Janne Liuhtonen <https://github.com/jliuhtonen>
|
9 | // Raphaël Barbazza <https://github.com/rbarbazz>
|
10 | // Piotr Błażejewicz <https://github.com/peterblazejewicz>
|
11 | // Emmanuel Gautier <https://github.com/emmanuelgautier>
|
12 | // Opportunity Liu <https://github.com/OpportunityLiu>
|
13 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
14 |
|
15 | /// <reference types="node" />
|
16 |
|
17 | import { Duplex } from "stream";
|
18 |
|
19 | export as namespace Papa;
|
20 |
|
21 | export {}; // Don't export all declarations!
|
22 |
|
23 | /**
|
24 | * Parse local files
|
25 | * @param file a File object obtained from the DOM.
|
26 | * @param config a config object which contains a callback.
|
27 | * @returns Doesn't return anything. Results are provided asynchronously to a callback function.
|
28 | */
|
29 | // eslint-disable-next-line @definitelytyped/no-unnecessary-generics
|
30 | export function parse<T, TFile extends LocalFile = LocalFile>(file: TFile, config: ParseLocalConfig<T, TFile>): void;
|
31 | /**
|
32 | * Parse remote files
|
33 | * @param url the path or URL to the file to download.
|
34 | * @param config a config object.
|
35 | * @returns Doesn't return anything. Results are provided asynchronously to a callback function.
|
36 | */
|
37 | // eslint-disable-next-line @definitelytyped/no-unnecessary-generics
|
38 | export function parse<T>(url: string, config: ParseRemoteConfig<T>): void;
|
39 | /**
|
40 | * Parse string in web worker
|
41 | * @param csvString a string of delimited text to be parsed.
|
42 | * @param config an optional config object.
|
43 | * @returns Doesn't return anything. Results are provided asynchronously to a callback function.
|
44 | */
|
45 | /* eslint-disable @definitelytyped/no-unnecessary-generics */
|
46 | // tslint:disable-next-line:unified-signatures
|
47 | export function parse<T>(csvString: string, config: ParseWorkerConfig<T> & { download?: false | undefined }): void;
|
48 | /* eslint-enable @definitelytyped/no-unnecessary-generics */
|
49 | /**
|
50 | * Parse string
|
51 | * @param csvString a string of delimited text to be parsed.
|
52 | * @param config an optional config object.
|
53 | * @returns a parse results object
|
54 | */
|
55 | export function parse<T>(
|
56 | csvString: string,
|
57 | config?: ParseConfig<T> & { download?: false | undefined; worker?: false | undefined },
|
58 | ): ParseResult<T>;
|
59 | /**
|
60 | * Parse string, remote files or local files
|
61 | * @param source data to be parsed.
|
62 | * @param config a config object.
|
63 | * @returns Doesn't return anything. Results are provided asynchronously to a callback function.
|
64 | */
|
65 | export function parse<T>(
|
66 | source: LocalFile | string,
|
67 | config: ParseLocalConfig<T, LocalFile> &
|
68 | (
|
69 | | (ParseConfig<T> & { download?: false | undefined; worker?: false | undefined })
|
70 | | (ParseWorkerConfig<T> & { download?: false | undefined })
|
71 | | ParseRemoteConfig<T>
|
72 | ),
|
73 | ): void;
|
74 | /**
|
75 | * Parse in a node streaming style
|
76 | * @param stream `NODE_STREAM_INPUT`
|
77 | * @param config a config object.
|
78 | * @returns a node duplex stream.
|
79 | *
|
80 | * @see https://github.com/mholt/PapaParse#papa-parse-for-node
|
81 | */
|
82 | export function parse(stream: typeof NODE_STREAM_INPUT, config?: ParseConfig): Duplex;
|
83 |
|
84 | /**
|
85 | * Unparses javascript data objects and returns a csv string
|
86 | * @param data can be one of: An array of arrays; An array of objects; An object explicitly defining `fields` and `data`
|
87 | * @param config an optional config object
|
88 | */
|
89 | export function unparse<T>(data: T[] | UnparseObject<T>, config?: UnparseConfig): string;
|
90 |
|
91 | /**
|
92 | * Read-Only Properties
|
93 | */
|
94 |
|
95 | /** An array of characters that are not allowed as delimiters. `\r`, `\n`, `"`, `\ufeff` */
|
96 | export const BAD_DELIMITERS: ReadonlyArray<string>;
|
97 |
|
98 | /** The true delimiter. Invisible. ASCII code 30. Should be doing the job we strangely rely upon commas and tabs for. */
|
99 | export const RECORD_SEP: '\x1E';
|
100 |
|
101 | /** Also sometimes used as a delimiting character. ASCII code 31. */
|
102 | export const UNIT_SEP: '\x1F';
|
103 | /**
|
104 | * Whether or not the browser supports HTML5 Web Workers.
|
105 | * If false, `worker: true` will have no effect.
|
106 | */
|
107 | export const WORKERS_SUPPORTED: boolean;
|
108 |
|
109 | /**
|
110 | * When passed to Papa Parse a Readable stream is returned.
|
111 | */
|
112 | export const NODE_STREAM_INPUT: unique symbol;
|
113 |
|
114 | /**
|
115 | * Configurable Properties
|
116 | */
|
117 |
|
118 | /**
|
119 | * The size in bytes of each file chunk. Used when streaming files obtained from the DOM that exist on the local computer. Default 10 MB.
|
120 | * @default 10485760
|
121 | */
|
122 | export let LocalChunkSize: number;
|
123 |
|
124 | /**
|
125 | * Same as `LocalChunkSize`, but for downloading files from remote locations. Default 5 MB.
|
126 | * @default 5242880
|
127 | */
|
128 | export let RemoteChunkSize: number;
|
129 |
|
130 | /**
|
131 | * The delimiter used when it is left unspecified and cannot be detected automatically. Default is comma.
|
132 | * @default ','
|
133 | */
|
134 | export let DefaultDelimiter: string;
|
135 |
|
136 | /** File object */
|
137 | export type LocalFile = File | NodeJS.ReadableStream;
|
138 |
|
139 | /**
|
140 | * On Papa there are actually more classes exposed
|
141 | * but none of them are officially documented
|
142 | * Since we can interact with the Parser from one of the callbacks
|
143 | * I have included the API for this class.
|
144 | */
|
145 | export class Parser {
|
146 | constructor(config: ParseConfig);
|
147 |
|
148 | parse(input: string, baseIndex: number, ignoreLastRow: boolean): any;
|
149 |
|
150 | // Sets the abort flag
|
151 | abort(): void;
|
152 |
|
153 | // Gets the cursor position
|
154 | getCharIndex(): number;
|
155 |
|
156 | pause(): void;
|
157 | resume(): void;
|
158 | }
|
159 |
|
160 | export interface ParseConfig<T = any, TInput = undefined> {
|
161 | /**
|
162 | * The delimiting character.
|
163 | * Leave blank to auto-detect from a list of most common delimiters, or any values passed in through `delimitersToGuess`.
|
164 | * It can be a string or a function.
|
165 | * If a string, it can be of any length (so multi-character delimiters are supported).
|
166 | * If a function, it must accept the input as first parameter and it must return a string which will be used as delimiter.
|
167 | * In both cases it cannot be found in `Papa.BAD_DELIMITERS`.
|
168 | * @default // auto-detect
|
169 | */
|
170 | delimiter?: string | ((input: string) => string) | undefined;
|
171 | /**
|
172 | * The newline sequence. Leave blank to auto-detect. Must be one of `\r`, `\n`, or `\r\n`.
|
173 | * @default // auto-detect
|
174 | */
|
175 | newline?: '\r' | '\n' | '\r\n' | undefined;
|
176 | /**
|
177 | * The character used to quote fields. The quoting of all fields is not mandatory. Any field which is not quoted will correctly read.
|
178 | * @default '"'
|
179 | */
|
180 | quoteChar?: string | undefined;
|
181 | /**
|
182 | * The character used to escape the quote character within a field.
|
183 | * If not set, this option will default to the value of `quoteChar`,
|
184 | * meaning that the default escaping of quote character within a quoted field is using the quote character two times.
|
185 | * (e.g. `"column with ""quotes"" in text"`)
|
186 | * @default '"'
|
187 | */
|
188 | escapeChar?: string | undefined;
|
189 | /**
|
190 | * If `true`, the first row of parsed data will be interpreted as field names.
|
191 | * An array of field names will be returned in meta, and each row of data will be an object of values keyed by field name instead of a simple array.
|
192 | * Rows with a different number of fields from the header row will produce an error.
|
193 | * Warning: Duplicate field names will overwrite values in previous fields having the same name.
|
194 | * @default false
|
195 | */
|
196 | header?: boolean | undefined;
|
197 | /**
|
198 | * A function to apply on each header. Requires header to be true. The function receives the header as its first argument and the index as second.
|
199 | */
|
200 | transformHeader?(header: string, index: number): string;
|
201 | /**
|
202 | * If `true`, numeric and boolean data will be converted to their type instead of remaining strings.
|
203 | * Numeric data must conform to the definition of a decimal literal.
|
204 | * Numerical values greater than 2^53 or less than -2^53 will not be converted to numbers to preserve precision.
|
205 | * European-formatted numbers must have commas and dots swapped.
|
206 | * If also accepts an object or a function.
|
207 | * If object it's values should be a boolean to indicate if dynamic typing should be applied for each column number (or header name if using headers).
|
208 | * If it's a function, it should return a boolean value for each field number (or name if using headers) which will be passed as first argument.
|
209 | * @default false
|
210 | */
|
211 | dynamicTyping?:
|
212 | | boolean
|
213 | | { [headerName: string]: boolean; [columnNumber: number]: boolean }
|
214 | | ((field: string | number) => boolean)
|
215 | | undefined;
|
216 | /** If > 0, only that many rows will be parsed. */
|
217 | preview?: number | undefined;
|
218 | /**
|
219 | * A string that indicates a comment (for example, "#" or "//").
|
220 | * When Papa encounters a line starting with this string, it will skip the line.
|
221 | * @default false
|
222 | */
|
223 | comments?: false | string | undefined;
|
224 | /**
|
225 | * If `true`, lines that are completely empty (those which evaluate to an empty string) will be skipped.
|
226 | * If set to `'greedy'`, lines that don't have any content (those which have only whitespace after parsing) will also be skipped.
|
227 | * @default false
|
228 | */
|
229 | skipEmptyLines?: boolean | 'greedy' | undefined;
|
230 | /**
|
231 | * Fast mode speeds up parsing significantly for large inputs.
|
232 | * However, it only works when the input has no quoted fields.
|
233 | * Fast mode will automatically be enabled if no " characters appear in the input.
|
234 | * You can force fast mode either way by setting it to true or false.
|
235 | */
|
236 | fastMode?: boolean | undefined;
|
237 | /**
|
238 | * A function to apply on each value.
|
239 | * The function receives the value as its first argument and the column number or header name when enabled as its second argument.
|
240 | * The return value of the function will replace the value it received.
|
241 | * The transform function is applied before `dynamicTyping`.
|
242 | */
|
243 | transform?(value: string, field: string | number): any;
|
244 | /**
|
245 | * An array of delimiters to guess from if the delimiter option is not set.
|
246 | * @default [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP]
|
247 | */
|
248 | delimitersToGuess?: string[] | undefined;
|
249 | /**
|
250 | * To stream the input, define a callback function.
|
251 | * Streaming is necessary for large files which would otherwise crash the browser.
|
252 | * You can call parser.abort() to abort parsing.
|
253 | * And, except when using a Web Worker, you can call parser.pause() to pause it, and parser.resume() to resume.
|
254 | */
|
255 | step?(results: ParseStepResult<T>, parser: Parser): void;
|
256 | /**
|
257 | * The callback to execute when parsing is complete.
|
258 | * It receives the parse results. If parsing a local file, the File is passed in, too.
|
259 | * When streaming, parse results are not available in this callback.
|
260 | */
|
261 | complete?(results: ParseResult<T>, file: TInput): void;
|
262 | /**
|
263 | * A function to execute before parsing the first chunk.
|
264 | * Can be used with chunk or step streaming modes.
|
265 | * The function receives as an argument the chunk about to be parsed, and it may return a modified chunk to parse.
|
266 | * This is useful for stripping header lines (as long as the header fits in a single chunk).
|
267 | */
|
268 | beforeFirstChunk?(chunk: string): string | void;
|
269 | }
|
270 |
|
271 | export interface ParseWorkerConfig<T = any> extends ParseConfig<T> {
|
272 | /**
|
273 | * Whether or not to use a worker thread.
|
274 | * Using a worker will keep your page reactive, but may be slightly slower.
|
275 | */
|
276 | worker: true;
|
277 | /**
|
278 | * The callback to execute when parsing is complete.
|
279 | * It receives the parse results. If parsing a local file, the File is passed in, too.
|
280 | * When streaming, parse results are not available in this callback.
|
281 | */
|
282 | complete(results: ParseResult<T>): void;
|
283 | }
|
284 |
|
285 | // Base interface for all async parsing
|
286 | interface ParseAsyncConfigBase<T = any, TInput = undefined> extends ParseConfig<T, TInput> {
|
287 | /**
|
288 | * Whether or not to use a worker thread.
|
289 | * Using a worker will keep your page reactive, but may be slightly slower.
|
290 | * @default false
|
291 | */
|
292 | worker?: boolean | undefined;
|
293 | /**
|
294 | * Overrides `Papa.LocalChunkSize` and `Papa.RemoteChunkSize`.
|
295 | */
|
296 | chunkSize?: number | undefined;
|
297 | /**
|
298 | * A callback function, identical to `step`, which activates streaming.
|
299 | * However, this function is executed after every chunk of the file is loaded and parsed rather than every row.
|
300 | * Works only with local and remote files.
|
301 | * Do not use both `chunk` and `step` callbacks together.
|
302 | */
|
303 | chunk?(results: ParseResult<T>, parser: Parser): void;
|
304 | /**
|
305 | * A callback to execute if FileReader encounters an error.
|
306 | * The function is passed two arguments: the error and the File.
|
307 | */
|
308 | error?(error: Error, file: TInput): void;
|
309 | }
|
310 |
|
311 | // Async parsing local file can specify encoding
|
312 | interface ParseLocalConfigBase<T = any, TInput = undefined> extends ParseAsyncConfigBase<T, TInput> {
|
313 | /** The encoding to use when opening local files. If specified, it must be a value supported by the FileReader API. */
|
314 | encoding?: string | undefined;
|
315 | }
|
316 |
|
317 | interface ParseLocalConfigStep<T = any, TInput = undefined> extends ParseLocalConfigBase<T, TInput> {
|
318 | /** @inheritdoc */
|
319 | step(results: ParseStepResult<T>, parser: Parser): void;
|
320 | }
|
321 | interface ParseLocalConfigNoStep<T = any, TInput = undefined> extends ParseLocalConfigBase<T, TInput> {
|
322 | /** @inheritdoc */
|
323 | complete(results: ParseResult<T>, file: TInput): void;
|
324 | }
|
325 |
|
326 | // Local parsing is async and thus must specify either `step` or `complete` (but may specify both)
|
327 | export type ParseLocalConfig<T = any, TInput = undefined> = ParseLocalConfigStep<T, TInput> | ParseLocalConfigNoStep<T, TInput>;
|
328 |
|
329 | // Remote parsing has options for the backing web request
|
330 | interface ParseRemoteConfigBase<T = any> extends ParseAsyncConfigBase<T, string> {
|
331 | /**
|
332 | * This indicates that the string you passed as the first argument to `parse()`
|
333 | * is actually a URL from which to download a file and parse its contents.
|
334 | */
|
335 | download: true;
|
336 | /**
|
337 | * If defined, should be an object that describes the headers.
|
338 | * @example { 'Authorization': 'token 123345678901234567890' }
|
339 | * @default undefined
|
340 | */
|
341 | downloadRequestHeaders?: { [headerName: string]: string } | undefined;
|
342 | /**
|
343 | * Use POST request on the URL of the download option. The value passed will be set as the body of the request.
|
344 | * @default undefined
|
345 | */
|
346 | downloadRequestBody?: Blob | BufferSource | FormData | URLSearchParams | string | undefined;
|
347 | /**
|
348 | * A boolean value passed directly into XMLHttpRequest's "withCredentials" property.
|
349 | * @default undefined
|
350 | */
|
351 | withCredentials?: boolean | undefined;
|
352 | }
|
353 |
|
354 | interface ParseRemoteConfigStep<T = any> extends ParseRemoteConfigBase<T> {
|
355 | /** @inheritdoc */
|
356 | step(results: ParseStepResult<T>, parser: Parser): void;
|
357 | }
|
358 | interface ParseRemoteConfigNoStep<T = any> extends ParseRemoteConfigBase<T> {
|
359 | /** @inheritdoc */
|
360 | complete(results: ParseResult<T>, file: string): void;
|
361 | }
|
362 |
|
363 | // Remote parsing is async and thus must specify either `step` or `complete` (but may specify both)
|
364 | export type ParseRemoteConfig<T = any> = ParseRemoteConfigStep<T> | ParseRemoteConfigNoStep<T>;
|
365 |
|
366 | export interface UnparseConfig {
|
367 | /**
|
368 | * If `true`, forces all fields to be enclosed in quotes.
|
369 | * If an array of `true`/`false` values, specifies which fields should be force-quoted (first boolean is for the first column, second boolean for the second column, ...).
|
370 | * A function that returns a boolean values can be used to determine the quotes value of a cell.
|
371 | * This function accepts the cell value and column index as parameters.
|
372 | * Note that this option is ignored for `undefined`, `null` and `date-object` values.
|
373 | * The option `escapeFormulae` also takes precedence over this.
|
374 | *
|
375 | * @default false
|
376 | */
|
377 | quotes?: boolean | boolean[] | ((value: any, columnIndex: number) => boolean) | undefined;
|
378 | /**
|
379 | * The character used to quote fields.
|
380 | * @default '"'
|
381 | */
|
382 | quoteChar?: string | undefined;
|
383 | /**
|
384 | * The character used to escape `quoteChar` inside field values.
|
385 | * @default '"'
|
386 | */
|
387 | escapeChar?: string | undefined;
|
388 | /**
|
389 | * The delimiting character. Multi-character delimiters are supported. It must not be found in `Papa.BAD_DELIMITERS`.
|
390 | * @default ','
|
391 | */
|
392 | delimiter?: string | undefined;
|
393 | /**
|
394 | * If `false`, will omit the header row.
|
395 | * If `data` is an array of arrays this option is ignored.
|
396 | * If `data` is an array of objects the keys of the first object are the header row.
|
397 | * If `data` is an object with the `keys` fields and `data` the `fields` are the header row.
|
398 | * @default true
|
399 | */
|
400 | header?: boolean | undefined;
|
401 | /**
|
402 | * The character used to determine newline sequence.
|
403 | * @default '\r\n'
|
404 | */
|
405 | newline?: string | undefined;
|
406 | /**
|
407 | * If `true`, lines that are completely empty (those which evaluate to an empty string) will be skipped.
|
408 | * If set to `'greedy'`, lines that don't have any content (those which have only whitespace after parsing) will also be skipped.
|
409 | * @default false
|
410 | */
|
411 | skipEmptyLines?: boolean | 'greedy' | undefined;
|
412 | /**
|
413 | * If `data` is an array of objects this option can be used to manually specify the keys (columns) you expect in the objects.
|
414 | * If not set the keys of the first objects are used as column.
|
415 | * @default undefined
|
416 | */
|
417 | columns?: string[] | undefined;
|
418 | /**
|
419 | * If `true`, field values that begin with `=`, `+`, `-`, or `@`,
|
420 | * will be prepended with a ` to defend against [injection attacks](https://www.contextis.com/en/blog/comma-separated-vulnerabilities),
|
421 | * because Excel and LibreOffice will automatically parse such cells as formulae.
|
422 | * @default false
|
423 | */
|
424 | escapeFormulae?: boolean | RegExp | undefined;
|
425 | }
|
426 |
|
427 | export interface UnparseObject<T> {
|
428 | fields: string[];
|
429 | data: T[];
|
430 | }
|
431 |
|
432 | /** Error structure */
|
433 | export interface ParseError {
|
434 | /** A generalization of the error */
|
435 | type: 'Quotes' | 'Delimiter' | 'FieldMismatch';
|
436 | /** Standardized error code */
|
437 | code: 'MissingQuotes' | 'UndetectableDelimiter' | 'TooFewFields' | 'TooManyFields' | 'InvalidQuotes';
|
438 | /** Human-readable details */
|
439 | message: string;
|
440 | /** Row index of parsed data where error is */
|
441 | row: number;
|
442 | }
|
443 |
|
444 | export interface ParseMeta {
|
445 | /** Delimiter used */
|
446 | delimiter: string;
|
447 | /** Line break sequence used */
|
448 | linebreak: string;
|
449 | /** Whether process was aborted */
|
450 | aborted: boolean;
|
451 | /** Array of field names */
|
452 | fields?: string[] | undefined;
|
453 | /** Whether preview consumed all input */
|
454 | truncated: boolean;
|
455 | cursor: number;
|
456 | }
|
457 |
|
458 | /**
|
459 | * A parse result always contains three objects: data, errors, and meta.
|
460 | * Data and errors are arrays, and meta is an object. In the step callback, the data array will only contain one element.
|
461 | */
|
462 | export interface ParseStepResult<T> {
|
463 | /**
|
464 | * In the step callback, the data array will only contain one element.
|
465 | */
|
466 | data: T;
|
467 | /** an array of errors. */
|
468 | errors: ParseError[];
|
469 | /**
|
470 | * contains extra information about the parse, such as delimiter used,
|
471 | * the newline sequence, whether the process was aborted, etc.
|
472 | * Properties in this object are not guaranteed to exist in all situations.
|
473 | */
|
474 | meta: ParseMeta;
|
475 | }
|
476 |
|
477 | /**
|
478 | * A parse result always contains three objects: data, errors, and meta.
|
479 | * Data and errors are arrays, and meta is an object. In the step callback, the data array will only contain one element.
|
480 | */
|
481 | export interface ParseResult<T> {
|
482 | /**
|
483 | * an array of rows. If header is false, rows are arrays; otherwise they are objects of data keyed by the field name.
|
484 | */
|
485 | data: T[];
|
486 | /** an array of errors. */
|
487 | errors: ParseError[];
|
488 | /**
|
489 | * contains extra information about the parse, such as delimiter used,
|
490 | * the newline sequence, whether the process was aborted, etc.
|
491 | * Properties in this object are not guaranteed to exist in all situations.
|
492 | */
|
493 | meta: ParseMeta;
|
494 | }
|
495 |
|
\ | No newline at end of file |