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 Papa=require("papaparse");
|
||||||
const errors= require("./errors.js");
|
const errors=require("./errors.js");
|
||||||
import { RemoteSource } from "./freeDatas2HTMLRemoteSource";
|
import { RemoteSource } from "./RemoteSource";
|
||||||
|
|
||||||
import { ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
|
import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./interfaces";
|
||||||
|
import { PublicPapaParseOptions, PrivatePapaParseOptions } from "./interfacesPapaParse";
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class ParserForCSV implements Parsers
|
export class ParserForCSV implements Parsers
|
||||||
{
|
{
|
||||||
private _datasRemoteSource: RemoteSources;
|
private _datasRemoteSource: RemoteSources;
|
||||||
private _datas2Parse:string="";
|
private _datas2Parse:string="";
|
||||||
private _parseResults:ParseResults|undefined=undefined;
|
private _parseResults:ParseResults|undefined=undefined;
|
||||||
public options: PublicPapaParseOptions =
|
public options: PublicPapaParseOptions=
|
||||||
{
|
{
|
||||||
delimiter:"",
|
delimiter: "",
|
||||||
newline:"",
|
newline: "",
|
||||||
quoteChar:'"',
|
quoteChar: '"',
|
||||||
escapeChar:'"',
|
escapeChar: '"',
|
||||||
transformHeader:function(field: string, index: number): string { return field.trim() },
|
transformHeader: function(field: string, index: number): string { return field.trim() },
|
||||||
preview:0,
|
preview: 0,
|
||||||
comments:"",
|
comments: "",
|
||||||
fastMode:undefined,
|
fastMode: undefined,
|
||||||
transform:undefined
|
transform: undefined
|
||||||
}
|
}
|
||||||
private _privateOptions: PrivatePapaParseOptions =
|
private _privateOptions: PrivatePapaParseOptions=
|
||||||
{
|
{
|
||||||
header:true,
|
header: true,
|
||||||
download:false,
|
download: false,
|
||||||
downloadRequestHeaders:undefined,
|
downloadRequestHeaders: undefined,
|
||||||
skipEmptyLines:"greedy",
|
skipEmptyLines: "greedy",
|
||||||
withCredentials:undefined
|
withCredentials: undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
// L'instance d'une autre classe que RemoteSource peut être passée au constructeur
|
// 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>
|
public async parse(): Promise<any>
|
||||||
{
|
{
|
||||||
const parser=this, options=this.options;
|
const parser=this;
|
||||||
let parseContent="";
|
let parseContent="";
|
||||||
if(parser._datasRemoteSource.url !== "")
|
if(parser._datasRemoteSource.url !== "")
|
||||||
{
|
{
|
||||||
|
@ -120,25 +98,25 @@ export class ParserForCSV implements Parsers
|
||||||
{
|
{
|
||||||
Papa.parse(parseContent,
|
Papa.parse(parseContent,
|
||||||
{
|
{
|
||||||
delimiter: options.delimiter,
|
delimiter: this.options.delimiter,
|
||||||
newline: options.newline,
|
newline: this.options.newline,
|
||||||
quoteChar: options.quoteChar,
|
quoteChar: this.options.quoteChar,
|
||||||
escapeChar: options.escapeChar,
|
escapeChar: this.options.escapeChar,
|
||||||
header: true,
|
header: true,
|
||||||
transformHeader: options.transformHeader,
|
transformHeader: this.options.transformHeader,
|
||||||
preview: options.preview,
|
preview: this.options.preview,
|
||||||
comments: options.comments,
|
comments: this.options.comments,
|
||||||
complete: function(results :any)
|
complete: function(results :any)
|
||||||
{
|
{
|
||||||
// Attention, Papa Parse peut accepter un nom de champ vide ou en doublon !
|
// Attention, Papa Parse peut accepter un nom de champ vide ou en doublon
|
||||||
let realFields: string[]=[];
|
let realFields: string[]=[], parseErrors: ParseErrors[]=[];
|
||||||
for(let field of results.meta.fields)
|
for(let field of results.meta.fields)
|
||||||
{
|
{
|
||||||
let checkField=field.trim();
|
let checkField=field.trim();
|
||||||
if(checkField !== "" && realFields.indexOf(checkField) === -1)
|
if(checkField !== "" && realFields.indexOf(checkField) === -1)
|
||||||
realFields.push(checkField);
|
realFields.push(checkField);
|
||||||
else
|
else
|
||||||
console.error(errors.parserFieldNameFail);
|
parseErrors.push({ row:-1, message: errors.parserFieldNameFail});
|
||||||
}
|
}
|
||||||
if(realFields.length === 0)
|
if(realFields.length === 0)
|
||||||
reject(new Error(errors.parserFieldsNotFound));
|
reject(new Error(errors.parserFieldsNotFound));
|
||||||
|
@ -147,7 +125,7 @@ export class ParserForCSV implements Parsers
|
||||||
parser._parseResults=
|
parser._parseResults=
|
||||||
{
|
{
|
||||||
datas: results.data,
|
datas: results.data,
|
||||||
errors: results.errors,
|
errors: parseErrors.concat(results.errors), // result.errors = errreurs rencontrées par Papa Parse
|
||||||
fields: realFields,
|
fields: realFields,
|
||||||
};
|
};
|
||||||
resolve(true);
|
resolve(true);
|
||||||
|
@ -156,9 +134,9 @@ export class ParserForCSV implements Parsers
|
||||||
download: this._privateOptions.download,
|
download: this._privateOptions.download,
|
||||||
downloadRequestHeaders: this._privateOptions.downloadRequestHeaders,
|
downloadRequestHeaders: this._privateOptions.downloadRequestHeaders,
|
||||||
skipEmptyLines:"greedy",
|
skipEmptyLines:"greedy",
|
||||||
fastMode: options.fastMode,
|
fastMode: this.options.fastMode,
|
||||||
withCredentials: this._privateOptions.withCredentials,
|
withCredentials: this._privateOptions.withCredentials,
|
||||||
transform: options.transform
|
transform: this.options.transform
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const errors = require("./errors.js");
|
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
|
export class ParserForJSON implements Parsers
|
||||||
{
|
{
|
|
@ -1,6 +1,6 @@
|
||||||
const errors = require("./errors.js");
|
const errors = require("./errors.js");
|
||||||
|
|
||||||
import { RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
|
import { RemoteSources, RemoteSourceSettings } from "./interfaces";
|
||||||
|
|
||||||
export class RemoteSource implements RemoteSources
|
export class RemoteSource implements RemoteSources
|
||||||
{
|
{
|
|
@ -1,11 +1,11 @@
|
||||||
const { compare }=require('natural-orderby');
|
const { compare }=require('natural-orderby');
|
||||||
const errors=require("./errors.js");
|
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 { Pagination} from "./freeDatas2HTMLPagination";
|
||||||
import { ParserForCSV} from "./ParserForCSV";
|
import { ParserForCSV} from "./ParserForCSV";
|
||||||
import { ParserForHTML} from "./freeDatas2HTMLParserForHTML";
|
import { ParserForHTML} from "./freeDatas2HTMLParserForHTML";
|
||||||
import { ParserForJSON} from "./freeDatas2HTMLParserForJSON";
|
import { ParserForJSON} from "./ParserForJSON";
|
||||||
import { Render} from "./freeDatas2HTMLRender";
|
import { Render} from "./freeDatas2HTMLRender";
|
||||||
import { Selector } from "./freeDatas2HTMLSelector";
|
import { Selector } from "./freeDatas2HTMLSelector";
|
||||||
import { SortingField } from "./freeDatas2HTMLSortingField";
|
import { SortingField } from "./freeDatas2HTMLSortingField";
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const errors = require("./errors.js");
|
const errors = require("./errors.js");
|
||||||
import { DOMElement, Paginations, PaginationsOptions, PaginationsPages } from "./freeDatas2HTMLInterfaces";
|
import { DOMElement, Paginations, PaginationsOptions, PaginationsPages } from "./interfaces";
|
||||||
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
||||||
|
|
||||||
export class Pagination implements Paginations
|
export class Pagination implements Paginations
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const errors=require("./errors.js");
|
const errors=require("./errors.js");
|
||||||
import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./freeDatas2HTMLInterfaces";
|
import { ParseErrors, ParseResults, Parsers, RemoteSources, RemoteSourceSettings } from "./interfaces";
|
||||||
import { RemoteSource } from "./freeDatas2HTMLRemoteSource";
|
import { RemoteSource } from "./RemoteSource";
|
||||||
|
|
||||||
export class ParserForHTML implements Parsers
|
export class ParserForHTML implements Parsers
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const errors = require("./errors.js");
|
const errors = require("./errors.js");
|
||||||
import { DatasRenders, DatasRendersSettings } from "./freeDatas2HTMLInterfaces";
|
import { DatasRenders, DatasRendersSettings } from "./interfaces";
|
||||||
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
||||||
|
|
||||||
export class Render implements DatasRenders
|
export class Render implements DatasRenders
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const { compare }= require('natural-orderby');
|
const { compare }= require('natural-orderby');
|
||||||
const errors = require("./errors.js");
|
const errors = require("./errors.js");
|
||||||
import { DOMElement, Selectors } from "./freeDatas2HTMLInterfaces";
|
import { DOMElement, Selectors } from "./interfaces";
|
||||||
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
||||||
|
|
||||||
export class Selector implements Selectors
|
export class Selector implements Selectors
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const { compare }=require('natural-orderby');
|
const { compare }=require('natural-orderby');
|
||||||
const errors=require("./errors.js");
|
const errors=require("./errors.js");
|
||||||
import { SortingFields } from "./freeDatas2HTMLInterfaces";
|
import { SortingFields } from "./interfaces";
|
||||||
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
import { FreeDatas2HTML } from "./freeDatas2HTML";
|
||||||
|
|
||||||
export class SortingField implements SortingFields
|
export class SortingField implements SortingFields
|
||||||
|
|
|
@ -54,7 +54,7 @@ export interface ParseErrors
|
||||||
{
|
{
|
||||||
code?: string;
|
code?: string;
|
||||||
message: string;
|
message: string;
|
||||||
row: number;
|
row: number; // -1 quand bug avant de traiter les lignes
|
||||||
type?: string;
|
type?: string;
|
||||||
}
|
}
|
||||||
export interface ParseResults
|
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");
|
const Papa=require("papaparse");
|
||||||
import { RemoteSourceSettings } from "../src/freeDatas2HTMLInterfaces";
|
import { RemoteSourceSettings } from "../src/interfaces";
|
||||||
import { ParserForCSV as Parser } from "../src/ParserForCSV";
|
import { ParserForCSV as Parser } from "../src/ParserForCSV";
|
||||||
const errors=require("../src/errors.js");
|
const errors=require("../src/errors.js");
|
||||||
|
|
||||||
|
@ -82,11 +82,12 @@ describe("Tests du parseur de CSV", () =>
|
||||||
expect(Papa.parse).toHaveBeenCalledTimes(2);
|
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();
|
await parser.parse();
|
||||||
expect(parser.parseResults.fields).toEqual(["field1","field2","field3"]);
|
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 () =>
|
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");
|
const errors=require("../src/errors.js");
|
||||||
|
|
||||||
describe("Tests du parseur de JSON", () =>
|
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");
|
const errors=require("../src/errors.js");
|
||||||
|
|
||||||
describe("Tests des urls distantes", () =>
|
describe("Tests des urls distantes", () =>
|
||||||
|
|
Loading…
Reference in New Issue