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