UNPKG

4.58 kBTypeScriptView Raw
1/// <reference types="node"/>
2import { Transform } from 'stream';
3
4declare namespace csvParser {
5 type CsvParser = Transform;
6
7 interface Options {
8 /**
9 * A single-character string used to specify the character used to escape strings in a CSV row.
10 *
11 * @default '"'
12 */
13 readonly escape?: string;
14
15 /**
16 * Specifies the headers to use. Headers define the property key for each value in a CSV row. If no `headers` option is provided, `csv-parser` will use the first line in a CSV file as the header specification.
17 *
18 * If `false`, specifies that the first row in a data file does _not_ contain headers, and instructs the parser to use the row index as the key for each row.
19 *
20 * Suppose you have a CSV file `data.csv` which contains the data:
21 *
22 * ```
23NAME,AGE
24Daffy Duck,24
25Bugs Bunny,22
26```
27 * Using `headers: false` with the data from `data.csv` would yield:
28 * ```
29[
30 { '0': 'Daffy Duck', '1': 24 },
31 { '0': 'Bugs Bunny', '1': 22 }
32]
33```
34 */
35 readonly headers?: ReadonlyArray<string> | boolean;
36
37 /**
38 * A function that can be used to modify the values of each header. Return `null` to remove the header, and it's column, from the results.
39 *
40 * @example
41 *
42 * csv({
43 * mapHeaders: ({ header, index }) => header.toLowerCase()
44 * });
45 */
46 readonly mapHeaders?: (args: { header: string; index: number }) => string | null;
47
48 /**
49 * A function that can be used to modify the value of each column value.
50 *
51 * @example
52 *
53 * csv({
54 * mapValues: ({ header, index, value }) => value.toLowerCase()
55 * });
56 */
57 readonly mapValues?: (args: { header: string; index: number; value: any }) => any;
58
59 /**
60 * Specifies a single-character string to denote the end of a line in a CSV file.
61 *
62 * @default '\n'
63 */
64 readonly newline?: string;
65
66 /**
67 * Specifies a single-character string to denote a quoted string.
68 *
69 * @default '"'
70 */
71 readonly quote?: string;
72
73 /**
74 * If `true`, instructs the parser not to decode UTF-8 strings.
75 */
76 readonly raw?: boolean;
77
78 /**
79 * Specifies a single-character string to use as the column separator for each row.
80 *
81 * @default ','
82 */
83 readonly separator?: string;
84
85 /**
86 * Instructs the parser to ignore lines which represent comments in a CSV file. Since there is no specification that dictates what a CSV comment looks like, comments should be considered non-standard. The "most common" character used to signify a comment in a CSV file is `"#"`. If this option is set to `true`, lines which begin with `#` will be skipped. If a custom character is needed to denote a commented line, this option may be set to a string which represents the leading character(s) signifying a comment line.
87 *
88 * @default false
89 */
90 readonly skipComments?: boolean | string;
91
92 /**
93 * Specifies the number of lines at the beginning of a data file that the parser should skip over, prior to parsing headers.
94 *
95 * @default 0
96 */
97 readonly skipLines?: number;
98
99 /**
100 * Maximum number of bytes per row. An error is thrown if a line exeeds this value. The default value is on 8 peta byte.
101 *
102 * @default Number.MAX_SAFE_INTEGER
103 */
104 readonly maxRowBytes?: number;
105
106 /**
107 * If `true`, instructs the parser that the number of columns in each row must match the number of `headers` specified.
108 */
109 readonly strict?: boolean;
110 }
111}
112
113/**
114 * Streaming CSV parser that aims for maximum speed as well as compatibility with the [csv-spectrum](https://npmjs.org/csv-spectrum) CSV acid test suite.
115 *
116 * @param optionsOrHeaders - As an alternative to passing an `options` object, you may pass an `Array[String]` which specifies the headers to use. If you need to specify options _and_ headers, please use the the object notation with the `headers` property.
117 *
118 * @example
119 *
120 * // data.csv:
121 * //
122 * // NAME,AGE
123 * // Daffy Duck,24
124 * // Bugs Bunny,22
125 *
126 * import csv = require('csv-parser');
127 * import * as fs from 'fs';
128 *
129 * const results = [];
130 *
131 * fs.createReadStream('data.csv')
132 * .pipe(csv())
133 * .on('data', (data) => results.push(data))
134 * .on('end', () => {
135 * console.log(results);
136 * // [
137 * // { NAME: 'Daffy Duck', AGE: '24' },
138 * // { NAME: 'Bugs Bunny', AGE: '22' }
139 * // ]
140 * });
141 */
142declare const csvParser: (
143 optionsOrHeaders?: csvParser.Options | ReadonlyArray<string>
144) => csvParser.CsvParser;
145
146export = csvParser;