Relecture code du parseur CSV
This commit is contained in:
parent
2b1eabd430
commit
4dc2753468
@ -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<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
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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 () =>
|
||||
|
Loading…
Reference in New Issue
Block a user