Skip to content

Commit

Permalink
Fix type ( #152 #157 #158 )
Browse files Browse the repository at this point in the history
  • Loading branch information
Bunlong authored Oct 9, 2023
2 parents 20da590 + 215f21d commit c779a8c
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 17 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 4.2.1 (2023-10-09)

### ✨ Features

* Fix type

Credits

* [@Bunlong](https://github.com/Bunlong)

## 4.2.0 (2023-10-07)

### ✨ Features
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ export default function JsonToCSV() {
const { jsonToCSV } = usePapaParse();

const handleJsonToCSV = () => {
const jsonData = `[
const jsonData = [
{
"Column 1": "1-1",
"Column 2": "1-2",
Expand All @@ -834,7 +834,7 @@ export default function JsonToCSV() {
"Column 3": 6,
"Column 4": 7
}
]`;
];
const results = jsonToCSV(jsonData);
console.log('---------------------------');
console.log('Results:', results);
Expand Down Expand Up @@ -886,6 +886,10 @@ readRemoteFile(url, {

## 📜 Changelog

Latest version 4.2.1 (2023-10-09):

* Fix type

Latest version 4.2.0 (2023-10-07):

* Upgrade dependencies
Expand Down
4 changes: 2 additions & 2 deletions examples/jsonToCSV.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function JsonToCSV() {
const { jsonToCSV } = usePapaParse();

const handleJsonToCSV = () => {
const jsonData = `[
const jsonData = [
{
"Column 1": "1-1",
"Column 2": "1-2",
Expand All @@ -31,7 +31,7 @@ export default function JsonToCSV() {
"Column 3": 6,
"Column 4": 7
}
]`;
];
const results = jsonToCSV(jsonData);
console.log('---------------------------');
console.log('Results:', results);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-papaparse",
"version": "4.2.0",
"version": "4.2.1",
"description": "The fastest in-browser CSV (or delimited text) parser for React. It is full of useful features such as CSVReader, CSVDownloader, readString, jsonToCSV, readRemoteFile, ... etc.",
"author": "Bunlong <bunlong.van@gmail.com>",
"license": "MIT",
Expand Down Expand Up @@ -118,7 +118,7 @@
},
{
"path": "./dist/*.ts",
"maxSize": "1000 B"
"maxSize": "1039 B"
}
]
}
7 changes: 5 additions & 2 deletions src/jsonToCSV.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import PapaParse, { UnparseConfig } from 'papaparse';
import PapaParse, { UnparseObject, UnparseConfig } from 'papaparse';

export function jsonToCSV(json: any, options: UnparseConfig = {}) {
export function jsonToCSV<T>(
json: T[] | UnparseObject<T>,
options: UnparseConfig = {},
) {
return PapaParse.unparse(json, options);
}
73 changes: 72 additions & 1 deletion src/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,78 @@ import { ParseConfig, ParseResult, Parser } from 'papaparse';
// 5.3 => https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/papaparse/index.d.ts
// 5.2 => https://github.com/DefinitelyTyped/DefinitelyTyped/blob/d3737ebd9125505f7ea237b9f17f1426579a3917/types/papaparse/index.d.ts

export interface CustomConfig<T = any, TInput = undefined>
export interface CSVReaderConfig<T = any, TInput = undefined>
extends ParseConfig<T, TInput> {
/**
* * * * * * * * * *
* ParseAsyncConfig
* * * * * * * * * *
*/

/**
* Whether or not to use a worker thread.
* Using a worker will keep your page reactive, but may be slightly slower.
* @default false
*/
worker?: boolean | undefined;
/**
* Overrides `Papa.LocalChunkSize` and `Papa.RemoteChunkSize`.
*/
chunkSize?: number | undefined;
/**
* A callback function, identical to `step`, which activates streaming.
* However, this function is executed after every chunk of the file is loaded and parsed rather than every row.
* Works only with local and remote files.
* Do not use both `chunk` and `step` callbacks together.
*/
chunk?(results: ParseResult<T>, parser: Parser): void;
/**
* A callback to execute if FileReader encounters an error.
* The function is passed two arguments: the error and the File.
*/
error?(error: Error, file: TInput): void;

// ParseLocalConfig
/** The encoding to use when opening local files. If specified, it must be a value supported by the FileReader API. */
encoding?: string | undefined;

/**
* * * * * * * * * * *
* ParseRemoteConfig
* * * * * * * * * * *
*/

/**
* This indicates that the string you passed as the first argument to `parse()`
* is actually a URL from which to download a file and parse its contents.
*/
// download: true;
download?: boolean | true; // default: false
/**
* If defined, should be an object that describes the headers.
* @example { 'Authorization': 'token 123345678901234567890' }
* @default undefined
*/
downloadRequestHeaders?: { [headerName: string]: string } | undefined;
/**
* Use POST request on the URL of the download option. The value passed will be set as the body of the request.
* @default undefined
*/
downloadRequestBody?:
| Blob
| BufferSource
| FormData
| URLSearchParams
| string
| undefined;
/**
* A boolean value passed directly into XMLHttpRequest's "withCredentials" property.
* @default undefined
*/
withCredentials?: boolean | undefined;
}

export interface ReadStringConfig<T = any, TInput = undefined>
extends ParseConfig<T, TInput> {
/**
* * * * * * * * * *
Expand Down
13 changes: 9 additions & 4 deletions src/readString.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import PapaParse, { ParseWorkerConfig } from 'papaparse';
import PapaParse, { NODE_STREAM_INPUT, ParseResult } from 'papaparse';

import { ReadStringConfig } from './model';

export function readString<T>(
csvString: string,
config: ParseWorkerConfig<T> & { download?: false | undefined },
csvString: string | typeof NODE_STREAM_INPUT,
config: ReadStringConfig<T> & {
download?: false | undefined;
complete(results: ParseResult<T>): void;
},
) {
return PapaParse.parse(csvString, config);
return PapaParse.parse(csvString as typeof NODE_STREAM_INPUT, config);
}
4 changes: 2 additions & 2 deletions src/useCSVReader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, {
useRef,
} from 'react';
import PapaParse, { ParseResult } from 'papaparse';
import { CustomConfig } from './model';
import { CSVReaderConfig } from './model';
import {
composeEventHandlers,
isIeOrEdge,
Expand All @@ -30,7 +30,7 @@ const DEFAULT_ACCEPT = 'text/csv, .csv, application/vnd.ms-excel';
export interface Props<T> {
children: (fn: any) => void | ReactNode;
accept?: string;
config?: CustomConfig<T>;
config?: CSVReaderConfig<T>;
minSize?: number;
maxSize?: number;
maxFiles?: number;
Expand Down
4 changes: 2 additions & 2 deletions supports/create-react-app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function App() {
};

const handleJsonToCSV = () => {
const jsonData = `[
const jsonData = [
{
"Column 1": "1-1",
"Column 2": "1-2",
Expand All @@ -55,7 +55,7 @@ function App() {
"Column 3": 6,
"Column 4": 7
}
]`;
];
const results = jsonToCSV(jsonData);
console.log('---------------------------');
console.log('Results:', results);
Expand Down

0 comments on commit c779a8c

Please sign in to comment.