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

@ -2,7 +2,7 @@ const Papa=require("papaparse");
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
@ -75,7 +75,7 @@ export class ParserForCSV implements Parsers
public async parse(): Promise<any>
{
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
});
});
}

View File

@ -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

View File

@ -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 () =>