diff --git a/src/ParserForCSV.ts b/src/ParserForCSV.ts index 5d28c05..72dc3a6 100644 --- a/src/ParserForCSV.ts +++ b/src/ParserForCSV.ts @@ -1,8 +1,8 @@ const Papa=require("papaparse"); -const errors= require("./errors.js"); +const errors=require("./errors.js"); import { RemoteSource } from "./freeDatas2HTMLRemoteSource"; -import { ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./interfaces"; +import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./interfaces"; import { PublicPapaParseOptions, PrivatePapaParseOptions } from "./interfacesPapaParse"; export class ParserForCSV implements Parsers @@ -12,23 +12,23 @@ export class ParserForCSV implements Parsers private _parseResults:ParseResults|undefined=undefined; public options: PublicPapaParseOptions= { - delimiter:"", - newline:"", - quoteChar:'"', - escapeChar:'"', - transformHeader:function(field: string, index: number): string { return field.trim() }, - preview:0, - comments:"", - fastMode:undefined, - transform:undefined + delimiter: "", + newline: "", + quoteChar: '"', + escapeChar: '"', + transformHeader: function(field: string, index: number): string { return field.trim() }, + preview: 0, + comments: "", + fastMode: undefined, + transform: undefined } private _privateOptions: PrivatePapaParseOptions= { - header:true, - download:false, - downloadRequestHeaders:undefined, - skipEmptyLines:"greedy", - withCredentials:undefined + header: true, + download: false, + downloadRequestHeaders: undefined, + skipEmptyLines: "greedy", + withCredentials: undefined } // L'instance d'une autre classe que RemoteSource peut être passée au constructeur @@ -75,7 +75,7 @@ export class ParserForCSV implements Parsers public async parse(): Promise { - const parser=this, options=this.options; + const parser=this; let parseContent=""; if(parser._datasRemoteSource.url !== "") { @@ -98,25 +98,25 @@ export class ParserForCSV implements Parsers { Papa.parse(parseContent, { - delimiter: options.delimiter, - newline: options.newline, - quoteChar: options.quoteChar, - escapeChar: options.escapeChar, + delimiter: this.options.delimiter, + newline: this.options.newline, + quoteChar: this.options.quoteChar, + escapeChar: this.options.escapeChar, header: true, - transformHeader: options.transformHeader, - preview: options.preview, - comments: options.comments, + transformHeader: this.options.transformHeader, + preview: this.options.preview, + comments: this.options.comments, complete: function(results :any) { - // Attention, Papa Parse peut accepter un nom de champ vide ou en doublon ! - let realFields: string[]=[]; + // Attention, Papa Parse peut accepter un nom de champ vide ou en doublon + let realFields: string[]=[], parseErrors: ParseErrors[]=[]; for(let field of results.meta.fields) { let checkField=field.trim(); if(checkField !== "" && realFields.indexOf(checkField) === -1) realFields.push(checkField); else - console.error(errors.parserFieldNameFail); + parseErrors.push({ row:-1, message: errors.parserFieldNameFail}); } if(realFields.length === 0) reject(new Error(errors.parserFieldsNotFound)); @@ -125,7 +125,7 @@ export class ParserForCSV implements Parsers parser._parseResults= { datas: results.data, - errors: results.errors, + errors: parseErrors.concat(results.errors), // result.errors = errreurs rencontrées par Papa Parse fields: realFields, }; resolve(true); @@ -134,9 +134,9 @@ export class ParserForCSV implements Parsers download: this._privateOptions.download, downloadRequestHeaders: this._privateOptions.downloadRequestHeaders, skipEmptyLines:"greedy", - fastMode: options.fastMode, + fastMode: this.options.fastMode, withCredentials: this._privateOptions.withCredentials, - transform: options.transform + transform: this.options.transform }); }); } diff --git a/src/interfaces.ts b/src/interfaces.ts index 85e2627..414a0b0 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -54,7 +54,7 @@ export interface ParseErrors { code?: string; message: string; - row: number; + row: number; // -1 quand bug avant de traiter les lignes type?: string; } export interface ParseResults diff --git a/tests/parserForCSVSpec.ts b/tests/parserForCSVSpec.ts index b415d08..0b276fb 100644 --- a/tests/parserForCSVSpec.ts +++ b/tests/parserForCSVSpec.ts @@ -82,11 +82,12 @@ describe("Tests du parseur de CSV", () => expect(Papa.parse).toHaveBeenCalledTimes(2); }); - it("Si les données à parser contiennent des noms de champ vide ou en doublon, ils doivent être ignorés.", async () => + it("Si les données à parser contiennent des noms de champ vide ou en doublon, ils doivent être ignorés et cela doit être notifié.", async () => { - parser.datas2Parse="field1;field2;field3;field3; ;"; + parser.datas2Parse="field1;field2;field3;field3; "; await parser.parse(); expect(parser.parseResults.fields).toEqual(["field1","field2","field3"]); + expect(parser.parseResults.errors).toEqual([{ row:-1, message: errors.parserFieldNameFail}, { row:-1, message: errors.parserFieldNameFail}]); }); it("Doit générer une erreur si aucun nom de champ trouvé.", async () =>