{"id":13055,"library":"d3-dsv","title":"d3-dsv: Delimiter-Separated Values Parser and Formatter","description":"d3-dsv is a robust JavaScript module from the D3.js ecosystem designed for parsing and formatting delimiter-separated values, primarily CSV (Comma-Separated Values) and TSV (Tab-Separated Values). The current stable version is 3.0.1, part of the d3 monorepo, which generally sees stable, infrequent major releases focusing on ecosystem alignment and modern JavaScript features. It adheres to RFC 4180 for CSV parsing, ensuring reliable data handling. Key features include direct parsing functions like `csvParse` and `tsvParse`, as well as a flexible `dsvFormat` method for handling arbitrary delimiters. A notable differentiator is its `autoType` function, introduced in v1.1.0, which automatically infers and converts string values to appropriate data types (e.g., numbers, dates) during parsing, significantly simplifying data preparation. The module is lightweight and offers both row-level and full-document parsing and formatting capabilities, making it suitable for both client-side browser applications (via ESM or UMD bundles) and server-side Node.js environments. Since version 3.0.0, it has adopted ES modules, requiring Node.js 12 or higher.","status":"active","version":"3.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/d3/d3-dsv","tags":["javascript","d3","d3-module","dsv","csv","tsv"],"install":[{"cmd":"npm install d3-dsv","lang":"bash","label":"npm"},{"cmd":"yarn add d3-dsv","lang":"bash","label":"yarn"},{"cmd":"pnpm add d3-dsv","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v3.0.0, d3-dsv is an ES module. CommonJS `require` is not supported for direct module import. Use dynamic import or an ES module bundler.","wrong":"const { csvParse } = require('d3-dsv');","symbol":"csvParse","correct":"import { csvParse } from 'd3-dsv';"},{"note":"Most d3-dsv functions are named exports; avoid default imports or importing the entire module namespace unless explicitly needed.","wrong":"import * as d3dsv from 'd3-dsv'; d3dsv.tsvFormat(...);","symbol":"tsvFormat","correct":"import { tsvFormat } from 'd3-dsv';"},{"note":"Use `dsvFormat` to create custom parser/formatter instances for delimiters other than comma or tab. For example: `const psv = dsvFormat('|');`","symbol":"dsvFormat","correct":"import { dsvFormat } from 'd3-dsv';"},{"note":"The `autoType` function is essential for automatically converting parsed string values into numbers, booleans, or dates, improving data utility.","symbol":"autoType","correct":"import { autoType } from 'd3-dsv';"}],"quickstart":{"code":"import { csvParse, tsvParse, dsvFormat, autoType, csvFormat } from 'd3-dsv';\n\n// Sample CSV data\nconst csvString = 'name,age,city\\nAlice,30,New York\\nBob,24,London';\nconst tsvString = 'id\\tvalue\\n1\\tapple\\n2\\tbanana';\nconst psvString = 'product|price|inStock\\nLaptop|1200|true\\nMouse|25|false';\n\nconsole.log('--- CSV Parsing ---');\nconst parsedCsv = csvParse(csvString);\nconsole.log('Parsed CSV (string values):', parsedCsv);\n// [{ name: 'Alice', age: '30', city: 'New York' }, { name: 'Bob', age: '24', city: 'London' }, columns: ['name', 'age', 'city']]\n\nconst parsedCsvAutoType = csvParse(csvString, autoType);\nconsole.log('Parsed CSV (auto-typed):', parsedCsvAutoType);\n// [{ name: 'Alice', age: 30, city: 'New York' }, { name: 'Bob', age: 24, city: 'London' }, columns: ['name', 'age', 'city']]\n\nconsole.log('\\n--- TSV Parsing ---');\nconst parsedTsv = tsvParse(tsvString);\nconsole.log('Parsed TSV:', parsedTsv);\n// [{ id: '1', value: 'apple' }, { id: '2', value: 'banana' }, columns: ['id', 'value']]\n\nconsole.log('\\n--- Custom DSV (Pipe-Separated) Parsing ---');\nconst psv = dsvFormat('|');\nconst parsedPsv = psv.parse(psvString, autoType);\nconsole.log('Parsed PSV (auto-typed):', parsedPsv);\n// [{ product: 'Laptop', price: 1200, inStock: true }, { product: 'Mouse', price: 25, inStock: false }, columns: ['product', 'price', 'inStock']]\n\nconsole.log('\\n--- CSV Formatting ---');\nconst dataToFormat = [\n  { name: 'Charlie', age: 45, city: 'Paris' },\n  { name: 'Diana', age: 38, city: 'Rome' }\n];\nconst formattedCsv = csvFormat(dataToFormat);\nconsole.log('Formatted CSV:', formattedCsv);\n// \"name,age,city\\nCharlie,45,Paris\\nDiana,38,Rome\"\n\nconst formattedPsv = psv.format(dataToFormat);\nconsole.log('Formatted PSV:', formattedPsv);\n// \"name|age|city\\nCharlie|45|Paris\\nDiana|38|Rome\"\n","lang":"typescript","description":"Demonstrates parsing CSV, TSV, and custom delimiter-separated values (PSV) both with and without `autoType` for automatic type inference. Also shows how to format data back into CSV and custom DSV strings using various functions."},"warnings":[{"fix":"Migrate your project to use ES module `import` syntax. If using Node.js, ensure your `package.json` specifies `\"type\": \"module\"` or use `.mjs` file extensions. Update Node.js to version 12 or higher.","message":"d3-dsv v3.0.0 and newer are pure ES modules (`\"type\": \"module\"`). They no longer provide a CommonJS entry point for `require()`. Node.js 12 or higher is also required.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"For projects requiring support for pre-ES2015 environments (like IE), stick to d3-dsv v1.x or use a transpilation step (e.g., Babel) to downlevel the JavaScript.","message":"d3-dsv v2.0.0 adopted ES2015 language features (e.g., `for-of` loops) and dropped support for older browser environments, including Internet Explorer.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"If encountering CSP errors related to `eval`, consider adjusting your CSP to include `script-src 'unsafe-eval'`. Alternatively, manually parse and type-convert data if `autoType` is not critical or if you prefer a stricter CSP without `unsafe-eval`.","message":"The `d3.csvParse` and `d3.tsvParse` functions, when used with the `row` accessor function (especially `d3.autoType`), may require an `unsafe-eval` Content Security Policy (CSP) directive in browser environments.","severity":"gotcha","affected_versions":"All"},{"fix":"Update to d3-dsv v1.1.2 or later to ensure consistent parsing behavior where missing trailing columns are represented as empty strings. If using older versions, explicitly handle `undefined` values during data processing.","message":"In versions prior to 1.1.2, missing trailing columns in DSV strings could result in `undefined` fields. This was fixed to return empty strings for consistency.","severity":"gotcha","affected_versions":"<1.1.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to `import { csvParse } from 'd3-dsv';`. Ensure your environment (Node.js or bundler) is configured for ES modules.","cause":"Attempting to use CommonJS `require()` syntax or an older bundler configuration with d3-dsv v3.x, which is a pure ES module.","error":"TypeError: d3_dsv_1.csvParse is not a function"},{"fix":"If in a modern browser using ES modules, use `import { csvParse } from 'd3-dsv';`. If in a legacy browser, ensure you load the UMD bundle (e.g., from a CDN) via a `<script>` tag *before* attempting to use `d3.csvParse`.","cause":"Using the UMD global `d3` in an environment where it hasn't been loaded (e.g., modern browser `script type=\"module\"` without the UMD bundle, or Node.js without proper setup).","error":"ReferenceError: d3 is not defined"},{"fix":"Either upgrade your browser environment, use a transpilation tool (like Babel) to convert ES2015+ syntax to ES5 for wider compatibility, or downgrade to d3-dsv v1.x if older browser support is critical and transpilation is not an option.","cause":"Running d3-dsv v2.0.0 or later in an older browser environment (like Internet Explorer) that does not support ES2015 language features such as `for-of` loops.","error":"SyntaxError: Unexpected token 'for'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}