Registry / data / d3-dsv

d3-dsv

JSON →
library3.0.1jsnpmunverified

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.

npm install d3-dsv
INSTALL
IMPORT
SIG · D3-DSV
D
d3-dsv
datajavascriptv3.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

csvParse
import { csvParse } from 'd3-dsv';
const { csvParse } = require('d3-dsv');
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.
tsvFormat
import { tsvFormat } from 'd3-dsv';
import * as d3dsv from 'd3-dsv'; d3dsv.tsvFormat(...);
Most d3-dsv functions are named exports; avoid default imports or importing the entire module namespace unless explicitly needed.
dsvFormat
import { dsvFormat } from 'd3-dsv';
Use `dsvFormat` to create custom parser/formatter instances for delimiters other than comma or tab. For example: `const psv = dsvFormat('|');`
autoType
import { autoType } from 'd3-dsv';
The `autoType` function is essential for automatically converting parsed string values into numbers, booleans, or dates, improving data utility.

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.

import { csvParse, tsvParse, dsvFormat, autoType, csvFormat } from 'd3-dsv'; // Sample CSV data const csvString = 'name,age,city\nAlice,30,New York\nBob,24,London'; const tsvString = 'id\tvalue\n1\tapple\n2\tbanana'; const psvString = 'product|price|inStock\nLaptop|1200|true\nMouse|25|false'; console.log('--- CSV Parsing ---'); const parsedCsv = csvParse(csvString); console.log('Parsed CSV (string values):', parsedCsv); // [{ name: 'Alice', age: '30', city: 'New York' }, { name: 'Bob', age: '24', city: 'London' }, columns: ['name', 'age', 'city']] const parsedCsvAutoType = csvParse(csvString, autoType); console.log('Parsed CSV (auto-typed):', parsedCsvAutoType); // [{ name: 'Alice', age: 30, city: 'New York' }, { name: 'Bob', age: 24, city: 'London' }, columns: ['name', 'age', 'city']] console.log('\n--- TSV Parsing ---'); const parsedTsv = tsvParse(tsvString); console.log('Parsed TSV:', parsedTsv); // [{ id: '1', value: 'apple' }, { id: '2', value: 'banana' }, columns: ['id', 'value']] console.log('\n--- Custom DSV (Pipe-Separated) Parsing ---'); const psv = dsvFormat('|'); const parsedPsv = psv.parse(psvString, autoType); console.log('Parsed PSV (auto-typed):', parsedPsv); // [{ product: 'Laptop', price: 1200, inStock: true }, { product: 'Mouse', price: 25, inStock: false }, columns: ['product', 'price', 'inStock']] console.log('\n--- CSV Formatting ---'); const dataToFormat = [ { name: 'Charlie', age: 45, city: 'Paris' }, { name: 'Diana', age: 38, city: 'Rome' } ]; const formattedCsv = csvFormat(dataToFormat); console.log('Formatted CSV:', formattedCsv); // "name,age,city\nCharlie,45,Paris\nDiana,38,Rome" const formattedPsv = psv.format(dataToFormat); console.log('Formatted PSV:', formattedPsv); // "name|age|city\nCharlie|45|Paris\nDiana|38|Rome"
Debug
Known issues
breakingd3-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.
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.
affects: >=3.0.0
breakingd3-dsv v2.0.0 adopted ES2015 language features (e.g., `for-of` loops) and dropped support for older browser environments, including Internet Explorer.
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.
affects: >=2.0.0
gotchaThe `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.
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`.
affects: All
gotchaIn 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.
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.
affects: <1.1.2
Errors
Common errors & fixes
TypeError: d3_dsv_1.csvParse is not a function
Attempting to use CommonJS `require()` syntax or an older bundler configuration with d3-dsv v3.x, which is a pure ES module.
fix
Change your import statement to `import { csvParse } from 'd3-dsv';`. Ensure your environment (Node.js or bundler) is configured for ES modules.
ReferenceError: d3 is not defined
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).
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`.
SyntaxError: Unexpected token 'for'
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.
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.
Upgrade
Version history
3.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
12
OpenAI (training)
2
Resources
d3-dsv — npm install d3-dsv · libregistry