Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
The d3-dsv npm package is designed for parsing and formatting delimiter-separated values, such as CSV and TSV files. It provides a simple and efficient way to work with structured data in text format, making it easier to import, analyze, and export data in web applications.
Parsing CSV strings
This feature allows you to parse a CSV string into an array of objects, where each object represents a row of the CSV, with properties corresponding to column names.
"var d3 = require('d3-dsv');\nvar csvString = 'name,age\nAlice,30\nBob,42';\nvar data = d3.csvParse(csvString);\nconsole.log(data);"
Formatting objects to CSV
This feature enables you to convert an array of objects into a CSV string, where each object in the array represents a row in the CSV and the object properties represent columns.
"var d3 = require('d3-dsv');\nvar data = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 42 }];\nvar csvString = d3.csvFormat(data);\nconsole.log(csvString);"
Parsing TSV strings
Similar to parsing CSV strings, this feature allows for parsing TSV (Tab-Separated Values) strings into an array of objects, facilitating the handling of TSV formatted data.
"var d3 = require('d3-dsv');\nvar tsvString = 'name\tage\nAlice\t30\nBob\t42';\nvar data = d3.tsvParse(tsvString);\nconsole.log(data);"
Formatting objects to TSV
This feature allows for converting an array of objects into a TSV string, making it easy to generate TSV formatted data from JavaScript objects.
"var d3 = require('d3-dsv');\nvar data = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 42 }];\nvar tsvString = d3.tsvFormat(data);\nconsole.log(tsvString);"
Papa Parse is a powerful CSV (Comma Separated Values) parser that can convert CSV files into JSON and back. It is similar to d3-dsv in its ability to handle CSV data but offers a more extensive set of features for handling large files and streaming.
csv-parser is a Node.js module that transforms CSV into JSON at the rate of 90,000 rows per second. It provides a simple and efficient streaming API, making it a good choice for processing large CSV files. It is similar to d3-dsv but focuses more on high-performance streaming.
fast-csv is another npm package for parsing and formatting CSV files. It offers both synchronous and asynchronous APIs and includes features for transforming data. While it shares functionality with d3-dsv, fast-csv emphasizes speed and flexibility in handling CSV data.
A parser and formatter for delimiter-separated values, most commonly comma-separated values (CSV) and tab-separated values (TSV). These tabular formats are popular with spreadsheet programs such as Microsoft Excel, and are often more space-efficient than JSON for large datasets. This implementation is based on RFC 4180.
Supports comma- and tab-separated values out of the box. To define a new delimiter, such as "|"
for pipe-separated values, use the dsv constructor:
var psv = dsv("|");
console.log(psv.parse("foo|bar\n1|2")); // [{foo: "1", bar: "2"}]
If you use NPM, npm install d3-dsv
. Otherwise, download the latest release.
# dsv(delimiter)
Constructs a new DSV parser and formatter for the specified delimiter.
# dsv.parse(string[, row])
Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of objects representing the parsed rows.
Unlike dsv.parseRows, this method requires that the first line of the DSV content contains a delimiter-separated list of column names; these column names become the attributes on the returned objects. For example, consider the following CSV file:
Year,Make,Model,Length
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
The resulting JavaScript array is:
[
{"Year": "1997", "Make": "Ford", "Model": "E350", "Length": "2.34"},
{"Year": "2000", "Make": "Mercury", "Model": "Cougar", "Length": "2.38"}
]
Field values are always strings; they will not be automatically converted to numbers, dates, or other types. In some cases, JavaScript may coerce strings to numbers for you automatically (for example, using the +
operator). By specifying a row conversion function, you can convert the strings to numbers or other specific types, such as dates:
var data = csv.parse(string, function(d) {
return {
year: new Date(+d.Year, 0, 1), // convert "Year" column to Date
make: d.Make,
model: d.Model,
length: +d.Length // convert "Length" column to number
};
});
Using +
rather than parseInt or parseFloat is typically faster, though more restrictive. For example, "30px"
when coerced using +
returns NaN
, while parseInt and parseFloat return 30
.
# dsv.parseRows(string[, row])
Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of arrays representing the parsed rows.
Unlike dsv.parse, this method treats the header line as a standard row, and should be used whenever DSV content does not contain a header. Each row is represented as an array rather than an object. Rows may have variable length. For example, consider the following CSV file, which notably lacks a header line:
1997,Ford,E350,2.34
2000,Mercury,Cougar,2.38
The resulting JavaScript array is:
[
["1997", "Ford", "E350", "2.34"],
["2000", "Mercury", "Cougar", "2.38"]
]
Field values are always strings; they will not be automatically converted to numbers. See dsv.parse for details. An optional row conversion function may be specified as the second argument to convert types and filter rows. For example:
var data = csv.parseRows(string, function(d, i) {
return {
year: new Date(+d[0], 0, 1), // convert first colum column to Date
make: d[1],
model: d[2],
length: +d[3] // convert fourth column to number
};
});
The row function is invoked for each row in the DSV content, being passed the current row’s array of field values (d
) and index (i
) as arguments. The return value of the function replaces the element in the returned array of rows; if the function returns null or undefined, the row is stripped from the returned array of rows. In effect, row is similar to applying a map and filter operator to the returned rows.
# dsv.format(rows)
Formats the specified array of object rows as delimiter-separated values, returning a string. This operation is the inverse of dsv.parse. Each row will be separated by a newline (\n
), and each column within each row will be separated by the delimiter (such as a comma, ,
). Values that contain either the delimiter, a double-quote ("
) or a newline will be escaped using double-quotes.
The header row is determined by the union of all properties on all objects in rows. The order of header columns is nondeterministic. All properties on each row object will be coerced to strings. For more control over which and how fields are formatted, first map rows to an array of array of string, and then use dsv.formatRows.
# dsv.formatRows(rows)
Formats the specified array of array of string rows as delimiter-separated values, returning a string. This operation is the reverse of dsv.parseRows. Each row will be separated by a newline (\n
), and each column within each row will be separated by the delimiter (such as a comma, ,
). Values that contain either the delimiter, a double-quote (") or a newline will be escaped using double-quotes.
To convert an array of objects to an array of arrays while explicitly specifying the columns, use array.map. For example:
var string = csv.formatRows(data.map(function(d, i) {
return [
d.year.getFullYear(), // Assuming d.year is a Date object.
d.make,
d.model,
d.length
];
}));
If you like, you can also array.concat this result with an array of column names to generate the first row:
var string = csv.formatRows([[
"year",
"make",
"model",
"length"
]].concat(data.map(function(d, i) {
return [
d.year.getFullYear(), // Assuming d.year is a Date object.
d.make,
d.model,
d.length
];
})));
# csv
A parser and formatter for comma-separated values (CSV), defined as:
var csv = dsv(",");
# tsv
A parser and formatter for tab-separated values (TSV), defined as:
var tsv = dsv("\t");
If a content security policy is in place, note that dsv.parse requires unsafe-eval
in the script-src
directive, due to the (safe) use of dynamic code generation for fast parsing. (See source.) Alternatively, use dsv.parseRows.
The d3-dsv module comes with a few binaries to convert DSV files:
These programs either take a single file as an argument or read from stdin, and write to stdout. For example, these statements are all equivalent:
csv2json file.csv > file.json
csv2json < file.csv > file.json
cat file.csv | csv2json - > file.json
FAQs
A parser and formatter for delimiter-separated values, such as CSV and TSV
We found that d3-dsv demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.