Skip to content

Commit

Permalink
updated transformHeader type
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcam committed Jul 20, 2020
1 parent 8c9eba5 commit 57db620
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 57 deletions.
2 changes: 1 addition & 1 deletion types/papaparse/index.d.ts
Expand Up @@ -119,7 +119,7 @@ export interface ParseConfig<T = any> {
chunk?(results: ParseResult<T>, parser: Parser): void; // default: undefined
beforeFirstChunk?(chunk: string): string | void; // default: undefined
transform?(value: string, field: string | number): any; // default: undefined
transformHeader?(header: string): string; // default: undefined
transformHeader?(header: string, index: number): string; // default: undefined
}

export interface UnparseConfig {
Expand Down
109 changes: 53 additions & 56 deletions types/papaparse/papaparse-tests.ts
@@ -1,36 +1,27 @@


import Papa = require("papaparse");
import {
ParseConfig,
UnparseConfig,
UnparseObject,
ParseError,
ParseMeta,
ParseResult
} from "papaparse";
import { Readable } from "stream";
import Papa = require('papaparse');
import { ParseConfig, UnparseConfig, UnparseObject, ParseError, ParseMeta, ParseResult } from 'papaparse';
import { Readable } from 'stream';

/**
* Parsing
*/
var res = Papa.parse("3,3,3");
var res = Papa.parse('3,3,3');

res.errors[0].code;

Papa.parse("3,3,3", {
Papa.parse('3,3,3', {
delimiter: ';',
comments: false,
trimHeaders: false,
step: function (results, p) {
p.abort();
results.data.length;
},
dynamicTyping: true
dynamicTyping: true,
});

Papa.parse("3,3,3", {
dynamicTyping: (field: string | number): boolean => /headerName/i.test(field.toString())
Papa.parse('3,3,3', {
dynamicTyping: (field: string | number): boolean => /headerName/i.test(field.toString()),
});

Papa.parse('3,3,3', {
Expand All @@ -56,8 +47,8 @@ Papa.parse('4;4;4', {
var file = new File(null, null, null);

Papa.parse(file, {
transform: function(value, field) {},
transformHeader: function(header) {
transform: function (value, field) {},
transformHeader: function (header, index) {
return header;
},
complete: function (a, b) {
Expand All @@ -67,14 +58,10 @@ Papa.parse(file, {
});

// .pipe to make sure it returns a stream
Papa.parse(Papa.NODE_STREAM_INPUT, {
}).pipe
Papa.parse(Papa.NODE_STREAM_INPUT, {}).pipe;

const readable = new Readable()
const rows = [
"1,2,3",
"4,5,6"
]
const readable = new Readable();
const rows = ['1,2,3', '4,5,6'];

rows.forEach(r => {
readable.push(r);
Expand All @@ -85,45 +72,55 @@ const papaStream: NodeJS.ReadWriteStream = Papa.parse(Papa.NODE_STREAM_INPUT);
readable.pipe(papaStream);

// generic
Papa.parse<string>("a,b,c", {
step: function(a) {
a.data[0]
}
})

Papa.parse<string>("a,b,c", {
chunk: function(a) {
a.data[0]
}
})

Papa.parse<[string, string, string]>("a,b,c", {
complete: function(a) {
a.data[0][0]
a.data[0][1]
a.data[0][2]
}
})
Papa.parse<string>('a,b,c', {
step: function (a) {
a.data[0];
},
});

Papa.parse<string>('a,b,c', {
chunk: function (a) {
a.data[0];
},
});

Papa.parse<[string, string, string]>('a,b,c', {
complete: function (a) {
a.data[0][0];
a.data[0][1];
a.data[0][2];
},
});

/**
* Unparsing
*/
Papa.unparse([{ a: 1, b: 1, c: 1 }]);
Papa.unparse([[1, 2, 3], [4, 5, 6]]);
Papa.unparse([
[1, 2, 3],
[4, 5, 6],
]);
Papa.unparse({
fields: ["3"],
data: []
fields: ['3'],
data: [],
});

Papa.unparse([{ a: 1, b: 1, c: 1 }], { quotes: false });
Papa.unparse([{ a: 1, b: 1, c: 1 }], { quotes: [false, true, true] });
Papa.unparse([[1, 2, 3], [4, 5, 6]], { delimiter: "," });
Papa.unparse({
fields: ["3"],
data: []
}, { newline: "\n" });

Papa.unparse(
[
[1, 2, 3],
[4, 5, 6],
],
{ delimiter: ',' },
);
Papa.unparse(
{
fields: ['3'],
data: [],
},
{ newline: '\n' },
);

/**
* Properties
Expand All @@ -134,7 +131,7 @@ Papa.LocalChunkSize;
/**
* Parser
*/
var parser = new Papa.Parser({})
var parser = new Papa.Parser({});
parser.getCharIndex();
parser.abort();
parser.parse("", 0, false);
parser.parse('', 0, false);

0 comments on commit 57db620

Please sign in to comment.