index.d.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Original definitions in https://github.com/DefinitelyTyped/DefinitelyTyped by: David Muller <https://github.com/davidm77>
  2. /// <reference types="node" />
  3. import * as stream from "stream";
  4. export type Callback = (err: CsvError | undefined, records: any | undefined, info: Info) => void;
  5. export interface Parser extends stream.Transform {}
  6. export class Parser {
  7. constructor(options: Options);
  8. __push(line: any): any;
  9. __write(chars: any, end: any, callback: any): any;
  10. readonly options: Options
  11. readonly info: Info;
  12. }
  13. export interface CastingContext {
  14. readonly column: number | string;
  15. readonly empty_lines: number;
  16. readonly error: CsvError;
  17. readonly header: boolean;
  18. readonly index: number;
  19. readonly quoting: boolean;
  20. readonly lines: number;
  21. readonly records: number;
  22. readonly invalid_field_length: number;
  23. }
  24. export type CastingFunction = (value: string, context: CastingContext) => any;
  25. export type CastingDateFunction = (value: string, context: CastingContext) => Date;
  26. export type ColumnOption = string | undefined | null | false | { name: string };
  27. /*
  28. Note, could not `extends stream.TransformOptions` because encoding can be
  29. BufferEncoding and undefined as well as null which is not defined in the
  30. extended type.
  31. */
  32. export interface Options {
  33. /**
  34. * If true, the parser will attempt to convert read data types to native types.
  35. * @deprecated Use {@link cast}
  36. */
  37. auto_parse?: boolean | CastingFunction;
  38. autoParse?: boolean | CastingFunction;
  39. /**
  40. * If true, the parser will attempt to convert read data types to dates. It requires the "auto_parse" option.
  41. * @deprecated Use {@link cast_date}
  42. */
  43. auto_parse_date?: boolean | CastingDateFunction;
  44. autoParseDate?: boolean | CastingDateFunction;
  45. /**
  46. * If true, detect and exclude the byte order mark (BOM) from the CSV input if present.
  47. */
  48. bom?: boolean;
  49. /**
  50. * If true, the parser will attempt to convert input string to native types.
  51. * If a function, receive the value as first argument, a context as second argument and return a new value. More information about the context properties is available below.
  52. */
  53. cast?: boolean | CastingFunction;
  54. /**
  55. * If true, the parser will attempt to convert input string to dates.
  56. * If a function, receive the value as argument and return a new value. It requires the "auto_parse" option. Be careful, it relies on Date.parse.
  57. */
  58. cast_date?: boolean | CastingDateFunction;
  59. castDate?: boolean | CastingDateFunction;
  60. /**
  61. * List of fields as an array,
  62. * a user defined callback accepting the first line and returning the column names or true if autodiscovered in the first CSV line,
  63. * default to null,
  64. * affect the result data set in the sense that records will be objects instead of arrays.
  65. */
  66. columns?: ColumnOption[] | boolean | ((record: any) => ColumnOption[]);
  67. /**
  68. * Convert values into an array of values when columns are activated and
  69. * when multiple columns of the same name are found.
  70. */
  71. group_columns_by_name?: boolean;
  72. groupColumnsByName?: boolean;
  73. /**
  74. * Treat all the characters after this one as a comment, default to '' (disabled).
  75. */
  76. comment?: string;
  77. /**
  78. * Restrict the definition of comments to a full line. Comment characters
  79. * defined in the middle of the line are not interpreted as such. The
  80. * option require the activation of comments.
  81. */
  82. comment_no_infix?: boolean;
  83. /**
  84. * Set the field delimiter. One character only, defaults to comma.
  85. */
  86. delimiter?: string | string[] | Buffer;
  87. /**
  88. * Set the source and destination encoding, a value of `null` returns buffer instead of strings.
  89. */
  90. encoding?: BufferEncoding | undefined;
  91. /**
  92. * Set the escape character, one character only, defaults to double quotes.
  93. */
  94. escape?: string | null | false | Buffer;
  95. /**
  96. * Start handling records from the requested number of records.
  97. */
  98. from?: number;
  99. /**
  100. * Start handling records from the requested line number.
  101. */
  102. from_line?: number;
  103. fromLine?: number;
  104. /**
  105. * Don't interpret delimiters as such in the last field according to the number of fields calculated from the number of columns, the option require the presence of the `column` option when `true`.
  106. */
  107. ignore_last_delimiters?: boolean | number;
  108. /**
  109. * Generate two properties `info` and `record` where `info` is a snapshot of the info object at the time the record was created and `record` is the parsed array or object.
  110. */
  111. info?: boolean;
  112. /**
  113. * If true, ignore whitespace immediately following the delimiter (i.e. left-trim all fields), defaults to false.
  114. * Does not remove whitespace in a quoted field.
  115. */
  116. ltrim?: boolean;
  117. /**
  118. * Maximum numer of characters to be contained in the field and line buffers before an exception is raised,
  119. * used to guard against a wrong delimiter or record_delimiter,
  120. * default to 128000 characters.
  121. */
  122. max_record_size?: number;
  123. maxRecordSize?: number;
  124. /**
  125. * Name of header-record title to name objects by.
  126. */
  127. objname?: string;
  128. /**
  129. * Alter and filter records by executing a user defined function.
  130. */
  131. on_record?: (record: any, context: CastingContext) => any;
  132. onRecord?: (record: any, context: CastingContext) => any;
  133. /**
  134. * Optional character surrounding a field, one character only, defaults to double quotes.
  135. */
  136. quote?: string | boolean | Buffer | null;
  137. /**
  138. * Generate two properties raw and row where raw is the original CSV row content and row is the parsed array or object.
  139. */
  140. raw?: boolean;
  141. /**
  142. * Discard inconsistent columns count, default to false.
  143. */
  144. relax_column_count?: boolean;
  145. relaxColumnCount?: boolean;
  146. /**
  147. * Discard inconsistent columns count when the record contains less fields than expected, default to false.
  148. */
  149. relax_column_count_less?: boolean;
  150. relaxColumnCountLess?: boolean;
  151. /**
  152. * Discard inconsistent columns count when the record contains more fields than expected, default to false.
  153. */
  154. relax_column_count_more?: boolean;
  155. relaxColumnCountMore?: boolean;
  156. /**
  157. * Preserve quotes inside unquoted field.
  158. */
  159. relax_quotes?: boolean;
  160. relaxQuotes?: boolean;
  161. /**
  162. * One or multiple characters used to delimit record rows; defaults to auto discovery if not provided.
  163. * Supported auto discovery method are Linux ("\n"), Apple ("\r") and Windows ("\r\n") row delimiters.
  164. */
  165. record_delimiter?: string | string[] | Buffer | Buffer[];
  166. recordDelimiter?: string | string[] | Buffer | Buffer[];
  167. /**
  168. * If true, ignore whitespace immediately preceding the delimiter (i.e. right-trim all fields), defaults to false.
  169. * Does not remove whitespace in a quoted field.
  170. */
  171. rtrim?: boolean;
  172. /**
  173. * Dont generate empty values for empty lines.
  174. * Defaults to false
  175. */
  176. skip_empty_lines?: boolean;
  177. skipEmptyLines?: boolean;
  178. /**
  179. * Skip a line with error found inside and directly go process the next line.
  180. */
  181. skip_records_with_error?: boolean;
  182. skipRecordsWithError?: boolean;
  183. /**
  184. * Don't generate records for lines containing empty column values (column matching /\s*\/), defaults to false.
  185. */
  186. skip_records_with_empty_values?: boolean;
  187. skipRecordsWithEmptyValues?: boolean;
  188. /**
  189. * Stop handling records after the requested number of records.
  190. */
  191. to?: number;
  192. /**
  193. * Stop handling records after the requested line number.
  194. */
  195. to_line?: number;
  196. toLine?: number;
  197. /**
  198. * If true, ignore whitespace immediately around the delimiter, defaults to false.
  199. * Does not remove whitespace in a quoted field.
  200. */
  201. trim?: boolean;
  202. }
  203. export interface Info {
  204. /**
  205. * Count the number of lines being fully commented.
  206. */
  207. readonly comment_lines: number;
  208. /**
  209. * Count the number of processed empty lines.
  210. */
  211. readonly empty_lines: number;
  212. /**
  213. * The number of lines encountered in the source dataset, start at 1 for the first line.
  214. */
  215. readonly lines: number;
  216. /**
  217. * Count the number of processed records.
  218. */
  219. readonly records: number;
  220. /**
  221. * Count of the number of processed bytes.
  222. */
  223. readonly bytes: number;
  224. /**
  225. * Number of non uniform records when `relax_column_count` is true.
  226. */
  227. readonly invalid_field_length: number;
  228. /**
  229. * Normalized verion of `options.columns` when `options.columns` is true, boolean otherwise.
  230. */
  231. readonly columns: boolean | { name: string }[] | { disabled: true }[];
  232. }
  233. export type CsvErrorCode =
  234. 'CSV_INVALID_OPTION_BOM'
  235. | 'CSV_INVALID_OPTION_CAST'
  236. | 'CSV_INVALID_OPTION_CAST_DATE'
  237. | 'CSV_INVALID_OPTION_COLUMNS'
  238. | 'CSV_INVALID_OPTION_GROUP_COLUMNS_BY_NAME'
  239. | 'CSV_INVALID_OPTION_COMMENT'
  240. | 'CSV_INVALID_OPTION_DELIMITER'
  241. | 'CSV_INVALID_OPTION_ON_RECORD'
  242. | 'CSV_INVALID_CLOSING_QUOTE'
  243. | 'INVALID_OPENING_QUOTE'
  244. | 'CSV_INVALID_COLUMN_MAPPING'
  245. | 'CSV_INVALID_ARGUMENT'
  246. | 'CSV_INVALID_COLUMN_DEFINITION'
  247. | 'CSV_MAX_RECORD_SIZE'
  248. | 'CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE'
  249. | 'CSV_QUOTE_NOT_CLOSED'
  250. | 'CSV_RECORD_INCONSISTENT_FIELDS_LENGTH'
  251. | 'CSV_RECORD_INCONSISTENT_COLUMNS'
  252. | 'CSV_OPTION_COLUMNS_MISSING_NAME'
  253. export class CsvError extends Error {
  254. readonly code: CsvErrorCode;
  255. [key: string]: any;
  256. constructor(code: CsvErrorCode, message: string | string[], options?: Options, ...contexts: any[]);
  257. }
  258. declare function parse(input: Buffer | string, options?: Options, callback?: Callback): Parser;
  259. declare function parse(input: Buffer | string, callback?: Callback): Parser;
  260. declare function parse(options?: Options, callback?: Callback): Parser;
  261. declare function parse(callback?: Callback): Parser;
  262. // export default parse;
  263. export { parse }