Compare commits
7 Commits
c3c0206c64
...
c61b9ef402
Author | SHA1 | Date |
---|---|---|
Fabrice PENHOËT | c61b9ef402 | |
Fabrice PENHOËT | 1c61f715f8 | |
Fabrice PENHOËT | 4dc2753468 | |
Fabrice PENHOËT | 2b1eabd430 | |
Fabrice PENHOËT | d980412b0f | |
Fabrice PENHOËT | d90e38d5dc | |
Fabrice PENHOËT | 2b57e40b76 |
|
@ -1,56 +1,34 @@
|
|||
const Papa=require("papaparse");
|
||||
const errors= require("./errors.js");
|
||||
import { RemoteSource } from "./freeDatas2HTMLRemoteSource";
|
||||
const errors=require("./errors.js");
|
||||
import { RemoteSource } from "./RemoteSource";
|
||||
|
||||
import { ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
|
||||
|
||||
// Options de Papa Parse
|
||||
// cf. https://www.papaparse.com/docs#config
|
||||
interface PublicPapaParseOptions
|
||||
{
|
||||
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;
|
||||
}
|
||||
interface PrivatePapaParseOptions
|
||||
{
|
||||
header: boolean;
|
||||
download: boolean;
|
||||
downloadRequestHeaders: undefined| { [index: string]:string };
|
||||
skipEmptyLines: string;
|
||||
withCredentials: boolean|undefined;
|
||||
}
|
||||
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 =
|
||||
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 =
|
||||
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
|
||||
|
@ -97,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 !== "")
|
||||
{
|
||||
|
@ -120,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));
|
||||
|
@ -147,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);
|
||||
|
@ -156,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
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
const errors = require("./errors.js");
|
||||
import { RemoteSource } from "./freeDatas2HTMLRemoteSource";
|
||||
import { RemoteSource } from "./RemoteSource";
|
||||
|
||||
import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
|
||||
import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./interfaces";
|
||||
|
||||
export class ParserForJSON implements Parsers
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
const errors = require("./errors.js");
|
||||
|
||||
import { RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
|
||||
import { RemoteSources, RemoteSourceSettings } from "./interfaces";
|
||||
|
||||
export class RemoteSource implements RemoteSources
|
||||
{
|
|
@ -1,11 +1,11 @@
|
|||
const { compare }=require('natural-orderby');
|
||||
const errors=require("./errors.js");
|
||||
|
||||
import { Counter, Datas, DatasRenders, DOMElement, Paginations, Parsers, ParseErrors, RemoteSources, Selectors, SortingFields, SortingFunctions } from "./freeDatas2HTMLInterfaces";
|
||||
import { Counter, Datas, DatasRenders, DOMElement, Paginations, Parsers, ParseErrors, RemoteSources, Selectors, SortingFields, SortingFunctions } from "./interfaces";
|
||||
import { Pagination} from "./freeDatas2HTMLPagination";
|
||||
import { ParserForCSV} from "./ParserForCSV";
|
||||
import { ParserForHTML} from "./freeDatas2HTMLParserForHTML";
|
||||
import { ParserForJSON} from "./freeDatas2HTMLParserForJSON";
|
||||
import { ParserForJSON} from "./ParserForJSON";
|
||||
import { Render} from "./freeDatas2HTMLRender";
|
||||
import { Selector } from "./freeDatas2HTMLSelector";
|
||||
import { SortingField } from "./freeDatas2HTMLSortingField";
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const errors = require("./errors.js");
|
||||
import { DOMElement, Paginations, PaginationsOptions, PaginationsPages } from "./freeDatas2HTMLInterfaces";
|
||||
import { DOMElement, Paginations, PaginationsOptions, PaginationsPages } from "./interfaces";
|
||||
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
||||
|
||||
export class Pagination implements Paginations
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const errors=require("./errors.js");
|
||||
import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
|
||||
import { RemoteSource } from "./freeDatas2HTMLRemoteSource";
|
||||
import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./interfaces";
|
||||
import { RemoteSource } from "./RemoteSource";
|
||||
|
||||
export class ParserForHTML implements Parsers
|
||||
{
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
const errors = require("./errors.js");
|
||||
import { DatasRenders, DatasRendersSettings } from "./freeDatas2HTMLInterfaces";
|
||||
import { DatasRenders, DatasRendersSettings } from "./interfaces";
|
||||
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
||||
|
||||
export class Render implements DatasRenders
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const { compare }= require('natural-orderby');
|
||||
const errors = require("./errors.js");
|
||||
import { DOMElement, Selectors } from "./freeDatas2HTMLInterfaces";
|
||||
import { DOMElement, Selectors } from "./interfaces";
|
||||
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
||||
|
||||
export class Selector implements Selectors
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const { compare }=require('natural-orderby');
|
||||
const errors=require("./errors.js");
|
||||
import { SortingFields } from "./freeDatas2HTMLInterfaces";
|
||||
import { SortingFields } from "./interfaces";
|
||||
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
||||
|
||||
export class SortingField implements SortingFields
|
||||
|
|
|
@ -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
|
|
@ -0,0 +1,22 @@
|
|||
// Options de Papa Parse
|
||||
// cf. https://www.papaparse.com/docs#config
|
||||
export interface PublicPapaParseOptions
|
||||
{
|
||||
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;
|
||||
}
|
||||
export interface PrivatePapaParseOptions
|
||||
{
|
||||
header: boolean;
|
||||
download: boolean;
|
||||
downloadRequestHeaders: undefined| { [index: string]:string };
|
||||
skipEmptyLines: string;
|
||||
withCredentials: boolean|undefined;
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
const Papa=require("papaparse");
|
||||
import { RemoteSourceSettings } from "../src/freeDatas2HTMLInterfaces";
|
||||
import { RemoteSourceSettings } from "../src/interfaces";
|
||||
import { ParserForCSV as Parser } from "../src/ParserForCSV";
|
||||
const errors=require("../src/errors.js");
|
||||
|
||||
|
@ -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 () =>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { ParserForJSON as Parser } from "../src/freeDatas2HTMLParserForJSON";
|
||||
import { ParserForJSON as Parser } from "../src/ParserForJSON";
|
||||
const errors=require("../src/errors.js");
|
||||
|
||||
describe("Tests du parseur de JSON", () =>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { RemoteSource } from "../src/freeDatas2HTMLRemoteSource";
|
||||
import { RemoteSource } from "../src/RemoteSource";
|
||||
const errors=require("../src/errors.js");
|
||||
|
||||
describe("Tests des urls distantes", () =>
|
||||
|
|
Loading…
Reference in New Issue