Compare commits
8 Commits
d56473ff41
...
04b4bc2389
Author | SHA1 | Date | |
---|---|---|---|
04b4bc2389 | |||
09e5c1a074 | |||
a7f1ecc5f9 | |||
35379b8236 | |||
aa2e1446f9 | |||
475a7c97d7 | |||
0feb525725 | |||
4c6bf36038 |
@ -12,8 +12,10 @@ module.exports = function(config) {
|
|||||||
frameworks: ["jasmine", "karma-typescript"],
|
frameworks: ["jasmine", "karma-typescript"],
|
||||||
|
|
||||||
// list of files / patterns to load in the browser
|
// list of files / patterns to load in the browser
|
||||||
files: [ "src/freeCSV2HTML.ts", "tests/*.ts" ],
|
files: [ "src/freeCSV2HTML.ts", "tests/*.ts" , { pattern: 'tests/datas/*.csv', watched: true, included: false, served: true } ],
|
||||||
|
|
||||||
|
proxies: { "/datas/": "http://localhost:9876/base/tests/datas/" },
|
||||||
|
|
||||||
// list of files / patterns to exclude
|
// list of files / patterns to exclude
|
||||||
exclude: [
|
exclude: [
|
||||||
],
|
],
|
||||||
@ -45,7 +47,8 @@ module.exports = function(config) {
|
|||||||
|
|
||||||
// start these browsers
|
// start these browsers
|
||||||
// available browser launchers: https://www.npmjs.com/search?q=keywords:karma-launcher
|
// available browser launchers: https://www.npmjs.com/search?q=keywords:karma-launcher
|
||||||
browsers: ['Firefox', 'Chromium'],
|
browsers: ['Firefox'],
|
||||||
|
//browsers: ['Firefox', 'Chromium'],
|
||||||
|
|
||||||
// Continuous Integration mode
|
// Continuous Integration mode
|
||||||
// if true, Karma captures browsers, runs the tests and exits
|
// if true, Karma captures browsers, runs the tests and exits
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
Z (numéro atomique),Élément,Symbole,Famille,Abondance des éléments dans la croûte terrestre (μg/k)
|
Z (numéro atomique),Élément,Symbole,Famille,Abondance des éléments dans la croûte terrestre (μg/k)
|
||||||
1,Hydrogène,H,Non-métal,1400000
|
1,Hydrogène,H,Non-métal,1400000
|
||||||
2,Hélium,He,Gaz noble,8
|
2,Hélium,He,Gaz noble,8
|
||||||
|
|
BIN
public/favicon.ico
Normal file
BIN
public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
53
src/build/firstExample.js
Normal file
53
src/build/firstExample.js
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||||
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||||||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||||
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||||
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||||
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||||
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||||
|
function step(op) {
|
||||||
|
if (f) throw new TypeError("Generator is already executing.");
|
||||||
|
while (_) try {
|
||||||
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||||
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||||
|
switch (op[0]) {
|
||||||
|
case 0: case 1: t = op; break;
|
||||||
|
case 4: _.label++; return { value: op[1], done: false };
|
||||||
|
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||||
|
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||||
|
default:
|
||||||
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||||
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||||
|
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||||
|
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||||
|
if (t[2]) _.ops.pop();
|
||||||
|
_.trys.pop(); continue;
|
||||||
|
}
|
||||||
|
op = body.call(thisArg, _);
|
||||||
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||||
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
import { freeCSV2HTML } from "./freeCSV2HTML";
|
||||||
|
var initialise = function () { return __awaiter(void 0, void 0, void 0, function () {
|
||||||
|
var converter;
|
||||||
|
return __generator(this, function (_a) {
|
||||||
|
try {
|
||||||
|
converter = new freeCSV2HTML();
|
||||||
|
converter.datasViewElt = { id: "datas" };
|
||||||
|
converter.datasSourceUrl = "http://localhost:8080/datas/elements-chimiques.csv";
|
||||||
|
console.log(converter);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
return [2];
|
||||||
|
});
|
||||||
|
}); };
|
||||||
|
initialise();
|
129
src/build/freeCSV2HTML.js
Normal file
129
src/build/freeCSV2HTML.js
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||||
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||||
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||||||
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||||
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||||
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||||
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
||||||
|
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
||||||
|
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
||||||
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
||||||
|
function step(op) {
|
||||||
|
if (f) throw new TypeError("Generator is already executing.");
|
||||||
|
while (_) try {
|
||||||
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
||||||
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
||||||
|
switch (op[0]) {
|
||||||
|
case 0: case 1: t = op; break;
|
||||||
|
case 4: _.label++; return { value: op[1], done: false };
|
||||||
|
case 5: _.label++; y = op[1]; op = [0]; continue;
|
||||||
|
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
||||||
|
default:
|
||||||
|
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
||||||
|
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
||||||
|
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
||||||
|
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
||||||
|
if (t[2]) _.ops.pop();
|
||||||
|
_.trys.pop(); continue;
|
||||||
|
}
|
||||||
|
op = body.call(thisArg, _);
|
||||||
|
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
||||||
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var Papa = require("papaparse");
|
||||||
|
var errors = require("./errors.js");
|
||||||
|
var freeCSV2HTML = (function () {
|
||||||
|
function freeCSV2HTML() {
|
||||||
|
this._datasViewElt = { id: "", eltDOM: undefined };
|
||||||
|
this._datasSourceUrl = "";
|
||||||
|
this._datasSelectors = [];
|
||||||
|
this.parseMeta = undefined;
|
||||||
|
this.parseErrors = [];
|
||||||
|
this.parseDatas = [];
|
||||||
|
}
|
||||||
|
Object.defineProperty(freeCSV2HTML.prototype, "datasViewElt", {
|
||||||
|
set: function (elt) {
|
||||||
|
var checkContainerExist = document.getElementById(elt.id);
|
||||||
|
if (checkContainerExist === null)
|
||||||
|
throw new Error(errors.elementNotFound + elt.id);
|
||||||
|
else {
|
||||||
|
this._datasViewElt.id = elt.id;
|
||||||
|
this._datasViewElt.eltDOM = checkContainerExist;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
Object.defineProperty(freeCSV2HTML.prototype, "datasSelectors", {
|
||||||
|
set: function (selectors) {
|
||||||
|
var checkContainerExist;
|
||||||
|
for (var i = 0; i < selectors.length; i++) {
|
||||||
|
checkContainerExist = document.getElementById(selectors[i].id);
|
||||||
|
if (checkContainerExist === null)
|
||||||
|
throw new Error(errors.elementNotFound + selectors[i].id);
|
||||||
|
else if (Number.isInteger(selectors[i].colCSV) === false || selectors[i].colCSV < 0)
|
||||||
|
throw new Error(errors.needNaturalNumber);
|
||||||
|
else
|
||||||
|
selectors[i].eltDOM = checkContainerExist;
|
||||||
|
}
|
||||||
|
this._datasSelectors = selectors;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
Object.defineProperty(freeCSV2HTML.prototype, "datasSourceUrl", {
|
||||||
|
set: function (url) {
|
||||||
|
if (url.trim().length === 0)
|
||||||
|
throw new Error(errors.needUrl);
|
||||||
|
else
|
||||||
|
this._datasSourceUrl = url.trim();
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
freeCSV2HTML.prototype.checkParameters = function () {
|
||||||
|
if (this._datasViewElt === undefined)
|
||||||
|
throw new Error(errors.needDatasElt);
|
||||||
|
if (this._datasSourceUrl === "")
|
||||||
|
throw new Error(errors.needUrl);
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
freeCSV2HTML.prototype.parse = function () {
|
||||||
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
|
var converter;
|
||||||
|
var _this = this;
|
||||||
|
return __generator(this, function (_a) {
|
||||||
|
converter = this;
|
||||||
|
return [2, new Promise(function (resolve, reject) {
|
||||||
|
if (_this.checkParameters()) {
|
||||||
|
Papa.parse(_this._datasSourceUrl, {
|
||||||
|
quoteChar: '"',
|
||||||
|
header: true,
|
||||||
|
complete: function (results) {
|
||||||
|
converter.parseMeta = results.meta;
|
||||||
|
converter.parseErrors = results.errors;
|
||||||
|
converter.parseDatas = results.data;
|
||||||
|
resolve(true);
|
||||||
|
},
|
||||||
|
download: true,
|
||||||
|
skipEmptyLines: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
freeCSV2HTML.prototype.run = function () {
|
||||||
|
return __awaiter(this, void 0, void 0, function () {
|
||||||
|
return __generator(this, function (_a) {
|
||||||
|
return [2, true];
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
return freeCSV2HTML;
|
||||||
|
}());
|
||||||
|
export { freeCSV2HTML };
|
0
src/build/papaParseInterfaces.js
Normal file
0
src/build/papaParseInterfaces.js
Normal file
@ -1,5 +1,9 @@
|
|||||||
module.exports =
|
module.exports =
|
||||||
{
|
{
|
||||||
elementNotFound : "Aucun élément HTML trouvé dans la page pour l'id : ",
|
datasNotFound : "Aucune donnée n'a été trouvée.",
|
||||||
urlNotValid: "L'url fournie pour le fichier CSS n 'est pas correcte : ",
|
elementNotFound : "Aucun élément HTML n'a été trouvé ayant comme \"id\" : ",
|
||||||
|
needDatasElt: "Merci de fournir un id valide pour l'élément où afficher les données.",
|
||||||
|
needNaturalNumber: "Merci de fournir un nombre entier supérieur ou égal à zéro pour désigner chaque colonne.",
|
||||||
|
needUrl: "Merci de fournir l'url du fichier CSV à parser.",
|
||||||
|
parserFail: "La lecture des données du fichier a échoué.",
|
||||||
};
|
};
|
@ -0,0 +1,90 @@
|
|||||||
|
import { freeCSV2HTML } from "./freeCSV2HTML";
|
||||||
|
|
||||||
|
const initialise = async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
let converter=new freeCSV2HTML();
|
||||||
|
converter.datasViewElt={ id:"datas" };
|
||||||
|
converter.datasSourceUrl="http://localhost:8080/datas/elements-chimiques.csv";
|
||||||
|
await converter.run();
|
||||||
|
}
|
||||||
|
catch(e)
|
||||||
|
{
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
initialise();
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
public async setDatasUrl(url: string) : Promise<any> // pas de setter classique à cause asynchrone
|
||||||
|
{
|
||||||
|
const converter=this; // nécessaire pour éviter confusion avec this à l'intérieur de XMLHttpRequest
|
||||||
|
await fetch(url).then(function(response)
|
||||||
|
{
|
||||||
|
if(response.ok)
|
||||||
|
{
|
||||||
|
converter._datasUrl=url;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if(converter._datasUrl!=="")
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
let req = new XMLHttpRequest();
|
||||||
|
req.open("GET", url);
|
||||||
|
req.addEventListener("load", function ()
|
||||||
|
{
|
||||||
|
if (req.status >= 200 && req.status < 400)
|
||||||
|
{
|
||||||
|
converter._datasUrl=url;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
console.error("Fichier non trouvé");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}*/
|
||||||
|
|
||||||
|
|
||||||
|
/* Exemple d'appelle complet à papaparse :
|
||||||
|
Papa.parse(url, {
|
||||||
|
delimiter :"",
|
||||||
|
newline: "",
|
||||||
|
quoteChar:'"',// à proposer en option
|
||||||
|
escapeChar:"",// idem
|
||||||
|
header:true,
|
||||||
|
transformHeader:"",
|
||||||
|
dynamicTyping:false,
|
||||||
|
preview:0,
|
||||||
|
encoding:"",
|
||||||
|
worker:true,
|
||||||
|
comments:"",
|
||||||
|
step:"",// prévoir en option pour très gros fichiers ?
|
||||||
|
complete: function(results :any)
|
||||||
|
{
|
||||||
|
console.log(results.meta);
|
||||||
|
console.log(results.data[35]);
|
||||||
|
console.log(results.errors);
|
||||||
|
},
|
||||||
|
error:function(error :any)
|
||||||
|
{
|
||||||
|
console.log(error);
|
||||||
|
},
|
||||||
|
download: true,// nécessaire pour un accès via "http..."
|
||||||
|
downloadRequestBody:false,
|
||||||
|
skipEmptyLines: true,
|
||||||
|
chunk:"",
|
||||||
|
fastMode:"",
|
||||||
|
beforeFirstChunk:"",
|
||||||
|
withCredentials:false,
|
||||||
|
transform: "", // à revoir
|
||||||
|
delimitersToGuess:""
|
||||||
|
});
|
||||||
|
* // https://www.papaparse.com/docs#config*/
|
@ -1,31 +1,135 @@
|
|||||||
|
const Papa = require("papaparse");
|
||||||
|
|
||||||
|
import { papaParseDatas, papaParseErrors, papaParseMeta } from "./papaParseInterfaces";
|
||||||
const errors=require("./errors.js");
|
const errors=require("./errors.js");
|
||||||
|
|
||||||
interface selectors
|
interface domElement
|
||||||
|
{
|
||||||
|
id: string;
|
||||||
|
eltDOM?: HTMLElement;
|
||||||
|
}
|
||||||
|
interface selectors extends domElement
|
||||||
{
|
{
|
||||||
colCSV: number;
|
colCSV: number;
|
||||||
idElt: string;
|
|
||||||
eltDOM?: HTMLElement;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class freeCSV2HTML
|
export class freeCSV2HTML
|
||||||
{
|
{
|
||||||
private _datasEltId: string = "";
|
// Configuration :
|
||||||
private _datasUrl: string = "";
|
private _datasViewElt: domElement = { id:"", eltDOM:undefined };
|
||||||
|
private _datasSourceUrl: string = "";
|
||||||
private _datasSelectors: selectors[] = [];
|
private _datasSelectors: selectors[] = [];
|
||||||
|
|
||||||
|
// Données reçues ou générées suite parsage du fichier CSV :
|
||||||
|
public parseMeta: papaParseMeta|undefined = undefined;
|
||||||
|
public parseDatas: papaParseDatas[] = [];
|
||||||
|
public parseErrors: papaParseErrors[] = [];
|
||||||
|
private datasHTML: string = "";
|
||||||
|
|
||||||
set datasEltId(id: string)
|
set datasViewElt(elt: domElement)
|
||||||
{
|
{
|
||||||
this._datasEltId=id;
|
let checkContainerExist=document.getElementById(elt.id);
|
||||||
|
if(checkContainerExist === null)
|
||||||
|
throw new Error(errors.elementNotFound+elt.id);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this._datasViewElt.id=elt.id;
|
||||||
|
this._datasViewElt.eltDOM=checkContainerExist;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
set datasUrl(url: string)
|
// Pas plus de tests car nombreux protocoles possibles pour l'url du fichier, pas uniquement http(s). À revoir.
|
||||||
|
set datasSourceUrl(url: string)
|
||||||
{
|
{
|
||||||
this._datasUrl=url;
|
if(url.trim().length === 0)
|
||||||
|
throw new Error(errors.needUrl);
|
||||||
|
else
|
||||||
|
this._datasSourceUrl=url.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// La validité du numéro de colonne ne peut être vérifée qu'après le parsage.
|
||||||
set datasSelectors(selectors: selectors[])
|
set datasSelectors(selectors: selectors[])
|
||||||
{
|
{
|
||||||
|
let checkContainerExist: HTMLElement|null;
|
||||||
|
for(let i = 0; i < selectors.length; i++)
|
||||||
|
{
|
||||||
|
checkContainerExist=document.getElementById(selectors[i].id);
|
||||||
|
if(checkContainerExist === null)
|
||||||
|
throw new Error(errors.elementNotFound+selectors[i].id);
|
||||||
|
else if(Number.isInteger(selectors[i].colCSV) === false || selectors[i].colCSV < 0)
|
||||||
|
throw new Error(errors.needNaturalNumber);
|
||||||
|
else
|
||||||
|
selectors[i].eltDOM=checkContainerExist;
|
||||||
|
}
|
||||||
this._datasSelectors=selectors;
|
this._datasSelectors=selectors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async parse(): Promise<any>
|
||||||
|
{
|
||||||
|
const converter=this; // nécessaire pour éviter confusion avec le "this" de la promise
|
||||||
|
return new Promise((resolve,reject) =>
|
||||||
|
{
|
||||||
|
if(this._datasSourceUrl !== "" )
|
||||||
|
{
|
||||||
|
Papa.parse(this._datasSourceUrl,
|
||||||
|
{
|
||||||
|
quoteChar:'"', // à proposer en option ?
|
||||||
|
header:true,
|
||||||
|
complete: function(results :any)
|
||||||
|
{
|
||||||
|
converter.parseMeta=results.meta;
|
||||||
|
converter.parseErrors=results.errors; // prévoir option bloquant la suite à la moindre erreur trouvée ?
|
||||||
|
converter.parseDatas=results.data;
|
||||||
|
resolve(true);
|
||||||
|
},
|
||||||
|
error:function(error :any)
|
||||||
|
{
|
||||||
|
reject(new Error(errors.parserFail));
|
||||||
|
},
|
||||||
|
download: true,
|
||||||
|
skipEmptyLines: true, // à proposer en option ?
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
resolve(new Error(errors.needUrl));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async run(): Promise<any>
|
||||||
|
{
|
||||||
|
if (this._datasViewElt.eltDOM === undefined)
|
||||||
|
throw new Error(errors.needDatasElt);
|
||||||
|
|
||||||
|
if(this._datasSourceUrl === "" )
|
||||||
|
throw new Error(errors.needUrl);
|
||||||
|
|
||||||
|
if(await this.parse() === true)
|
||||||
|
{
|
||||||
|
if(this.parseDatas.length === 0 || this.parseMeta === undefined || this.parseMeta.fields === undefined)
|
||||||
|
{
|
||||||
|
this._datasViewElt.eltDOM.innerHTML=errors.datasNotFound;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.datasHTML="<table><thead>";
|
||||||
|
for (let i in this.parseMeta.fields)
|
||||||
|
this.datasHTML+="<th>"+this.parseMeta.fields[i]+"</th>";
|
||||||
|
this.datasHTML+="</thead><tbody>";
|
||||||
|
for (let row in this.parseDatas)
|
||||||
|
{
|
||||||
|
this.datasHTML+="<tr>";
|
||||||
|
for(let field in this.parseDatas[row])
|
||||||
|
{
|
||||||
|
if( this.parseMeta.fields.indexOf(field) !== -1) // si les erreurs papaParse sont acceptées, il peut y avoir des données en trop : "__parsed_extra"
|
||||||
|
this.datasHTML+="<td>"+this.parseDatas[row][field]+"</td>";
|
||||||
|
}
|
||||||
|
this.datasHTML+="</tr>";
|
||||||
|
}
|
||||||
|
this.datasHTML+="</tbody></table>";
|
||||||
|
this._datasViewElt.eltDOM.innerHTML=this.datasHTML;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
23
src/papaParseInterfaces.ts
Normal file
23
src/papaParseInterfaces.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// cf. https://www.papaparse.com/docs#results
|
||||||
|
|
||||||
|
export interface papaParseDatas
|
||||||
|
{
|
||||||
|
[key: string]: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface papaParseErrors
|
||||||
|
{
|
||||||
|
type: string;
|
||||||
|
code: string;
|
||||||
|
message: string;
|
||||||
|
row: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface papaParseMeta
|
||||||
|
{
|
||||||
|
delimiter?: string;
|
||||||
|
linebreak?: string;
|
||||||
|
aborted?: boolean;
|
||||||
|
fields?: string[];
|
||||||
|
truncated?: boolean;
|
||||||
|
}
|
14
tests/datas/elements-chimiques-erreurs.csv
Normal file
14
tests/datas/elements-chimiques-erreurs.csv
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
Z (numéro atomique),Élément,Symbole,Famille
|
||||||
|
1,Hydrogène,H,Non-métal,1400000
|
||||||
|
2,Hélium,He,Gaz noble,8
|
||||||
|
3,Lithium,Li,Métal alcalin,20000
|
||||||
|
4,Béryllium,Be,Métal alcalino-terreux,2800
|
||||||
|
5,Bore,B,Métalloïde,10000
|
||||||
|
6,Carbone,C,Non-métal,200000
|
||||||
|
7,Azote,N,Non-métal,19000
|
||||||
|
8,Oxygène,O,Non-métal,461000000
|
||||||
|
9,Fluor,F,Halogène,585000
|
||||||
|
10,Néon,Ne,Gaz noble,5
|
||||||
|
11,Sodium,Na,Métal alcalin,23600000
|
||||||
|
12,Magnésium,Mg,Métal alcalino-terreux,23300000
|
||||||
|
13,Aluminium,Al,Métal pauvre,82300000
|
|
0
tests/datas/elements-chimiques-vide.csv
Normal file
0
tests/datas/elements-chimiques-vide.csv
Normal file
|
119
tests/datas/elements-chimiques.csv
Normal file
119
tests/datas/elements-chimiques.csv
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
Z (numéro atomique),Élément,Symbole,Famille,Abondance des éléments dans la croûte terrestre (μg/k)
|
||||||
|
1,Hydrogène,H,Non-métal,1400000
|
||||||
|
2,Hélium,He,Gaz noble,8
|
||||||
|
3,Lithium,Li,Métal alcalin,20000
|
||||||
|
4,Béryllium,Be,Métal alcalino-terreux,2800
|
||||||
|
5,Bore,B,Métalloïde,10000
|
||||||
|
6,Carbone,C,Non-métal,200000
|
||||||
|
7,Azote,N,Non-métal,19000
|
||||||
|
8,Oxygène,O,Non-métal,461000000
|
||||||
|
9,Fluor,F,Halogène,585000
|
||||||
|
10,Néon,Ne,Gaz noble,5
|
||||||
|
11,Sodium,Na,Métal alcalin,23600000
|
||||||
|
12,Magnésium,Mg,Métal alcalino-terreux,23300000
|
||||||
|
13,Aluminium,Al,Métal pauvre,82300000
|
||||||
|
14,Silicium,Si,Métalloïde,282000000
|
||||||
|
15,Phosphore,P,Non-métal,1050000
|
||||||
|
16,Soufre,S,Non-métal,350000
|
||||||
|
17,Chlore,Cl,Halogène,145000
|
||||||
|
18,Argon,Ar,Gaz noble,3500
|
||||||
|
19,Potassium,K,Métal alcalin,20900000
|
||||||
|
20,Calcium,Ca,Métal alcalino-terreux,41500000
|
||||||
|
21,Scandium,Sc,Métal de transition,22000
|
||||||
|
22,Titane,Ti,Métal de transition,5650000
|
||||||
|
23,Vanadium,V,Métal de transition,120000
|
||||||
|
24,Chrome,Cr,Métal de transition,102000
|
||||||
|
25,Manganèse,Mn,Métal de transition,950000
|
||||||
|
26,Fer,Fe,Métal de transition,56300000
|
||||||
|
27,Cobalt,Co,Métal de transition,25000
|
||||||
|
28,Nickel,Ni,Métal de transition,84000
|
||||||
|
29,Cuivre,Cu,Métal de transition,60000
|
||||||
|
30,Zinc,Zn,Métal pauvre,70000
|
||||||
|
31,Gallium,Ga,Métal pauvre,19000
|
||||||
|
32,Germanium,Ge,Métalloïde,1500
|
||||||
|
33,Arsenic,As,Métalloïde,1800
|
||||||
|
34,Sélénium,Se,Non-métal,50
|
||||||
|
35,Brome,Br,Halogène,2400
|
||||||
|
36,Krypton,Kr,gaz rare,"0,1"
|
||||||
|
37,Rubidium,Rb,Métal alcalin,90000
|
||||||
|
38,Strontium,Sr,Métal alcalino-terreux,370000
|
||||||
|
39,Yttrium,Y,Métal de transition,33000
|
||||||
|
40,Zirconium,Zr,Métal de transition,165000
|
||||||
|
41,Niobium,Nb,Métal de transition,20000
|
||||||
|
42,Molybdène,Mo,Métal de transition,1200
|
||||||
|
43,Technétium,Tc,Métal de transition,Traces
|
||||||
|
44,Ruthénium,Ru,Métal de transition,1
|
||||||
|
45,Rhodium,Rh,Métal de transition,1
|
||||||
|
46,Palladium,Pd,Métal de transition,15
|
||||||
|
47,Argent,Ag,Métal de transition,75
|
||||||
|
48,Cadmium,Cd,Métal pauvre,150
|
||||||
|
49,Indium,In,Métal pauvre,250
|
||||||
|
50,Étain,Sn,Métal pauvre,2300
|
||||||
|
51,Antimoine,Sb,Métalloïde,200
|
||||||
|
52,Tellure,Te,Métalloïde,1
|
||||||
|
53,Iode,I,Halogène,450
|
||||||
|
54,Xénon,Xe,gaz rare,"0,03"
|
||||||
|
55,Césium,Cs,Métal alcalin,3000
|
||||||
|
56,Baryum,Ba,Métal alcalino-terreux,425000
|
||||||
|
57,Lanthane,La,Lanthanide,39000
|
||||||
|
58,Cérium,Ce,Lanthanide,66500
|
||||||
|
59,Praséodyme,Pr,Lanthanide,9200
|
||||||
|
60,Néodyme,Nd,Lanthanide,41500
|
||||||
|
61,Prométhium,Pm,Lanthanide,Traces
|
||||||
|
62,Samarium,Sm,Lanthanide,7050
|
||||||
|
63,Europium,Eu,Lanthanide,2000
|
||||||
|
64,Gadolinium,Gd,Lanthanide,6200
|
||||||
|
65,Terbium,Tb,Lanthanide,1200
|
||||||
|
66,Dysprosium,Dy,Lanthanide,5200
|
||||||
|
67,Holmium,Ho,Lanthanide,1300
|
||||||
|
68,Erbium,Er,Lanthanide,3500
|
||||||
|
69,Thulium,Tm,Lanthanide,520
|
||||||
|
70,Ytterbium,Yb,Lanthanide,3200
|
||||||
|
71,Lutécium,Lu,Lanthanide,800
|
||||||
|
72,Hafnium,Hf,Métal de transition,3000
|
||||||
|
73,Tantale,Ta,Métal de transition,2000
|
||||||
|
74,Tungstène,W,Métal de transition,1250
|
||||||
|
75,Rhénium,Re,Métal de transition,"0,7"
|
||||||
|
76,Osmium,Os,Métal de transition,"1,5"
|
||||||
|
77,Iridium,Ir,Métal de transition,1
|
||||||
|
78,Platine,Pt,Métal de transition,5
|
||||||
|
79,Or,Au,Métal de transition,4
|
||||||
|
80,Mercure,Hg,Métal pauvre,85
|
||||||
|
81,Thallium,Tl,Métal pauvre,850
|
||||||
|
82,Plomb,Pb,Métal pauvre,14000
|
||||||
|
83,Bismuth,Bi,Métal pauvre,"8,5"
|
||||||
|
84,Polonium,Po,Métal pauvre,200×10−9
|
||||||
|
85,Astate,At,Métalloïde,Traces
|
||||||
|
86,Radon,Rn,Gaz noble,400×10−12
|
||||||
|
87,Francium,Fr,Métal alcalin,Traces
|
||||||
|
88,Radium,Ra,Métal alcalino-terreux,900×10−6
|
||||||
|
89,Actinium,Ac,Actinide,550×10−9
|
||||||
|
90,Thorium,Th,Actinide,9600
|
||||||
|
91,Protactinium,Pa,Actinide,"1,4×10−3 "
|
||||||
|
92,Uranium,U,Actinide,2700
|
||||||
|
93,Neptunium,Np,Actinide,Traces
|
||||||
|
94,Plutonium,Pu,Actinide,Traces
|
||||||
|
95,Américium,Am,Actinide,—
|
||||||
|
96,Curium,Cm,Actinide,—
|
||||||
|
97,Berkélium,Bk,Actinide,—
|
||||||
|
98,Californium,Cf,Actinide,—
|
||||||
|
99,Einsteinium,Es,Actinide,—
|
||||||
|
100,Fermium,Fm,Actinide,—
|
||||||
|
101,Mendélévium,Md,Actinide,—
|
||||||
|
102,Nobélium,No,Actinide,—
|
||||||
|
103,Lawrencium,Lr,Actinide,—
|
||||||
|
104,Rutherfordium,Rf,Métal de transition,—
|
||||||
|
105,Dubnium,Db,Métal de transition,—
|
||||||
|
106,Seaborgium,Sg,Métal de transition,—
|
||||||
|
107,Bohrium,Bh,Métal de transition,—
|
||||||
|
108,Hassium,Hs,Métal de transition,—
|
||||||
|
109,Meitnérium,Mt,Indéfinie,—
|
||||||
|
110,Darmstadtium,Ds,Indéfinie,—
|
||||||
|
111,Roentgenium,Rg,Indéfinie,—
|
||||||
|
112,Copernicium,Cn,Métal de transition,—
|
||||||
|
113,Nihonium,Nh,Indéfinie,—
|
||||||
|
114,Flérovium,Fl,Indéfinie,—
|
||||||
|
115,Moscovium,Mc,Indéfinie,—
|
||||||
|
116,Livermorium,Lv,Indéfinie,—
|
||||||
|
117,Tennesse,Ts,Indéfinie,—
|
||||||
|
118,Oganesson,Og,Indéfinie,—
|
|
5
tests/fixtures.js
Normal file
5
tests/fixtures.js
Normal file
File diff suppressed because one or more lines are too long
@ -1,57 +1,116 @@
|
|||||||
import { freeCSV2HTML } from "../src/freeCSV2HTML";
|
import { freeCSV2HTML } from "../src/freeCSV2HTML";
|
||||||
const errors=require("../src/errors.js");
|
const errors=require("../src/errors.js");
|
||||||
|
const fixtures=require("./fixtures.js");
|
||||||
|
|
||||||
describe("freeCSV2HTML", function()
|
describe("freeCSV2HTML", () =>
|
||||||
{
|
{
|
||||||
let converter: freeCSV2HTML;
|
let converter: freeCSV2HTML;
|
||||||
const fixture="<div id='fixture'><div id='selectro1'></div><div id='selector2'></div><div id='datas'></div></div>";
|
|
||||||
|
|
||||||
beforeEach(function()
|
beforeEach( () =>
|
||||||
{
|
{
|
||||||
converter=new freeCSV2HTML();
|
converter=new freeCSV2HTML();
|
||||||
document.body.insertAdjacentHTML('afterbegin', fixture);
|
document.body.insertAdjacentHTML('afterbegin', fixtures.datasViewEltHTML);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function()
|
afterEach( () =>
|
||||||
{
|
{
|
||||||
document.body.removeChild(document.getElementById('fixture'));
|
document.body.removeChild(document.getElementById('fixture'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Doit être une instance de freeCSV2HTML", function()
|
it("Doit avoir créé une instance de freeCSV2HTML", () =>
|
||||||
{
|
{
|
||||||
expect(converter).toBeInstanceOf(freeCSV2HTML);
|
expect(converter).toBeInstanceOf(freeCSV2HTML);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Setting freeCSV2HTML", function()
|
describe("Setting freeCSV2HTML", () =>
|
||||||
{
|
{
|
||||||
it("Doit générer une erreur s'il n'y a pas d'élément dans la page pour l'id fourni pour recevoir les données.", function()
|
it("Doit générer une erreur s'il n'y a pas d'élément dans la page pour l'id fourni pour afficher les données.", () =>
|
||||||
{
|
{
|
||||||
expect(function() { return converter.datasEltId="dontExistId"; }).toThrowError(errors.elementNotFound+"dontExistId");
|
expect( () => { return converter.datasViewElt={ id:"dontExistId" }; }).toThrowError(errors.elementNotFound+"dontExistId");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Ne doit pas générer une erreur s'il y a un élément dans la page pour l'id fourni pour recevoir les données.", function()
|
it("Ne doit pas générer une erreur s'il y a bien un élément dans la page pour l'id fourni pour afficher les données.", () =>
|
||||||
{
|
{
|
||||||
expect(function() { return converter.datasEltId="datas"; }).not.toThrowError();
|
expect( () => { return converter.datasViewElt={ id:"datas" }; }).not.toThrowError();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Doit générer une erreur si l'url fournie pour le fichier csv n'est pas valide.", function()
|
it("Doit générer une erreur s'il n'y a pas d'éléments dans la page pour chaque id fourni pour afficher un des sélecteurs.", () =>
|
||||||
{
|
{
|
||||||
expect(function() { return converter.datasUrl="http:/localhost:8080/datas/elements-chimiques.csv"; }).toThrowError(errors.urlNotValid);
|
expect( () =>{ return converter.datasSelectors=[{ colCSV:2, id:"selector2"},{ colCSV:3, id:"selector3"}]; }).toThrowError(errors.elementNotFound+"selector3");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Ne doit pas générer une erreur si l'url fournie pour le fichier csv est pas valide.", function()
|
it("Doit générer une erreur si tous les numéros de colonne des sélecteurs ne sont pas des nombres naturels.", () =>
|
||||||
{
|
{
|
||||||
expect(function() { return converter.datasUrl="http://localhost:8080/datas/elements-chimiques.csv"; }).not.toThrowError();
|
expect( () => { return converter.datasSelectors=[{ colCSV:2.3, id:"selector1"},{ colCSV:3, id:"selector2"}]; }).toThrowError(errors.needNaturalNumber);
|
||||||
|
expect( () => { return converter.datasSelectors=[{ colCSV:2, id:"selector1"},{ colCSV:-1, id:"selector2"}]; }).toThrowError(errors.needNaturalNumber);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Doit générer une erreur s'il n'y a pas d'éléments dans la page pour les ids fournis pour recevoir les sélecteurs.", function()
|
it("Ne doit pas générer une erreur si les informations fournies pour les sélecteurs sont correctes.", () =>
|
||||||
{
|
{
|
||||||
expect(function() { return converter.datasSelectors=[{ colCSV:2, idElt:"selector2"},{ colCSV:3, idElt:"selector3"}]; }).toThrowError(errors.elementNotFound+"select3");
|
expect( () => { return converter.datasSelectors=[{ colCSV:0, id:"selector1"},{ colCSV:3, id:"selector2"}]; }).not.toThrowError();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Doit générer une erreur si l'url fournie pour le fichier de données est vide.", () =>
|
||||||
|
{
|
||||||
|
expect( () => { return converter.datasSourceUrl=" "; }).toThrowError(errors.needUrl);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Running freeCSV2HTML", () =>
|
||||||
|
{
|
||||||
|
it("Doit générer une erreur si au moins un des deux paramètres nécessaires au fonctionnement n'est pas fourni.", async () =>
|
||||||
|
{
|
||||||
|
await expectAsync( converter.run()).toBeRejectedWith(new Error(errors.needDatasElt));
|
||||||
|
converter.datasViewElt={ id:"datas" };
|
||||||
|
await expectAsync( converter.run()).toBeRejectedWith(new Error(errors.needUrl));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Ne doit pas générer une erreur s'il y a bien un élément dans la page pour chaque id fourni pour recevoir un sélecteur.", function()
|
it("Doit générer une erreur si la lecture du fichier échoue.", async () =>
|
||||||
{
|
{
|
||||||
expect(function() { return converter.datasSelectors=[{ colCSV:2, idElt:"selector1"},{ colCSV:3, idElt:"selector2"}]; }).not.toThrowError();
|
converter.datasViewElt={ id:"datas" };
|
||||||
|
converter.datasSourceUrl="http://localhost:9876/datas/fake.csv";
|
||||||
|
await expectAsync( converter.parse()).toBeRejectedWith(new Error(errors.parserFail));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Ne doit pas générer d'erreur si les deux paramètres fournis sont ok.", async () =>
|
||||||
|
{
|
||||||
|
converter.datasViewElt={ id:"datas" };
|
||||||
|
converter.datasSourceUrl="http://localhost:9876/datas/elements-chimiques.csv";
|
||||||
|
await expectAsync( converter.run()).toBeResolved();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Doit enregistrer la liste des erreurs rencontrées en parsant le fichier.", async () =>
|
||||||
|
{
|
||||||
|
converter.datasViewElt={ id:"datas" };
|
||||||
|
converter.datasSourceUrl="http://localhost:9876/datas/elements-chimiques-erreurs.csv";
|
||||||
|
await converter.parse();
|
||||||
|
expect(converter.parseErrors.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Ne doit enregistrer aucune erreur de lecture si le fichier est ok.", async () =>
|
||||||
|
{
|
||||||
|
converter.datasViewElt={ id:"datas" };
|
||||||
|
converter.datasSourceUrl="http://localhost:9876/datas/elements-chimiques.csv";
|
||||||
|
await converter.parse();
|
||||||
|
expect(converter.parseErrors.length).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Doit afficher une erreur dans la page si aucune donnée n'a été trouvée dans le fichier.", async () =>
|
||||||
|
{
|
||||||
|
converter.datasViewElt={ id:"datas" };
|
||||||
|
converter.datasSourceUrl="http://localhost:9876/datas/elements-chimiques-vide.csv";
|
||||||
|
await converter.run();
|
||||||
|
let txtDatasViewsElt=document.getElementById("datas").innerHTML;
|
||||||
|
expect(txtDatasViewsElt).toEqual(errors.datasNotFound);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Doit afficher un tableau correspondant aux données du fichier csv", async () =>
|
||||||
|
{
|
||||||
|
converter.datasViewElt={ id:"datas" };
|
||||||
|
converter.datasSourceUrl="http://localhost:9876/datas/elements-chimiques.csv";
|
||||||
|
await converter.run();
|
||||||
|
let txtDatasViewsElt=document.getElementById("datas").innerHTML;
|
||||||
|
expect(txtDatasViewsElt).toEqual(fixtures.datasHTML);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
Loading…
x
Reference in New Issue
Block a user