Relecture code du parseur CSV

This commit is contained in:
Fabrice PENHOËT 2021-10-18 11:27:53 +02:00
parent 2b1eabd430
commit 4dc2753468
3 changed files with 34 additions and 33 deletions

View File

@ -1,8 +1,8 @@
const Papa=require("papaparse"); const Papa=require("papaparse");
const errors= require("./errors.js"); const errors=require("./errors.js");
import { RemoteSource } from "./freeDatas2HTMLRemoteSource"; import { RemoteSource } from "./freeDatas2HTMLRemoteSource";
import { ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./interfaces"; import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./interfaces";
import { PublicPapaParseOptions, PrivatePapaParseOptions } from "./interfacesPapaParse"; import { PublicPapaParseOptions, PrivatePapaParseOptions } from "./interfacesPapaParse";
export class ParserForCSV implements Parsers export class ParserForCSV implements Parsers
@ -12,23 +12,23 @@ export class ParserForCSV implements Parsers
private _parseResults:ParseResults|undefined=undefined; private _parseResults:ParseResults|undefined=undefined;
public options: PublicPapaParseOptions= public options: PublicPapaParseOptions=
{ {
delimiter:"", delimiter: "",
newline:"", newline: "",
quoteChar:'"', quoteChar: '"',
escapeChar:'"', escapeChar: '"',
transformHeader:function(field: string, index: number): string { return field.trim() }, transformHeader: function(field: string, index: number): string { return field.trim() },
preview:0, preview: 0,
comments:"", comments: "",
fastMode:undefined, fastMode: undefined,
transform:undefined transform: undefined
} }
private _privateOptions: PrivatePapaParseOptions= private _privateOptions: PrivatePapaParseOptions=
{ {
header:true, header: true,
download:false, download: false,
downloadRequestHeaders:undefined, downloadRequestHeaders: undefined,
skipEmptyLines:"greedy", skipEmptyLines: "greedy",
withCredentials:undefined withCredentials: undefined
} }
// L'instance d'une autre classe que RemoteSource peut être passée au constructeur // 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<any> public async parse(): Promise<any>
{ {
const parser=this, options=this.options; const parser=this;
let parseContent=""; let parseContent="";
if(parser._datasRemoteSource.url !== "") if(parser._datasRemoteSource.url !== "")
{ {
@ -98,25 +98,25 @@ export class ParserForCSV implements Parsers
{ {
Papa.parse(parseContent, Papa.parse(parseContent,
{ {
delimiter: options.delimiter, delimiter: this.options.delimiter,
newline: options.newline, newline: this.options.newline,
quoteChar: options.quoteChar, quoteChar: this.options.quoteChar,
escapeChar: options.escapeChar, escapeChar: this.options.escapeChar,
header: true, header: true,
transformHeader: options.transformHeader, transformHeader: this.options.transformHeader,
preview: options.preview, preview: this.options.preview,
comments: options.comments, comments: this.options.comments,
complete: function(results :any) complete: function(results :any)
{ {
// Attention, Papa Parse peut accepter un nom de champ vide ou en doublon ! // Attention, Papa Parse peut accepter un nom de champ vide ou en doublon
let realFields: string[]=[]; let realFields: string[]=[], parseErrors: ParseErrors[]=[];
for(let field of results.meta.fields) for(let field of results.meta.fields)
{ {
let checkField=field.trim(); let checkField=field.trim();
if(checkField !== "" && realFields.indexOf(checkField) === -1) if(checkField !== "" && realFields.indexOf(checkField) === -1)
realFields.push(checkField); realFields.push(checkField);
else else
console.error(errors.parserFieldNameFail); parseErrors.push({ row:-1, message: errors.parserFieldNameFail});
} }
if(realFields.length === 0) if(realFields.length === 0)
reject(new Error(errors.parserFieldsNotFound)); reject(new Error(errors.parserFieldsNotFound));
@ -125,7 +125,7 @@ export class ParserForCSV implements Parsers
parser._parseResults= parser._parseResults=
{ {
datas: results.data, datas: results.data,
errors: results.errors, errors: parseErrors.concat(results.errors), // result.errors = errreurs rencontrées par Papa Parse
fields: realFields, fields: realFields,
}; };
resolve(true); resolve(true);
@ -134,9 +134,9 @@ export class ParserForCSV implements Parsers
download: this._privateOptions.download, download: this._privateOptions.download,
downloadRequestHeaders: this._privateOptions.downloadRequestHeaders, downloadRequestHeaders: this._privateOptions.downloadRequestHeaders,
skipEmptyLines:"greedy", skipEmptyLines:"greedy",
fastMode: options.fastMode, fastMode: this.options.fastMode,
withCredentials: this._privateOptions.withCredentials, withCredentials: this._privateOptions.withCredentials,
transform: options.transform transform: this.options.transform
}); });
}); });
} }

View File

@ -54,7 +54,7 @@ export interface ParseErrors
{ {
code?: string; code?: string;
message: string; message: string;
row: number; row: number; // -1 quand bug avant de traiter les lignes
type?: string; type?: string;
} }
export interface ParseResults export interface ParseResults

View File

@ -82,11 +82,12 @@ describe("Tests du parseur de CSV", () =>
expect(Papa.parse).toHaveBeenCalledTimes(2); 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(); await parser.parse();
expect(parser.parseResults.fields).toEqual(["field1","field2","field3"]); 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 () => it("Doit générer une erreur si aucun nom de champ trouvé.", async () =>