-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* custom headers * Added tests * updateDoc * Apply suggestions from code review Update example Co-authored-by: Zach McElrath <[email protected]> * PR feedback --------- Co-authored-by: Zach McElrath <[email protected]>
- Loading branch information
1 parent
1934a8d
commit 8cd6ca0
Showing
5 changed files
with
124 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,14 +67,66 @@ or load from a CDN: | |
}; | ||
``` | ||
|
||
## Example with Header mapping | ||
|
||
```jsx | ||
// import jsonToCsvExport from "json-to-csv-export"; | ||
() => { | ||
const mockData = [ | ||
{ | ||
id: 1, | ||
firstName: "Sarajane", | ||
lastName: "Wheatman", | ||
email: "[email protected]", | ||
language: "Zulu", | ||
ip: "40.98.252.240", | ||
}, | ||
{ | ||
id: 2, | ||
firstName: "Linell", | ||
lastName: "Humpherston", | ||
email: "[email protected]", | ||
language: "Czech", | ||
ip: "82.225.151.150", | ||
}, | ||
]; | ||
|
||
const headers = [ | ||
{ key: "id", label: "Identifier" }, | ||
{ key: "firstName", label: "First Name" }, | ||
{ key: "lastName", label: "Last Name" }, | ||
{ key: "email", label: "Email Address" }, | ||
{ key: "language", label: "Language" }, | ||
{ key: "ip", label: "IP Address" }, | ||
]; | ||
|
||
return ( | ||
<button onClick={() => jsonToCsvExport({ data: mockData, headers })}> | ||
Download Data | ||
</button> | ||
); | ||
}; | ||
``` | ||
|
||
|
||
|
||
|
||
## Properties | ||
|
||
| # | Property | Type | Requirement | Default | Description | | ||
| --- | --------- | -------- | ----------- | ------------------------- | ---------------------------------------------------------------------------------------- | | ||
| 1 | data | [] | required | | array of objects | | ||
| 2 | filename | string | optional | "export.csv" | The filename. The .csv extention will be added if not included in file name | | ||
| 3 | delimiter | string | optional | "," | fields separator | | ||
| 4 | headers | string[] | optional | provided data object keys | List of columns that will be used in the final CSV file. Recommended for large datasets! | | ||
```typescript | ||
interface HeaderMapping { | ||
label: string; | ||
key: string; | ||
} | ||
``` | ||
|
||
|
||
| # | Property | Type | Requirement | Default | Description | | ||
| - | --------- | -------------------------------- | ----------- | ------------------------- | ---------------------------------------------------------------------------------------- | | ||
| 1 | data | [] | required | | array of objects | | ||
| 2 | filename | string | optional | "export.csv" | The filename. The .csv extention will be added if not included in file name | | ||
| 3 | delimiter | string | optional | "," | fields separator | | ||
| 4 | headers | string[] OR<br />HeaderMapping[] | optional | provided data object keys | List of columns that will be used in the final CSV file. Recommended for large datasets! | | ||
|
||
## Migration from version 1.x to 2.x | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import csvDownload from "../src/index"; | ||
import csvDownload, { HeaderMapping } from "../src/index"; | ||
import mockData from "./__mocks__/mockData"; | ||
|
||
// current version of JSDom doesn't support Blob.text(), so this is a FileReader-based workaround. | ||
|
@@ -111,4 +111,33 @@ describe("csvDownload", () => { | |
generatedCsvString.includes(`Blanch,[email protected],Elby,1`) | ||
).toBeTruthy(); | ||
}); | ||
|
||
test("downloads CSV with HeaderMapping",async () => { | ||
const headers: HeaderMapping[] = [ | ||
{ key: "First Name", label: "First Name Label" }, | ||
{ key: "Last Name", label: "Last Name Label" }, | ||
{ key: "Email", label: "Email Address Label" }, | ||
{ key: "Gender", label: "Gender Label" }, | ||
{ key: "IP Address", label: "IP Address Label" }, | ||
]; | ||
|
||
const expectedCsv = `First Name,Last Name,Email Address,Gender,IP Address\r\nPaulie,Steffens,[email protected],Female,115.83.208.158`; | ||
|
||
|
||
csvDownload({ | ||
data: mockData, | ||
headers, | ||
filename: "test-customHeaders.csv", | ||
}); | ||
expect(link.download).toEqual("test-customHeaders.csv"); | ||
expect(capturedBlob).not.toBe(null); | ||
const generatedCsvString = await getBlobAsText(capturedBlob as Blob); | ||
expect( | ||
generatedCsvString.startsWith(`First Name Label,Last Name Label,Email Address Label,Gender Label,IP Address Label`) | ||
).toBeTruthy(); | ||
expect( | ||
generatedCsvString.includes(`Blanch,Elby,[email protected],Female,112.81.107.207`) | ||
).toBeTruthy(); | ||
|
||
}); | ||
}); |