### Install csv-parser with yarn Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Install the csv-parser module using yarn. This is an alternative package manager for Node.js projects. ```console $ yarn add csv-parser ``` -------------------------------- ### Install csv-parser with npm Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Install the csv-parser module using npm. This is the standard way to add packages to your Node.js project. ```console $ npm install csv-parser ``` -------------------------------- ### Rename Columns Example Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/mapHeaders.test.js.md Demonstrates renaming columns using the mapHeaders option. The first row shows the result of mapping 'x' to 'a', 'y' to 'b', and 'z' to 'c'. ```javascript { x: '1', y: '2', z: '3', } ``` -------------------------------- ### Skip Columns Example Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/mapHeaders.test.js.md Demonstrates skipping columns 'a' and 'c' using the mapHeaders option. The first row shows the result, retaining only column 'b'. ```javascript { b: '2', } ``` -------------------------------- ### Map Values to Lowercase Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Use the 'mapValues' option to transform the content of each column. This example converts all values to lowercase. ```javascript csv({ mapValues: ({ header, index, value }) => value.toLowerCase() }) ``` -------------------------------- ### Map Headers to Lowercase Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Use the 'mapHeaders' option to transform header names. This example converts all headers to lowercase. ```javascript csv({ mapHeaders: ({ header, index }) => header.toLowerCase() }) ``` -------------------------------- ### Parse TSV File using CLI Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Example of using the CLI to parse a Tab-Separated Values (TSV) file by specifying the tab character as the separator. ```bash cat data.tsv | csv-parser -s $' ' ``` -------------------------------- ### Backtick Separator Example Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/issues.test.js.md Demonstrates handling of CSV data with backtick separators, as addressed in issue #105. The output shows parsed lines as JavaScript objects. ```javascript [ { p_desc: 'Bulbasaur can be seen napping', pokemon_id: '1' }, { p_desc: 'There is a bud on this', pokemon_id: '2' } ] ``` -------------------------------- ### Strict Mode with Skipped Lines Example Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/issues.test.js.md Illustrates the behavior of strict parsing combined with skipped lines, as resolved in issue #136. The output displays parsed CSV rows as objects with header keys. ```javascript [ { h1: '1', h2: '2', h3: '3' }, { h1: '4', h2: '5', h3: '6' }, { h1: '7', h2: '8', h3: '9' } ] ``` -------------------------------- ### String Support Test Output Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md This snippet shows the expected output for a test case involving string data, resulting in a simple JSON object. ```json {"hello":"world"} ``` -------------------------------- ### Benchmark Results Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Displays the performance benchmarks for csv-parser across various CSV file types and configurations. This can help in understanding the module's speed under different conditions. ```console → npm run bench Filename Rows Parsed Duration backtick.csv 2 3.5ms bad-data.csv 3 0.55ms basic.csv 1 0.26ms comma-in-quote.csv 1 0.29ms comment.csv 2 0.40ms empty-columns.csv 1 0.40ms escape-quotes.csv 3 0.38ms geojson.csv 3 0.46ms large-dataset.csv 7268 73ms newlines.csv 3 0.35ms no-headers.csv 3 0.26ms option-comment.csv 2 0.24ms option-escape.csv 3 0.25ms option-maxRowBytes.csv 4577 39ms option-newline.csv 0 0.47ms option-quote-escape.csv 3 0.33ms option-quote-many.csv 3 0.38ms option-quote.csv 2 0.22ms quotes+newlines.csv 3 0.20ms strict.csv 3 0.22ms latin.csv 2 0.38ms mac-newlines.csv 2 0.28ms utf16-big.csv 2 0.33ms utf16.csv 2 0.26ms utf8.csv 2 0.24ms ``` -------------------------------- ### Indexes Output Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/mapValues.test.js.md This snippet shows the indexes corresponding to the CSV columns. ```javascript [ 0, 1, 2, ] ``` -------------------------------- ### Handle Headers Event Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Listen for the 'headers' event to access parsed header names. This is useful for dynamic processing based on column titles. ```js fs.createReadStream('data.csv') .pipe(csv()) .on('headers', (headers) => { console.log(`First header: ${headers[0]}`) }) ``` -------------------------------- ### Basic CSV Parsing Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Parse a CSV file by piping a readable stream to the csv-parser. The results are collected in an array and logged when the stream ends. ```js const csv = require('csv-parser') const fs = require('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' } // ] }); ``` -------------------------------- ### csv([options | headers]) Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Parses CSV data. It can accept an options object or an array of headers. The function returns an array of objects, where each object represents a row in the CSV. ```APIDOC ## csv([options | headers]) ### Description Parses CSV data. It can accept an options object or an array of headers. The function returns an array of objects, where each object represents a row in the CSV. ### Parameters #### options Type: `Object` As an alternative to passing an `options` object, you may pass an `Array[String]` which specifies the headers to use. For example: ```js csv(['Name', 'Age']); ``` If you need to specify options _and_ headers, please use the the object notation with the `headers` property as shown below. - **escape** (String) - Default: `"` - A single-character string used to specify the character used to escape strings in a CSV row. - **headers** (Array[String] | Boolean) - 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 column index as the key for each column. - **mapHeaders** (Function) - A function that can be used to modify the values of each header. Return a `String` to modify the header. Return `null` to remove the header, and it's column, from the results. ##### Parameters - **header** (String) - The current column header. - **index** (Number) - The current column index. - **mapValues** (Function) - A function that can be used to modify the content of each column. The return value will replace the current column content. ##### Parameters - **header** (String) - The current column header. - **index** (Number) - The current column index. - **value** (String) - The current column value (or content). - **newline** (String) - Default: `\n` - Specifies a single-character string to denote the end of a line in a CSV file. - **quote** (String) - Default: `"` - Specifies a single-character string to denote a quoted string. - **raw** (Boolean) - If `true`, instructs the parser not to decode UTF-8 strings. - **separator** (String) - Default: `,` - Specifies a single-character string to use as the column separator for each row. - **skipComments** (Boolean | String) - Default: `false` - Instructs the parser to ignore lines which represent comments in a CSV file. 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. - **skipLines** (Number) - Default: `0` - Specifies the number of lines at the beginning of a data file that the parser should skip over, prior to parsing headers. - **maxRowBytes** (Number) - Default: `Number.MAX_SAFE_INTEGER` - Maximum number of bytes per row. An error is thrown if a line exceeds this value. - **strict** (Boolean) - Default: `false` - If `true`, instructs the parser that the number of columns in each row must match the number of `headers` specified or throws an exception. - **outputByteOffset** (Boolean) - Default: `false` - If `true`, instructs the parser to emit each row with a `byteOffset` property. The byteOffset represents the offset in bytes of the beginning of the parsed row in the original stream. Will change the output format of stream to be `{ byteOffset, row }`. ### Returns `Array[Object]` ``` -------------------------------- ### Headers Option - Snapshot 1 Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/headers.test.js.md Represents the parsed CSV data when the 'headers' option is used, showing an array of objects with keys derived from the header row. ```json [ { a: '1', b: '2', c: '3', }, { a: '4', b: '5', c: '6', }, { a: '7', b: '8', c: '9', }, ] ``` -------------------------------- ### Binary Sanity Test Output Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md This snippet shows the expected output for a binary sanity test, representing a simple JSON object. ```json {"a":"1"} ``` -------------------------------- ### Custom Escape Character - First Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/headers.test.js.md Demonstrates parsing with a custom escape character, showing an 'escaped' value in the second column. ```json { a: '1', b: 'some "escaped" value', c: '2', } ``` -------------------------------- ### Headers: false - Snapshot 1 Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/headers.test.js.md Displays the parsed CSV data when the 'headers' option is set to false, resulting in an array of objects with numeric keys corresponding to column indices. ```json [ { 0: 'a', 1: 'b', 2: 'c', }, { 0: '1', 1: '2', 2: '3', }, { 0: '4', 1: '5', 2: '6', }, { 0: '7', 1: '8', 2: '9', 3: '10', }, ] ``` -------------------------------- ### First Row Output Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/mapValues.test.js.md This snippet shows the structure of the first row parsed by the csv-parser. ```javascript { a: 1, b: 2, c: 3, } ``` -------------------------------- ### Specify Headers with Array Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Use an array of strings to define the headers for the CSV data. This is a shorthand for the 'headers' option. ```javascript csv(['Name', 'Age']); ``` -------------------------------- ### Headers Output Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/mapValues.test.js.md This snippet displays the headers extracted from the CSV data. ```javascript [ 'a', 'b', 'c', ] ``` -------------------------------- ### Simple CSV Row with Byte Offset Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/byteOffset.test.js.md Retrieves the byte offset and row data for the first row in a simple CSV. ```javascript { byteOffset: 6, row: { a: '1', b: '2', c: '3', }, } ``` -------------------------------- ### CSV Parsing with fewer columns (strict: false) Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/strictNo.test.js.md Demonstrates how the CSV parser handles rows with fewer columns than the header when strict mode is disabled. The output shows the parsed object for the first, a broken, and the last row. ```javascript { a: '1', b: '2', c: '3', } ``` ```javascript { a: '4', b: '5', } ``` ```javascript { a: '6', b: '7', c: '8', } ``` -------------------------------- ### CSV Parsing: Newlines with CRLF Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Demonstrates parsing CSV with newlines and CRLF line endings. ```json [{"a":"1","b":"2","c":"3"},{"a":"Once upon ␍␊ a time","b":"5","c":"6"},{"a":"7","b":"8","c":"9"}] ``` -------------------------------- ### Large Dataset First Row with Byte Offset Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/byteOffset.test.js.md Retrieves the byte offset and row data for the first row in a large dataset CSV. ```javascript { byteOffset: 85, row: { depth: '100', dmin: '', gap: '', id: 'ak12293661', latitude: '59.9988', longitude: '-152.7191', mag: '3', magType: 'ml', net: 'ak', nst: '', place: '54km S of Redoubt Volcano, Alaska', rms: '0.54', time: '2015-12-22T18:45:11.000Z', type: 'earthquake', updated: '2015-12-22T19:09:29.736Z', }, } ``` -------------------------------- ### CSV Parsing: Empty Fields Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Shows the output when a CSV contains empty fields. ```json [{"a":"1","b":"","c":""},{"a":"2","b":"3","c":"4"}] ``` -------------------------------- ### Comment Snapshot Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/skipComments.test.js.md This snapshot shows the parsed output when comments are skipped. It represents an array of objects, where each object corresponds to a row in the CSV. ```json [ { a: '1', b: '2', c: '3', }, ] ``` -------------------------------- ### Custom Escape Character - Second Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/headers.test.js.md Illustrates parsing with a custom escape character, showing an empty string represented by double quotes. ```json { a: '3', b: '""', c: '4', } ``` -------------------------------- ### CSV Parsing: Simple Case Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Represents the output of a basic, straightforward CSV parsing operation. ```json [{"a":"1","b":"2","c":"3"}] ``` -------------------------------- ### CSV Parsing with more columns (strict: false) Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/strictNo.test.js.md Illustrates the CSV parser's behavior with rows containing more columns than the header when strict mode is off. The output includes the parsed object for the first, a broken, and the last row. ```javascript { a: '1', b: '2', c: '3', } ``` ```javascript { _3: '7', a: '4', b: '5', c: '6', } ``` ```javascript { a: '8', b: '9', c: '10', } ``` -------------------------------- ### Large Dataset Second Row with Byte Offset Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/byteOffset.test.js.md Retrieves the byte offset and row data for the second row in a large dataset CSV. ```javascript { byteOffset: 231, row: { depth: '65.4', dmin: '', gap: '', id: 'ak12293651', latitude: '62.9616', longitude: '-148.7532', mag: '1.9', magType: 'ml', net: 'ak', nst: '', place: '48km SSE of Cantwell, Alaska', rms: '0.51', time: '2015-12-22T18:38:34.000Z', type: 'earthquake', updated: '2015-12-22T18:47:23.287Z', }, } ``` -------------------------------- ### Handle CSV without Headers Source: https://github.com/mafintosh/csv-parser/blob/master/README.md When the CSV data does not contain a header row, set 'headers' to false. Column indices will be used as keys. ```javascript csv([ { '0': 'Daffy Duck', '1': 24 }, { '0': 'Bugs Bunny', '1': 22 } ]) ``` -------------------------------- ### CSV Parser Output with Numeric Column Names Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/escape.test.js.md This snippet shows the expected output of the CSV parser when the 'headers' option is set to false and column names are numeric. It represents an array of objects, where keys are numeric indices corresponding to columns. ```javascript [ { 0: 'a', 1: 'b', 2: 'c', }, { 0: '1', 1: '2', 2: '3', }, ] ``` -------------------------------- ### Newlines in Cell: Second Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Shows a row containing a field with a newline character, demonstrating how it's preserved. ```json {"a":"Once upon ␊ a time","b":"5","c":"6"} ``` -------------------------------- ### CSV Parsing: Quotes and Newlines Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Shows how CSV handles fields containing both quotes and newlines. ```json [{"a":"1","b":"ha ␊ \"ha\" ␊ ha"},{"a":"3","b":"4"}] ``` -------------------------------- ### Raw Escaped Quotes and Newlines: First Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Illustrates parsing a field that contains both escaped quotes and newline characters. ```json {"a":"1","b":"ha ␊ \"ha\" ␊ ha"} ``` -------------------------------- ### Raw Escaped Quotes: First Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Demonstrates parsing a row with a field containing escaped double quotes. ```json {"a":"1","b":"ha \"ha\" ha"} ``` -------------------------------- ### CSV Parser CLI Usage Source: https://github.com/mafintosh/csv-parser/blob/master/README.md The CLI converts CSV to newline-delimited JSON. Use flags to customize parsing behavior like separators and quotes. ```bash Usage: csv-parser [filename?] [options] --escape,-e Set the escape character (defaults to quote value) --headers,-h Explicitly specify csv headers as a comma separated list --help Show this help --output,-o Set output file. Defaults to stdout --quote,-q Set the quote character ("" by default) --remove Remove columns from output by header name --separator,-s Set the separator character ("," by default) --skipComments,-c Skip CSV comments that begin with '#'. Set a value to change the comment character. --skipLines,-l Set the number of lines to skip to before parsing headers --strict Require column length match headers length --version,-v Print out the installed version ``` -------------------------------- ### First Row Parsed Object Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/newline.test.js.md Represents the first row parsed from the CSV data. ```json { a: '1', b: '2', c: '3', } ``` -------------------------------- ### CSV Parsing: Escaped Quotes Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Demonstrates how escaped quotes within a CSV field are handled. ```json [{"a":"1","b":"ha \"ha\" ha"},{"a":"3","b":"4"}] ``` -------------------------------- ### CSV Parsing: UTF-8 Characters Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Demonstrates the parser's ability to handle UTF-8 characters in CSV fields. ```json [{"a":"1","b":"2","c":"3"},{"a":"4","b":"5","c":"ʤ"}] ``` -------------------------------- ### CSV Parsing with Custom Separator Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Configure the csv-parser to use a different separator, such as a tab character, by passing an options object to the csv function. ```js csv({ separator: '\t' }); ``` -------------------------------- ### Raw Escaped Quotes and Newlines: Second Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Shows a field with escaped quotes and newlines, including leading/trailing whitespace around the quotes. ```json {"a":"2","b":" ␊ \"\" ␊ "} ``` -------------------------------- ### Custom Quote Character - First Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/quote.test.js.md Parses the first row using a custom quote character, with a standard field value. ```json { a: '1', b: 'some value', c: '2' } ``` -------------------------------- ### CSV Parsing: Newlines in Fields Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Illustrates parsing CSV where fields contain newline characters. ```json [{"a":"1","b":"2","c":"3"},{"a":"Once upon ␊ a time","b":"5","c":"6"},{"a":"7","b":"8","c":"9"}] ``` -------------------------------- ### CSV Parsing: JSON Field Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Shows a CSV where one field contains a JSON string. ```json [{"key":"1","val":"{\"type\": \"Point\", \"coordinates\": [102.0, 0.5]}"}] ``` -------------------------------- ### Custom Quote and Escape Character - First Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/quote.test.js.md Parses the first row with custom quote and escape characters, showing an escaped single quote within a field. ```json { a: '1', b: 'some \'escaped\' value', c: '2' } ``` -------------------------------- ### Newlines in Cell: First Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Represents the first row of data where fields do not contain special characters or newlines. ```json {"a":"1","b":"2","c":"3"} ``` -------------------------------- ### Custom Quote Character - Second Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/quote.test.js.md Parses the second row using a custom quote character, with simple numeric string values. ```json { a: '3', b: '4', c: '5' } ``` -------------------------------- ### Custom Quote and Escape Character - Second Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/quote.test.js.md Parses the second row with custom quote and escape characters, demonstrating an empty quoted field represented by two single quotes. ```json { a: '3', b: '\'\'', c: '4' } ``` -------------------------------- ### Custom Escape Character - Third Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/headers.test.js.md Shows a standard row without special escaping, indicating normal data parsing. ```json { a: '5', b: '6', c: '7', } ``` -------------------------------- ### Second Row Parsed Object Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/newline.test.js.md Represents the second row parsed from the CSV data, including a string value. ```json { a: 'X-Men', b: '5', c: '6', } ``` -------------------------------- ### Raw Escaped Quotes: Third Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Represents a row with a simple field, following rows with escaped quotes. ```json {"a":"3","b":"4"} ``` -------------------------------- ### Raw Escaped Quotes: Second Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Shows a row where a field contains consecutive escaped double quotes. ```json {"a":"2","b":"\"\""} ``` -------------------------------- ### Remove BOM using sed Source: https://github.com/mafintosh/csv-parser/blob/master/README.md This command-line snippet removes a Byte Order Mark from a CSV file using 'sed'. It's useful for preparing files before piping them to other tools. ```bash $ sed $'s/\xEF\xBB\xBF//g' data.csv ``` -------------------------------- ### Strip BOM from CSV Stream Source: https://github.com/mafintosh/csv-parser/blob/master/README.md Use this snippet to remove a Byte Order Mark from a CSV file stream before parsing. It requires the 'strip-bom-stream' module. ```javascript const fs = require('fs'); const csv = require('csv-parser'); const stripBom = require('strip-bom-stream'); fs.createReadStream('data.csv') .pipe(stripBom()) .pipe(csv()) ... ``` -------------------------------- ### CSV Parsing: Comma in Quotes Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Demonstrates parsing a CSV row where a comma is present within a quoted field. ```json [{"address":"120 any st.","city":"Anytown, WW","first":"John","last":"Doe","zip":"08123"}] ``` -------------------------------- ### Newlines in Cell: Fourth Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/test.js.md Represents the fourth row of data, similar to the first, without special characters. ```json {"a":"7","b":"8","c":"9"} ``` -------------------------------- ### Third Row Parsed Object Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/newline.test.js.md Represents the third row parsed from the CSV data. ```json { a: '7', b: '8', c: '9', } ``` -------------------------------- ### Custom Quote and Escape Character - Third Row Source: https://github.com/mafintosh/csv-parser/blob/master/test/snapshots/quote.test.js.md Parses the third row with custom quote and escape characters, showing a simple field without special characters. ```json { a: '5', b: '6', c: '7' } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.