index.d.ts 4.5 KB

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