Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
export-to-csv
Advanced tools
The export-to-csv npm package is a utility that allows you to easily export data to CSV format. It is particularly useful for converting JSON data into CSV files, which can then be downloaded or used in other applications.
Basic CSV Export
This feature allows you to export a basic JSON array to a CSV file. The code sample demonstrates how to configure the export options and generate the CSV data.
const { ExportToCsv } = require('export-to-csv');
const data = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'San Francisco' }
];
const options = {
fieldSeparator: ',',
quoteStrings: '"',
decimalSeparator: '.',
showLabels: true,
showTitle: true,
title: 'My CSV File',
useTextFile: false,
useBom: true,
useKeysAsHeaders: true
};
const csvExporter = new ExportToCsv(options);
const csvData = csvExporter.generateCsv(data);
console.log(csvData);
Custom Headers
This feature allows you to specify custom headers for the CSV file. The code sample shows how to set the headers option to define the column names in the CSV output.
const { ExportToCsv } = require('export-to-csv');
const data = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'San Francisco' }
];
const options = {
fieldSeparator: ',',
quoteStrings: '"',
decimalSeparator: '.',
showLabels: true,
showTitle: true,
title: 'My CSV File',
useTextFile: false,
useBom: true,
headers: ['Name', 'Age', 'City']
};
const csvExporter = new ExportToCsv(options);
const csvData = csvExporter.generateCsv(data);
console.log(csvData);
Download CSV File
This feature allows you to download the generated CSV data as a file. The code sample demonstrates how to use the 'fs' module to write the CSV data to a file.
const { ExportToCsv } = require('export-to-csv');
const fs = require('fs');
const data = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'San Francisco' }
];
const options = {
fieldSeparator: ',',
quoteStrings: '"',
decimalSeparator: '.',
showLabels: true,
showTitle: true,
title: 'My CSV File',
useTextFile: false,
useBom: true,
useKeysAsHeaders: true
};
const csvExporter = new ExportToCsv(options);
const csvData = csvExporter.generateCsv(data);
fs.writeFileSync('my_csv_file.csv', csvData);
The json2csv package is another popular library for converting JSON data to CSV format. It offers a wide range of customization options and supports both synchronous and asynchronous operations. Compared to export-to-csv, json2csv provides more advanced features such as custom field transformations and multi-level JSON support.
The csv-writer package is a simple and easy-to-use library for writing CSV files. It allows you to define the structure of the CSV file and write data to it in a straightforward manner. While it may not offer as many customization options as export-to-csv, it is a good choice for basic CSV writing tasks.
The fast-csv package is a fast and efficient library for parsing and formatting CSV data. It supports both reading and writing CSV files and offers a range of configuration options. Compared to export-to-csv, fast-csv is more focused on performance and is suitable for handling large datasets.
Based off of this library by Javier Telio
Helper library to quickly and easily create a CSV file in browser or Node
yarn add export-to-csv
// npm install --save export-to-csv
import { ExportToCsv } from 'export-to-csv';
var data = [
{
name: 'Test 1',
age: 13,
average: 8.2,
approved: true,
description: "using 'Content here, content here' "
},
{
name: 'Test 2',
age: 11,
average: 8.2,
approved: true,
description: "using 'Content here, content here' "
},
{
name: 'Test 4',
age: 10,
average: 8.2,
approved: true,
description: "using 'Content here, content here' "
},
];
const options = {
fieldSeparator: ',',
quoteStrings: '"',
decimalseparator: '.',
showLabels: true,
showTitle: true,
title: 'My Awesome CSV',
useBom: true,
useKeysAsHeaders: true,
// headers: ['Column 1', 'Column 2', etc...] <-- Won't work with useKeysAsHeaders present!
};
const csvExporter = new ExportToCsv(options);
csvExporter.generateCsv(data);
Option | Default | Description |
---|---|---|
fieldSeparator | , | Defines the field separator character |
filename | 'generated' | Sets the name of the downloaded file. ".csv" will be appended to the value provided. |
quoteStrings | " | If provided, will use this characters to "escape" fields, otherwise will use double quotes as deafult |
decimalseparator | . | Defines the decimal separator character (default is .). If set to "locale", it uses the language sensitive representation of the number. |
showLabels | false | If true, the first row will be the headers option or object keys if useKeysAsHeaders is present |
showTitle | false | Includes the title as the first line in the generated file |
title | 'My Generated Report' | This string will be used as the report title |
useBom | true | If true, adds a BOM character at the start of the CSV to improve file compatibility |
useKeysAsHeaders | false | If true, this will use the keys of the first object in the collection as the column headers |
headers | [] | Expects an array of strings, which if supplied, will be used as the column headers |
Credits and Original Authors |
---|
javiertelioz |
sn123 |
arf1980 |
FAQs
Easily create CSV data from json collection
The npm package export-to-csv receives a total of 93,243 weekly downloads. As such, export-to-csv popularity was classified as popular.
We found that export-to-csv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.