export-to-csv
Advanced tools
Comparing version 0.2.2 to 1.0.0
{ | ||
"name": "export-to-csv", | ||
"version": "0.2.2", | ||
"version": "1.0.0", | ||
"description": "Easily create CSV data from json collection", | ||
"type": "module", | ||
"repository": { | ||
@@ -10,5 +11,9 @@ "type": "git", | ||
"scripts": { | ||
"build": "rm -rf output && rollup -c rollup.config.mjs", | ||
"e2e": "rm -rf integration/export-to-csv.js && npm run build && cp output/index.js integration/export-to-csv.js && playwright test", | ||
"e2e-ci": "rm -rf integration/export-to-csv.js && npm run build && cp output/index.js integration/export-to-csv.js && npx playwright install --with-deps && playwright test", | ||
"e2e-server": "npx http-server ./integration -p 3000", | ||
"test": "jest", | ||
"dev": "tsc --watch", | ||
"test": "karma start", | ||
"prepublishOnly": "tsc" | ||
"prepublishOnly": "npm run build" | ||
}, | ||
@@ -22,8 +27,4 @@ "keywords": [ | ||
"openoffice", | ||
"angular2", | ||
"typescript", | ||
"react", | ||
"ngx", | ||
"angular4", | ||
"angular5" | ||
"zero-dependencies" | ||
], | ||
@@ -36,17 +37,20 @@ "author": "alexcaza", | ||
}, | ||
"main": "./build/index.js", | ||
"types": "./build/export-to-csv.d.ts", | ||
"main": "./output/index.js", | ||
"types": "./output/index.d.ts", | ||
"homepage": "https://github.com/alexcaza/export-to-csv#readme", | ||
"devDependencies": { | ||
"@types/jasmine": "^4.3.5", | ||
"jasmine-core": "^5.1.1", | ||
"karma": "^6.4.2", | ||
"karma-chrome-launcher": "^3.2.0", | ||
"karma-jasmine": "^5.1.0", | ||
"karma-phantomjs-launcher": "^1.0.4", | ||
"karma-sourcemap-loader": "^0.4.0", | ||
"karma-typescript": "^5.5.4", | ||
"@babel/core": "^7.22.11", | ||
"@babel/preset-env": "^7.22.14", | ||
"@babel/preset-typescript": "^7.22.11", | ||
"@jest/globals": "^29.6.4", | ||
"@playwright/test": "^1.37.1", | ||
"@rollup/plugin-typescript": "^11.1.3", | ||
"babel-jest": "^29.6.4", | ||
"http-server": "^14.1.1", | ||
"jest": "^29.6.4", | ||
"prettier": "3.0.3", | ||
"rollup": "^3.28.1", | ||
"tslib": "^2.6.2", | ||
"typescript": "~5.2.2" | ||
} | ||
} |
118
README.md
# export-to-csv | Export to CSV Mini Library | ||
Based off of [this library](https://github.com/javiertelioz/angular2-csv) by Javier Telio | ||
> Helper library to quickly and easily create a CSV file in browser or Node | ||
> | ||
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](#alternatives) section below) | ||
If you want a lightweight, stable, easy-to-use basic CSV generation and download library, feel free to install. | ||
## Installation | ||
```javascript | ||
yarn add export-to-csv | ||
// npm install --save export-to-csv | ||
npm install --save export-to-csv | ||
``` | ||
## Usage | ||
```javascript | ||
import { ExportToCsv } from 'export-to-csv'; | ||
This library was written with TypeScript in mind, so the examples will be in TS. | ||
var data = [ | ||
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](integration/index.html) for browser/JS use, and the [unit tests](lib/__specs__) to understand how the library functions. | ||
```typescript | ||
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: 'Test 1', | ||
age: 13, | ||
average: 8.2, | ||
approved: true, | ||
description: "using 'Content here, content here' " | ||
name: "Rouky", | ||
date: "2023-09-01", | ||
percentage: 0.4, | ||
quoted: '"Pickles"', | ||
}, | ||
{ | ||
name: 'Test 2', | ||
age: 11, | ||
average: 8.2, | ||
approved: true, | ||
description: "using 'Content here, content here' " | ||
name: "Keiko", | ||
date: "2023-09-01", | ||
percentage: 0.9, | ||
quoted: '"Cactus"', | ||
}, | ||
{ | ||
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', | ||
useTextFile: false, | ||
useBom: true, | ||
useKeysAsHeaders: true, | ||
// headers: ['Column 1', 'Column 2', etc...] <-- Won't work with useKeysAsHeaders present! | ||
}; | ||
// Converts your Array<Object> to a CsvOutput string based on the configs | ||
const csv = generateCsv(csvConfig)(mockData); | ||
const csvExporter = new ExportToCsv(options); | ||
// Get the button in your HTML | ||
const csvBtn = document.querySelector("#csv"); | ||
csvExporter.generateCsv(data); | ||
// 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)); | ||
``` | ||
@@ -64,24 +59,33 @@ | ||
| 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString). | | ||
| 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<string> | **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](https://en.wikipedia.org/wiki/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. | | ||
| 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString).| | ||
| **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 | | ||
| **useTextFile** | false | If true, returns a `.txt` file instead of `.csv` | | ||
| **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| | ||
# 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. | ||
- https://csv.js.org/ | ||
- https://www.papaparse.com/ | ||
# Thanks! | ||
| Credits and Original Authors | | ||
| :------------- | | ||
This library was originally based off of [this library](https://github.com/javiertelioz/angular2-csv) by Javier Telio | ||
| Credits and Original Authors | | ||
| :-------------------------------------------------- | | ||
| **[javiertelioz](https://github.com/javiertelioz)** | | ||
| **[sn123](https://github.com/sn123)** | | ||
| **[arf1980](https://github.com/arf1980)** | | ||
| **[sn123](https://github.com/sn123)** | | ||
| **[arf1980](https://github.com/arf1980)** | |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
91
Yes
21503
13
12
267