forked from mafintosh/csv-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
152 lines (136 loc) · 4.63 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/// <reference types="node"/>
import { Transform } from 'stream';
declare namespace csvParser {
type CsvParserError = Error & {
readonly lineNumber: number;
readonly headers: ReadonlyArray<string>;
readonly cells: ReadonlyArray<string>;
}
type CsvParser = Transform;
interface Options {
/**
* A single-character string used to specify the character used to escape strings in a CSV row.
*
* @default '"'
*/
readonly escape?: string;
/**
* 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.
*
* 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.
*
* Suppose you have a CSV file `data.csv` which contains the data:
*
* ```
NAME,AGE
Daffy Duck,24
Bugs Bunny,22
```
* Using `headers: false` with the data from `data.csv` would yield:
* ```
[
{ '0': 'Daffy Duck', '1': 24 },
{ '0': 'Bugs Bunny', '1': 22 }
]
```
*/
readonly headers?: ReadonlyArray<string> | boolean;
/**
* 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.
*
* @example
*
* csv({
* mapHeaders: ({ header, index }) => header.toLowerCase()
* });
*/
readonly mapHeaders?: (args: { header: string; index: number }) => string | null;
/**
* A function that can be used to modify the value of each column value.
*
* @example
*
* csv({
* mapValues: ({ header, index, value }) => value.toLowerCase()
* });
*/
readonly mapValues?: (args: { header: string; index: number; value: any }) => any;
/**
* Specifies a single-character string to denote the end of a line in a CSV file.
*
* @default '\n'
*/
readonly newline?: string;
/**
* Specifies a single-character string to denote a quoted string.
*
* @default '"'
*/
readonly quote?: string;
/**
* If `true`, instructs the parser not to decode UTF-8 strings.
*/
readonly raw?: boolean;
/**
* Specifies a single-character string to use as the column separator for each row.
*
* @default ','
*/
readonly separator?: string;
/**
* 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.
*
* @default false
*/
readonly skipComments?: boolean | string;
/**
* Specifies the number of lines at the beginning of a data file that the parser should skip over, prior to parsing headers.
*
* @default 0
*/
readonly skipLines?: number;
/**
* Maximum number of bytes per row. An error is thrown if a line exeeds this value. The default value is on 8 peta byte.
*
* @default Number.MAX_SAFE_INTEGER
*/
readonly maxRowBytes?: number;
/**
* If `true`, instructs the parser that the number of columns in each row must match the number of `headers` specified.
*/
readonly strict?: boolean;
}
}
/**
* 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.
*
* @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.
*
* @example
*
* // data.csv:
* //
* // NAME,AGE
* // Daffy Duck,24
* // Bugs Bunny,22
*
* import csv = require('csv-parser');
* import * as fs from 'fs';
*
* const results = [];
*
* fs.createReadStream('data.csv')
* .pipe(csv())
* .on('data', (data) => results.push(data))
* .on('end', () => {
* console.log(results);
* // [
* // { NAME: 'Daffy Duck', AGE: '24' },
* // { NAME: 'Bugs Bunny', AGE: '22' }
* // ]
* });
*/
declare const csvParser: (
optionsOrHeaders?: csvParser.Options | ReadonlyArray<string>
) => csvParser.CsvParser;
export = csvParser;