Revue du code, notamment de l'utilisation des boucles for().

This commit is contained in:
Fabrice PENHOËT 2021-10-07 17:15:35 +02:00
parent be57ef6927
commit 19c7994388
6 changed files with 37 additions and 39 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "freedatas2html", "name": "freedatas2html",
"version": "0.7.2", "version": "0.7.3",
"description": "Visualization of data from various sources (CSV, API, HTML...) with filters, classification, pagination, etc.", "description": "Visualization of data from various sources (CSV, API, HTML...) with filters, classification, pagination, etc.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -107,12 +107,12 @@ export class FreeDatas2HTML
set datasSortingFunctions(SortingFunctions: SortingFunctions[]) set datasSortingFunctions(SortingFunctions: SortingFunctions[])
{ {
this._datasSortingFunctions=[]; this._datasSortingFunctions=[];
for(let i = 0; i < SortingFunctions.length; i++) for(let checkedFunction of SortingFunctions)
{ {
if(! this.checkFieldExist(SortingFunctions[i].datasFieldNb)) if(! this.checkFieldExist(checkedFunction.datasFieldNb))
throw new Error(errors.converterFieldNotFound); throw new Error(errors.converterFieldNotFound);
else else
this._datasSortingFunctions.push(SortingFunctions[i]); this._datasSortingFunctions.push(checkedFunction);
} }
} }
@ -134,10 +134,10 @@ export class FreeDatas2HTML
// Retourne l'éventuelle fonction spécifique de classement associée à un champ // Retourne l'éventuelle fonction spécifique de classement associée à un champ
public getSortingFunctionForField(datasFieldNb: number): SortingFunctions|undefined public getSortingFunctionForField(datasFieldNb: number): SortingFunctions|undefined
{ {
for(let i in this._datasSortingFunctions) for(let checkedFunction of this._datasSortingFunctions)
{ {
if(this._datasSortingFunctions[i].datasFieldNb === datasFieldNb) if(checkedFunction.datasFieldNb === datasFieldNb)
return this._datasSortingFunctions[i]; return checkedFunction;
} }
return undefined; return undefined;
} }
@ -177,13 +177,10 @@ export class FreeDatas2HTML
this.datasHTML=this.createDatas2Display(this.fields, this.datas); this.datasHTML=this.createDatas2Display(this.fields, this.datas);
this._datasViewElt.eltDOM.innerHTML=this.datasHTML; this._datasViewElt.eltDOM.innerHTML=this.datasHTML;
// On réactive les éventuels champs de classement qui ont été écrasés // On réactive les éventuels champs de classement qui ont été écrasés
for(let i in this.datasSortingFields) for(let field of this.datasSortingFields)
{
let field=this.datasSortingFields[i];
field.field2HTML(); field.field2HTML();
} }
} }
}
private createDatas2Display(fields: string[], datas: any[]) : string private createDatas2Display(fields: string[], datas: any[]) : string
{ {

View File

@ -51,9 +51,9 @@ export class Pagination implements Paginations
options.displayElement=FreeDatas2HTML.checkInDOMById(options.displayElement); options.displayElement=FreeDatas2HTML.checkInDOMById(options.displayElement);
if(options.values.length === 0) if(options.values.length === 0)
throw new Error(errors.paginationNeedOptionsValues); throw new Error(errors.paginationNeedOptionsValues);
for(let i in options.values) for(let option of options.values)
{ {
if(!Pagination.isPositiveInteger(options.values[i])) if(! Pagination.isPositiveInteger(option))
throw new Error(errors.needPositiveInteger); throw new Error(errors.needPositiveInteger);
} }
if(this.selectedValue !== undefined && options.values.indexOf(this.selectedValue) === -1) if(this.selectedValue !== undefined && options.values.indexOf(this.selectedValue) === -1)
@ -81,8 +81,8 @@ export class Pagination implements Paginations
else else
{ {
let selectorsHTML="<label for='freeDatas2HTMLPaginationSelector'>"+this._options.name+" </label><select name='freeDatas2HTMLPaginationSelector' id='freeDatas2HTMLPaginationSelector'><option value='0'>----</option>"; let selectorsHTML="<label for='freeDatas2HTMLPaginationSelector'>"+this._options.name+" </label><select name='freeDatas2HTMLPaginationSelector' id='freeDatas2HTMLPaginationSelector'><option value='0'>----</option>";
for(let j in this._options.values) for(let i=0; i< this._options.values.length; i++)
selectorsHTML+="<option value='"+(Number(j)+1)+"'>"+this._options.values[j]+"</option>"; selectorsHTML+="<option value='"+(i+1)+"'>"+this._options.values[i]+"</option>";
selectorsHTML+="</select>"; selectorsHTML+="</select>";
this._options.displayElement.eltDOM!.innerHTML=selectorsHTML; // initialiser dans le setter this._options.displayElement.eltDOM!.innerHTML=selectorsHTML; // initialiser dans le setter
let selectElement=document.getElementById("freeDatas2HTMLPaginationSelector") as HTMLInputElement; let selectElement=document.getElementById("freeDatas2HTMLPaginationSelector") as HTMLInputElement;

View File

@ -109,8 +109,8 @@ export class ParserForCSV implements Parsers
if(parser._datasRemoteSource.headers !== undefined) if(parser._datasRemoteSource.headers !== undefined)
{ {
this._privateOptions.downloadRequestHeaders={}; this._privateOptions.downloadRequestHeaders={};
for (let i in parser._datasRemoteSource.headers) for(let i=0; i< parser._datasRemoteSource.headers.length; i++)
this._privateOptions.downloadRequestHeaders[""+parser._datasRemoteSource.headers[Number(i)].key+""]=parser._datasRemoteSource.headers[Number(i)].value; this._privateOptions.downloadRequestHeaders[parser._datasRemoteSource.headers[i].key]=parser._datasRemoteSource.headers[i].value;
} }
} }
else if(parser._datas2Parse !== "") else if(parser._datas2Parse !== "")
@ -132,10 +132,10 @@ export class ParserForCSV implements Parsers
{ {
// Attention, Papa Parse peut accepter un nom de champ vide // Attention, Papa Parse peut accepter un nom de champ vide
let realFields: string[]=[]; let realFields: string[]=[];
for(let i in results.meta.fields) for(let field of results.meta.fields)
{ {
if(results.meta.fields[i].trim() !== "") if(field.trim() !== "")
realFields.push(results.meta.fields[i]); realFields.push(field);
} }
if(realFields.length === 0) if(realFields.length === 0)
reject(new Error(errors.parserFail)); reject(new Error(errors.parserFail));

View File

@ -40,7 +40,7 @@ export class Render implements DatasRenders
if(this.settings.fieldsBegining !== undefined && this.settings.fieldDisplaying !== undefined && this.settings.fieldsEnding !== undefined ) if(this.settings.fieldsBegining !== undefined && this.settings.fieldDisplaying !== undefined && this.settings.fieldsEnding !== undefined )
{ {
datasHTML+=this.settings.fieldsBegining; datasHTML+=this.settings.fieldsBegining;
for (let field of this._converter.fields) for(let field of this._converter.fields)
datasHTML+=this.settings.fieldDisplaying.replace("#FIELDNAME", field); datasHTML+=this.settings.fieldDisplaying.replace("#FIELDNAME", field);
datasHTML+=this.settings.fieldsEnding; datasHTML+=this.settings.fieldsEnding;
} }
@ -49,7 +49,7 @@ export class Render implements DatasRenders
// Suivant les objets/lignes, les champs peuvent se trouver dans un ordre différent, // Suivant les objets/lignes, les champs peuvent se trouver dans un ordre différent,
// ou encore des champs peuvent manquer ou être en trop. // ou encore des champs peuvent manquer ou être en trop.
// Seuls les champs de "fields" doivent être traités et en respectant l'ordre de ce tableau. // Seuls les champs de "fields" doivent être traités et en respectant l'ordre de ce tableau.
for (let row of datas) for(let row of datas)
{ {
datasHTML+=this.settings.lineBegining; datasHTML+=this.settings.lineBegining;
for(let field of this._converter.fields) for(let field of this._converter.fields)

View File

@ -60,34 +60,35 @@ export class Selector implements Selectors
else else
{ {
this.name=this._converter.fields![this._datasFieldNb]; // this._converter.parse... ne peuvent être indéfinis si this._converter existe (cf constructeur) this.name=this._converter.fields![this._datasFieldNb]; // this._converter.parse... ne peuvent être indéfinis si this._converter existe (cf constructeur)
for (let row in this._converter.datas) for (let row of this._converter.datas)
{ {
if(this._separator === undefined) if(this._separator === undefined)
{ {
let checkedValue=String(this._converter.datas[row][this.name]); if(row[this.name] !== "" && this.values.indexOf(row[this.name]) === -1)
if(checkedValue !== "" && this.values.indexOf(checkedValue) === -1) this.values.push(row[this.name]);
this.values.push(checkedValue);
} }
else else
{ {
let checkedValues=String(this._converter.datas[row][this.name]).split(this._separator); // String() car les données peuvent être des chiffres, etc. let checkedValues=row[this.name].split(this._separator);
for(let i in checkedValues) for(let value of checkedValues)
{ {
let checkedValue=checkedValues[i]; if(value !== "" && this.values.indexOf(value) === -1)
if(checkedValue !== "" && this.values.indexOf(checkedValue) === -1) this.values.push(value);
this.values.push(checkedValue);
} }
} }
} }
if(this.values.length > 0) if(this.values.length > 0)
{ {
// Classement des données à l'aide (ou non) d'une fonction spécifique :
if(this._converter.getSortingFunctionForField(this._datasFieldNb) !== undefined) if(this._converter.getSortingFunctionForField(this._datasFieldNb) !== undefined)
this.values.sort(this._converter.getSortingFunctionForField(this._datasFieldNb)!.sort); // si une fonction est définie pour ce champ, sort() doit exister, cf. interface this.values.sort(this._converter.getSortingFunctionForField(this._datasFieldNb)!.sort); // sans le "!" : TS2532: Object is possibly 'undefined' ??
else else
this.values.sort(compare()); this.values.sort(compare());
let selectorsHTML="<label for='freeDatas2HTML_"+this._datasViewElt.id+"'>"+this.name+" : </label><select name='freeDatas2HTML_"+this._datasViewElt.id+"' id='freeDatas2HTML_"+this._datasViewElt.id+"'><option value='0'>----</option>"; // l'option zéro permet de rafraichir en ignorant ce filtre
for(let j in this.values) // Création et injection du SELECT dans le DOM
selectorsHTML+="<option value='"+(Number(j)+1)+"'>"+this.values[j]+"</option>"; let selectorsHTML="<label for='freeDatas2HTML_"+this._datasViewElt.id+"'>"+this.name+" : </label><select name='freeDatas2HTML_"+this._datasViewElt.id+"' id='freeDatas2HTML_"+this._datasViewElt.id+"'><option value='0'>----</option>"; // l'option zéro permet d'actualiser l'affichage en ignorant ce filtre
for(let i=0; i< this.values.length; i++)
selectorsHTML+="<option value='"+(i+1)+"'>"+this.values[i]+"</option>";
selectorsHTML+="</select>"; selectorsHTML+="</select>";
this. _datasViewElt.eltDOM.innerHTML=selectorsHTML; this. _datasViewElt.eltDOM.innerHTML=selectorsHTML;
const selectElement=document.getElementById("freeDatas2HTML_"+this._datasViewElt.id) as HTMLInputElement, mySelector=this; const selectElement=document.getElementById("freeDatas2HTML_"+this._datasViewElt.id) as HTMLInputElement, mySelector=this;
@ -124,7 +125,7 @@ export class Selector implements Selectors
if(this.values[selectedValue] === undefined) if(this.values[selectedValue] === undefined)
throw new Error(errors.selectorSelectedIndexNotFound); throw new Error(errors.selectorSelectedIndexNotFound);
if(data[this.name] === undefined) // attribut absent pour cet enregistrement, qui est donc refusé if(data[this.name] === undefined) // champ absent pour cet enregistrement, qui est donc refusé
return false; return false;
const selectedValueTxt=this.values[selectedValue] ; const selectedValueTxt=this.values[selectedValue] ;
@ -138,10 +139,10 @@ export class Selector implements Selectors
else else
{ {
let find=false; let find=false;
let checkedValues=String(data[this.name]).split(this._separator); let checkedValues=data[this.name].split(this._separator);
for(let j in checkedValues) for(let value of checkedValues)
{ {
if(checkedValues[j] === selectedValueTxt) if(value === selectedValueTxt)
{ {
find=true; find=true;
break; break;