From 6e064fedee47a32b883ebe419a26dad2b499d0d0 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 23 Sep 2016 07:37:29 -0400 Subject: [PATCH] Add parseFiles method to babyparse (#11318) --- babyparse/babyparse-tests.ts | 68 ++++++++++++++++++++---------------- babyparse/babyparse.d.ts | 10 ++++++ 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/babyparse/babyparse-tests.ts b/babyparse/babyparse-tests.ts index 29d2d22891..186351ccd5 100644 --- a/babyparse/babyparse-tests.ts +++ b/babyparse/babyparse-tests.ts @@ -329,32 +329,32 @@ var CORE_PARSER_TESTS: ParserTest[] = [ } }, // { - // description: "Comment with non-default character", - // input: 'a,b,c\n!Comment goes here\nd,e,f', - // config: { comments: '!' }, - // expected: { - // data: [['a', 'b', 'c'], ['d', 'e', 'f']], - // errors: [] - // } + // description: "Comment with non-default character", + // input: 'a,b,c\n!Comment goes here\nd,e,f', + // config: { comments: '!' }, + // expected: { + // data: [['a', 'b', 'c'], ['d', 'e', 'f']], + // errors: [] + // } // }, // { - // description: "Bad comments value specified", - // notes: "Should silently disable comment parsing", - // input: 'a,b,c\n5comment\nd,e,f', - // config: { comments: 5 }, - // expected: { - // data: [['a', 'b', 'c'], ['5comment'], ['d', 'e', 'f']], - // errors: [] - // } + // description: "Bad comments value specified", + // notes: "Should silently disable comment parsing", + // input: 'a,b,c\n5comment\nd,e,f', + // config: { comments: 5 }, + // expected: { + // data: [['a', 'b', 'c'], ['5comment'], ['d', 'e', 'f']], + // errors: [] + // } // }, // { - // description: "Multi-character comment string", - // input: 'a,b,c\n=N(Comment)\nd,e,f', - // config: { comments: "=N(" }, - // expected: { - // data: [['a', 'b', 'c'], ['d', 'e', 'f']], - // errors: [] - // } + // description: "Multi-character comment string", + // input: 'a,b,c\n=N(Comment)\nd,e,f', + // config: { comments: "=N(" }, + // expected: { + // data: [['a', 'b', 'c'], ['d', 'e', 'f']], + // errors: [] + // } // }, { description: "Input with only a commented line", @@ -419,13 +419,13 @@ var CORE_PARSER_TESTS: ParserTest[] = [ } }, // { - // description: "Fast mode with comments", - // input: '// Commented line\na,b,c', - // config: { fastMode: true, comments: "//" }, - // expected: { - // data: [['a', 'b', 'c']], - // errors: [] - // } + // description: "Fast mode with comments", + // input: '// Commented line\na,b,c', + // config: { fastMode: true, comments: "//" }, + // expected: { + // data: [['a', 'b', 'c']], + // errors: [] + // } // }, { description: "Fast mode with preview", @@ -1091,7 +1091,7 @@ function compare(actualData: any[], actualErrors: any[], expected: ParserTestExp } } - if (passed) // final check will catch any other differences + if (passed) // final check will catch any other differences passed = JSON.stringify(actual) == JSON.stringify(expected); // We pass back an object right now, even though it only contains @@ -1228,3 +1228,11 @@ var parser = new Baby.Parser({}) parser.getCharIndex(); parser.abort(); parser.parse(""); + +/** + * Parse Files + */ +Baby.parseFiles('example.csv') +Baby.parseFiles('example.csv', {encoding: 'utf-8'}) +Baby.parseFiles(['example1.csv', 'example2.txt']) +Baby.parseFiles(['example1.csv', 'example2.txt'], {encoding: 'utf-8'}) \ No newline at end of file diff --git a/babyparse/babyparse.d.ts b/babyparse/babyparse.d.ts index 6e8df86c3e..5fd9856ebc 100644 --- a/babyparse/babyparse.d.ts +++ b/babyparse/babyparse.d.ts @@ -9,6 +9,11 @@ declare namespace BabyParse { * Parse a csv string or a csv file */ parse(csvString: string, config?: ParseConfig): ParseResult; + + /** + * Parse single file or multiple files + */ + parseFiles(input: string | string[] | FileObject | FileObject[], config?: ParseConfig): ParseResult; /** * Unparses javascript data objects and returns a csv string @@ -86,6 +91,11 @@ declare namespace BabyParse { data: string | Array; } + interface FileObject { + file: string + config?: ParseConfig + } + interface ParseError { type: string; // A generalization of the error code: string; // Standardized error code