Skip to content

Commit

Permalink
Merge pull request #242 from mrodrig/v5-prep
Browse files Browse the repository at this point in the history
V5 documentation prep
  • Loading branch information
mrodrig authored Oct 29, 2023
2 parents 1f2e6f9 + 7e71ee7 commit d636bff
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
20 changes: 6 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ $ npm install @mrodrig/json-2-csv-cli

## Upgrading?

Upgrading to v4 from v3? Check out the [upgrade guide](https://github.com/mrodrig/json-2-csv/blob/master/upgrade_guides/UPGRADE_3_to_4.md).
Upgrading to v5 from v4? Check out the [upgrade guide](https://github.com/mrodrig/json-2-csv/blob/master/upgrade_guides/UPGRADE_4_to_5.md).

## Usage

Expand All @@ -41,13 +41,12 @@ or
```javascript
import { json2csv } from 'json-2-csv';
```
Looking for examples? Check out the Wiki: [json-2-csv Wiki](https://github.com/mrodrig/json-2-csv/wiki)

### API

#### `json2csv(array, options)` => `Promise<string>`
#### `json2csv(array, options)` => `string`

Returns a `Promise` that resolves with the CSV `string` or rejects with an `Error` if there was an issue.
Returns the CSV `string` or rejects with an `Error` if there was an issue.

* `array` - An array of JSON documents to be converted to CSV.
* `options` - (Optional) A JSON document specifying any of the following key value pairs:
Expand Down Expand Up @@ -165,11 +164,9 @@ Returns a `Promise` that resolves with the CSV `string` or rejects with an `Erro
* Default: `false`


For examples, please refer to the [json2csv API Documentation (Link)](https://github.com/mrodrig/json-2-csv/wiki/json2csv-Documentation)
#### `csv2json(csv, options)` => object[]

#### `csv2json(csv, options)` => Promise<object[]>

Returns a `Promise` that resolves with the JSON object array (`object[]`) or rejects with an `Error` if there was an issue.
Returns the JSON object array (`object[]`) or rejects with an `Error` if there was an issue.

* `csv` - A string of CSV
* `options` - (Optional) A JSON document specifying any of the following key value pairs:
Expand Down Expand Up @@ -197,8 +194,6 @@ Returns a `Promise` that resolves with the JSON object array (`object[]`) or rej
* `trimFieldValues` - Boolean - Should the field values be trimmed?
* Default: `false`

For examples, please refer to the [csv2json API Documentation (Link)](https://github.com/mrodrig/json-2-csv/wiki/csv2json-Documentation)

### CLI
Note: As of `3.5.8`, the command line interface functionality has been pulled out to a separate package. Please be sure to
install the `@mrodrig/json-2-csv-cli` NPM package if you wish to use the CLI functionality shown below:
Expand Down Expand Up @@ -258,9 +253,6 @@ To see test coverage, please run:
$ npm run coverage
```

## Frequently Asked Questions (FAQ)
Please find the updated list (relocated to the Wiki) here: [Frequently Asked Questions (Link)](https://github.com/mrodrig/json-2-csv/wiki/FAQ)

## Features
* Header Generation (per document keys)
* Allows for conversion of specific keys in both json2csv and csv2json via the options.keys parameter (as of 1.1.2)
Expand All @@ -272,11 +264,11 @@ Please find the updated list (relocated to the Wiki) here: [Frequently Asked Que
* Allows for custom field delimiters, end of line delimiters, etc.
* Wrapped value support for json2csv and csv2json (as of 1.3.0)
* Support for multiple different schemas (as of 1.4.0)
* Promisified versions of the functions are now available by default: json2csvAsync, csv2jsonAsync (as of 2.2.0)
* RFC 4180 Compliance (as of 3.0.0)
* CLI functionality (as of 3.0.0)
* `csv2json test.csv -o output.json`
* *and*
* `json2csv test.json -o output.csv -W -k arrayOfStrings -o output.csv`
* Empty field value option (as of 3.1.0)
* TypeScript typings included (as of 3.4.0) - thanks to [@GabrielCastro](https://github.com/GabrielCastro)!
* Synchronous use case support (as of 5.0.0) - thanks to [@Nokel81](https://github.com/Nokel81)
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"name": "json-2-csv",
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
"version": "4.1.1",
"version": "5.0.0",
"homepage": "https://mrodrig.github.io/json-2-csv",
"repository": {
"type": "git",
Expand Down
40 changes: 40 additions & 0 deletions upgrade_guides/UPGRADE_4_to_5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Upgrade Guide - v4 to v5

## Breaking Changes

* Asynchronous methods are now synchronous

Thanks to [@Nokel81](https://github.com/Nokel81) for the pull request to convert the module to convert the asynchronous internal flow to be entirely synchronous. This helps enable certain use cases where a conversion is needed in a synchronous manner, or where asynchronous code might not be feasible. While these changes shift the module towards a more synchronous focused use case, it's still entirely possible to perform JSON to CSV or CSV to JSON conversions for an asynchronous use case too.

```javascript
const converter = require('json-2-csv');

// Synchronous:

const csv = converter.json2csv([ { level: 'info', message: 'Our first test' }]);
console.log('First output is', csv);

// Asynchronous:

async function runConversion() {
return converter.json2csv([ { level: 'info', message: 'Another test...' }]);
}

async function runAsync() {
const csv = await runConversion();
console.log('Second output is', csv);
}

runAsync();
console.log('This can run before the second output appears...');
```

Example output:

```
First output is level,message
info,Our first test
This can run before the second output appears...
Second output is level,message
info,Another test...
```

0 comments on commit d636bff

Please sign in to comment.