2021-09-29 17:56:10 +02:00
|
|
|
const Papa = require("papaparse");
|
|
|
|
const errors = require("./errors.js");
|
|
|
|
|
|
|
|
import { ParseResults, Parsers, RemoteSource } from "./freeDatas2HTMLInterfaces";
|
2021-10-01 13:12:45 +02:00
|
|
|
interface PapaParseOptions
|
2021-09-29 17:56:10 +02:00
|
|
|
{
|
|
|
|
delimiter: string;
|
|
|
|
newline: string;
|
|
|
|
quoteChar: string;
|
|
|
|
escapeChar: string;
|
|
|
|
transformHeader?(field: string, index: number): string;
|
|
|
|
preview: number;
|
|
|
|
comments: false|string,
|
|
|
|
fastMode: boolean|undefined;
|
|
|
|
transform?(value: string): string;
|
|
|
|
}
|
2021-10-01 13:12:45 +02:00
|
|
|
interface PrivatePapaParseOptions
|
|
|
|
{
|
|
|
|
header: boolean; // nécessaire pour obtenir le nom des champs
|
|
|
|
download: boolean;
|
|
|
|
downloadRequestHeaders: undefined| { [index: string]:string|boolean|number } ;
|
|
|
|
skipEmptyLines: string;
|
|
|
|
withCredentials: boolean|undefined;
|
|
|
|
}
|
2021-09-29 17:56:10 +02:00
|
|
|
|
|
|
|
export class ParserForCSV implements Parsers
|
|
|
|
{
|
2021-10-01 13:12:45 +02:00
|
|
|
private _datasRemoteSource: RemoteSource={ url:"" };
|
|
|
|
private _datas2Parse:string="";
|
2021-09-29 17:56:10 +02:00
|
|
|
private _parseResults:ParseResults|undefined=undefined;
|
|
|
|
|
|
|
|
// Ouverture de certaines options de Papa Parse :
|
|
|
|
// cf. https://www.papaparse.com/docs#config
|
2021-10-01 13:12:45 +02:00
|
|
|
public options: PapaParseOptions =
|
2021-09-29 17:56:10 +02:00
|
|
|
{
|
|
|
|
delimiter: "",
|
|
|
|
newline: "",
|
|
|
|
quoteChar: '"',
|
|
|
|
escapeChar: '"',
|
|
|
|
transformHeader: function(field: string, index: number): string { return field.trim() },
|
|
|
|
preview: 0,
|
|
|
|
comments: "",
|
|
|
|
fastMode: undefined,
|
|
|
|
transform: undefined
|
|
|
|
}
|
2021-10-01 13:12:45 +02:00
|
|
|
// Options de Papa Parse ne pouvant être modifées de l'extérieur
|
|
|
|
private _privateOptions: PrivatePapaParseOptions =
|
|
|
|
{
|
|
|
|
header: true, // nécessaire pour obtenir le nom des champs
|
|
|
|
download: false,
|
|
|
|
downloadRequestHeaders: undefined,
|
|
|
|
skipEmptyLines:"greedy",
|
|
|
|
withCredentials: undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
// Revoir tous les protocoles possibles pour une source distante (http(s)://.. , ftp..)
|
2021-09-29 17:56:10 +02:00
|
|
|
set datasRemoteSource(source: RemoteSource)
|
|
|
|
{
|
|
|
|
if(source.url.trim().length === 0)
|
|
|
|
throw new Error(errors.parserNeedUrl);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
source.url=source.url.trim();
|
|
|
|
this._datasRemoteSource=source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 13:12:45 +02:00
|
|
|
get datasRemoteSource() : RemoteSource
|
|
|
|
{
|
|
|
|
return this._datasRemoteSource;
|
|
|
|
}
|
|
|
|
|
2021-09-29 17:56:10 +02:00
|
|
|
set datas2Parse(datas: string)
|
|
|
|
{
|
|
|
|
if(datas.trim().length === 0)
|
|
|
|
throw new Error(errors.parserNeedDatas);
|
|
|
|
else
|
|
|
|
this._datas2Parse=datas.trim();
|
|
|
|
}
|
|
|
|
|
2021-10-01 13:12:45 +02:00
|
|
|
get datas2Parse() : string
|
|
|
|
{
|
|
|
|
return this._datas2Parse;
|
|
|
|
}
|
|
|
|
|
2021-09-29 17:56:10 +02:00
|
|
|
get parseResults() : ParseResults|undefined
|
|
|
|
{
|
|
|
|
return this._parseResults;
|
|
|
|
}
|
|
|
|
|
2021-10-01 13:12:45 +02:00
|
|
|
get privateOptions() : any
|
|
|
|
{
|
|
|
|
return this._privateOptions;
|
|
|
|
}
|
|
|
|
|
2021-09-29 17:56:10 +02:00
|
|
|
// async dans le cas d'une source distante
|
|
|
|
// Et création d'une Promise car PapaParse utilise une fonction callback.
|
|
|
|
public async parse(): Promise<any>
|
|
|
|
{
|
|
|
|
const parser=this, options=this.options;
|
|
|
|
return new Promise((resolve,reject) =>
|
|
|
|
{
|
2021-10-01 13:12:45 +02:00
|
|
|
let parseContent="";
|
|
|
|
if(parser._datasRemoteSource.url !== "")
|
2021-09-29 17:56:10 +02:00
|
|
|
{
|
|
|
|
parseContent=parser._datasRemoteSource.url;
|
2021-10-01 13:12:45 +02:00
|
|
|
this._privateOptions.download=true;
|
|
|
|
this._privateOptions.withCredentials=parser._datasRemoteSource.withCredentials;
|
2021-09-29 17:56:10 +02:00
|
|
|
if(parser._datasRemoteSource.headers !== undefined)
|
|
|
|
{
|
2021-10-01 13:12:45 +02:00
|
|
|
this._privateOptions.downloadRequestHeaders={};
|
2021-09-29 17:56:10 +02:00
|
|
|
for (let i in parser._datasRemoteSource.headers)
|
2021-10-01 13:12:45 +02:00
|
|
|
this._privateOptions.downloadRequestHeaders[""+parser._datasRemoteSource.headers[Number(i)].key+""]=parser._datasRemoteSource.headers[Number(i)].value;
|
2021-09-29 17:56:10 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-01 13:12:45 +02:00
|
|
|
else if(parser._datas2Parse !== "")
|
2021-09-29 17:56:10 +02:00
|
|
|
parseContent=parser._datas2Parse;
|
|
|
|
else
|
|
|
|
reject(new Error(errors.parserNeedSource));
|
|
|
|
|
|
|
|
Papa.parse(parseContent,
|
|
|
|
{
|
|
|
|
delimiter: options.delimiter,
|
|
|
|
newline: options.newline,
|
|
|
|
quoteChar: options.quoteChar,
|
|
|
|
escapeChar: options.escapeChar,
|
2021-10-01 13:12:45 +02:00
|
|
|
header: true,
|
2021-09-29 17:56:10 +02:00
|
|
|
transformHeader: options.transformHeader,
|
|
|
|
preview: options.preview,
|
|
|
|
comments: options.comments,
|
|
|
|
complete: function(results :any)
|
|
|
|
{
|
|
|
|
// Attention, Papa Parse peut accepter un nom de champ vide
|
|
|
|
let realFields: string[]=[];
|
|
|
|
for(let i in results.meta.fields)
|
|
|
|
{
|
|
|
|
if(results.meta.fields[i].trim() !== "")
|
|
|
|
realFields.push(results.meta.fields[i]);
|
|
|
|
}
|
|
|
|
if(realFields.length === 0)
|
|
|
|
reject(new Error(errors.parserFail));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
parser._parseResults={
|
|
|
|
datas: results.data,
|
|
|
|
errors: results.errors,
|
|
|
|
fields: realFields,
|
|
|
|
};
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
},
|
2021-10-01 13:12:45 +02:00
|
|
|
download: this._privateOptions.download,
|
|
|
|
downloadRequestHeaders: this._privateOptions.downloadRequestHeaders,
|
2021-09-29 17:56:10 +02:00
|
|
|
skipEmptyLines:"greedy",
|
|
|
|
fastMode: options.fastMode,
|
2021-10-01 13:12:45 +02:00
|
|
|
withCredentials: this._privateOptions.withCredentials,
|
|
|
|
transform: options.transform
|
2021-09-29 17:56:10 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|