FreeDatas2HTML/src/ParserForCSV.ts

143 lines
4.9 KiB
TypeScript
Raw Normal View History

const Papa=require("papaparse");
2021-10-18 11:27:53 +02:00
const errors=require("./errors.js");
2021-10-18 11:35:30 +02:00
import { RemoteSource } from "./RemoteSource";
2021-10-18 11:27:53 +02:00
import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./interfaces";
import { PublicPapaParseOptions, PrivatePapaParseOptions } from "./interfacesPapaParse";
export class ParserForCSV implements Parsers
{
private _datasRemoteSource: RemoteSources;
private _datas2Parse:string="";
private _parseResults:ParseResults|undefined=undefined;
public options: PublicPapaParseOptions=
{
2021-10-18 11:27:53 +02:00
delimiter: "",
newline: "",
quoteChar: '"',
escapeChar: '"',
transformHeader: function(field: string, index: number): string { return field.trim() },
preview: 0,
comments: "",
fastMode: undefined,
transform: undefined
}
private _privateOptions: PrivatePapaParseOptions=
{
2021-10-18 11:27:53 +02:00
header: true,
download: false,
downloadRequestHeaders: undefined,
skipEmptyLines: "greedy",
withCredentials: undefined
}
// L'instance d'une autre classe que RemoteSource peut être passée au constructeur
constructor(datasRemoteSource?: RemoteSources)
{
if(datasRemoteSource !== undefined)
this._datasRemoteSource=datasRemoteSource;
else
this._datasRemoteSource=new RemoteSource({ url:"" });
}
public setRemoteSource(source: RemoteSourceSettings)
{
this._datasRemoteSource=new RemoteSource(source);
}
get datasRemoteSource() : RemoteSources
{
return this._datasRemoteSource;
}
set datas2Parse(datas: string)
{
if(datas.trim().length === 0)
throw new Error(errors.parserNeedDatas);
else
this._datas2Parse=datas.trim();
}
get datas2Parse() : string
{
return this._datas2Parse;
}
get parseResults() : ParseResults|undefined
{
return this._parseResults;
}
get privateOptions() : any
{
return this._privateOptions;
}
2021-10-07 18:09:15 +02:00
public async parse(): Promise<any>
{
2021-10-18 11:27:53 +02:00
const parser=this;
2021-10-07 18:09:15 +02:00
let parseContent="";
if(parser._datasRemoteSource.url !== "")
{
2021-10-07 18:09:15 +02:00
parseContent=parser._datasRemoteSource.url;
this._privateOptions.download=true;
this._privateOptions.withCredentials=parser._datasRemoteSource.withCredentials;
if(parser._datasRemoteSource.headers !== undefined)
{
2021-10-07 18:09:15 +02:00
this._privateOptions.downloadRequestHeaders={};
for(let header of parser._datasRemoteSource.headers)
this._privateOptions.downloadRequestHeaders[header.key]=header.value;
2021-10-07 18:09:15 +02:00
}
}
else if(parser._datas2Parse !== "")
parseContent=parser._datas2Parse;
else
throw new Error(errors.parserNeedSource);
return new Promise((resolve,reject) =>
{
Papa.parse(parseContent,
{
2021-10-18 11:27:53 +02:00
delimiter: this.options.delimiter,
newline: this.options.newline,
quoteChar: this.options.quoteChar,
escapeChar: this.options.escapeChar,
header: true,
2021-10-18 11:27:53 +02:00
transformHeader: this.options.transformHeader,
preview: this.options.preview,
comments: this.options.comments,
complete: function(results :any)
{
2021-10-18 11:27:53 +02:00
// 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
2021-10-18 11:27:53 +02:00
parseErrors.push({ row:-1, message: errors.parserFieldNameFail});
}
if(realFields.length === 0)
reject(new Error(errors.parserFieldsNotFound));
else
{
parser._parseResults=
{
datas: results.data,
2021-10-18 11:27:53 +02:00
errors: parseErrors.concat(results.errors), // result.errors = errreurs rencontrées par Papa Parse
fields: realFields,
};
resolve(true);
}
},
download: this._privateOptions.download,
downloadRequestHeaders: this._privateOptions.downloadRequestHeaders,
skipEmptyLines:"greedy",
2021-10-18 11:27:53 +02:00
fastMode: this.options.fastMode,
withCredentials: this._privateOptions.withCredentials,
2021-10-18 11:27:53 +02:00
transform: this.options.transform
});
});
}
}