Giter Club home page Giter Club logo

export-to-csv's Introduction

export-to-csv | Export to CSV Mini Library

Small, simple and single purpose. Zero dependencies, functionally inspired and fairly well typed.

If you're looking for a fully CSV compliant, consistently maintained, whole-package library, I'd recommend looking elsewhere! (see alternatives section below)

If you want a lightweight, stable, easy-to-use basic CSV generation and download library, feel free to install.

Installation

npm install --save export-to-csv

Usage

This library was written with TypeScript in mind, so the examples will be in TS.

You can easily use this library in JavaScript as well. The bundle is using ES6 modules, which all modern browsers support.

You can also look at the integration tests for browser/JS use, and the unit tests to understand how the library functions.

import { mkConfig, generateCsv, download } from "export-to-csv";

// mkConfig merges your options with the defaults
// and returns WithDefaults<ConfigOptions>
const csvConfig = mkConfig({ useKeysAsHeaders: true });

const mockData = [
  {
    name: "Rouky",
    date: "2023-09-01",
    percentage: 0.4,
    quoted: '"Pickles"',
  },
  {
    name: "Keiko",
    date: "2023-09-01",
    percentage: 0.9,
    quoted: '"Cactus"',
  },
];

// Converts your Array<Object> to a CsvOutput string based on the configs
const csv = generateCsv(csvConfig)(mockData);

// Get the button in your HTML
const csvBtn = document.querySelector("#csv");

// Add a click handler that will run the `download` function.
// `download` takes `csvConfig` and the generated `CsvOutput`
// from `generateCsv`.
csvBtn.addEventListener("click", () => download(csvConfig)(csv));

API

Option Default Type Description
fieldSeparator "," string Defines the field separator character
filename "generated" string Sets the name of the file creates from the download function
quoteStrings false boolean Determines whether or not to quote strings (using quoteCharacter's value). Whether or not this is set, \r, \n, and fieldSeparator will be quoted.
quoteCharacter '"' string Sets the quote character to use.
decimalSeparator "." string Defines the decimal separator character (default is .). If set to "locale", it uses the language sensitive representation of the number.
showTitle false boolean Sets whether or not to add the value of title to the start of the CSV. (This is not supported by all CSV readers)
title "My Generated Report" string The title to display as the first line of the CSV file. (This is not the name of the file [see filename])
showColumnHeaders true boolean Determines if columns should have headers. When set to false, the first row of the CSV will be data.
columnHeaders [] Array Use this option if column/header order is important! Determines the headers to use as the first line of the CSV data. Items in the array must match key names of objects in your input collection.
useKeysAsHeaders false boolean If set, the CSV will use the key names in your collection as headers. Warning: headers recommended for large collections. If set, it'll override the headers option. Column/header order also not guaranteed. Use headers only if order is important!
boolDisplay {true: "TRUE", false: "FALSE"} {true: string, false: string} Determines how to display boolean values in the CSV. This only works for true and false. 1 and 0 will not be coerced and will display as 1 and 0.
useBom true boolean Adds a byte order mark which is required by Excel to display CSVs, despite is not being necessary with UTF-8 ๐Ÿคทโ€โ™‚๏ธ
useTextFile false boolean Will download the file as text/plain instead of text/csv and use a .txt vs .csv file-extension.

Alternatives

As mentioned above, this library is intentionally small and was designed to solve a very simple need. It was not originally designed to be fully CSV compliant, so many things you need might be missing. I'm also not the most active on it (~7 year gap between updates). So, here are some alternatives with more support and that might be more fully featured.

Thanks!

This library was originally based off of this library by Javier Telio

Credits and Original Authors
javiertelioz
sn123
arf1980

export-to-csv's People

Contributors

alexcaza avatar javiertelioz avatar sn123 avatar devlavshah avatar hukacode avatar jmann-tw avatar makotoishida avatar ahmed-anas avatar arf1980 avatar richard-peterson-at-lifelogic avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.