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",
"version": "0.7.2",
"version": "0.7.3",
"description": "Visualization of data from various sources (CSV, API, HTML...) with filters, classification, pagination, etc.",
"main": "index.js",
"scripts": {

View File

@ -107,12 +107,12 @@ export class FreeDatas2HTML
set datasSortingFunctions(SortingFunctions: SortingFunctions[])
{
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);
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
public getSortingFunctionForField(datasFieldNb: number): SortingFunctions|undefined
{
for(let i in this._datasSortingFunctions)
for(let checkedFunction of this._datasSortingFunctions)
{
if(this._datasSortingFunctions[i].datasFieldNb === datasFieldNb)
return this._datasSortingFunctions[i];
if(checkedFunction.datasFieldNb === datasFieldNb)
return checkedFunction;
}
return undefined;
}
@ -177,11 +177,8 @@ export class FreeDatas2HTML
this.datasHTML=this.createDatas2Display(this.fields, this.datas);
this._datasViewElt.eltDOM.innerHTML=this.datasHTML;
// On réactive les éventuels champs de classement qui ont été écrasés
for(let i in this.datasSortingFields)
{
let field=this.datasSortingFields[i];
for(let field of this.datasSortingFields)
field.field2HTML();
}
}
}

View File

@ -51,9 +51,9 @@ export class Pagination implements Paginations
options.displayElement=FreeDatas2HTML.checkInDOMById(options.displayElement);
if(options.values.length === 0)
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);
}
if(this.selectedValue !== undefined && options.values.indexOf(this.selectedValue) === -1)
@ -81,8 +81,8 @@ export class Pagination implements Paginations
else
{
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)
selectorsHTML+="<option value='"+(Number(j)+1)+"'>"+this._options.values[j]+"</option>";
for(let i=0; i< this._options.values.length; i++)
selectorsHTML+="<option value='"+(i+1)+"'>"+this._options.values[i]+"</option>";
selectorsHTML+="</select>";
this._options.displayElement.eltDOM!.innerHTML=selectorsHTML; // initialiser dans le setter
let selectElement=document.getElementById("freeDatas2HTMLPaginationSelector") as HTMLInputElement;

View File

@ -109,8 +109,8 @@ export class ParserForCSV implements Parsers
if(parser._datasRemoteSource.headers !== undefined)
{
this._privateOptions.downloadRequestHeaders={};
for (let i in parser._datasRemoteSource.headers)
this._privateOptions.downloadRequestHeaders[""+parser._datasRemoteSource.headers[Number(i)].key+""]=parser._datasRemoteSource.headers[Number(i)].value;
for(let i=0; i< parser._datasRemoteSource.headers.length; i++)
this._privateOptions.downloadRequestHeaders[parser._datasRemoteSource.headers[i].key]=parser._datasRemoteSource.headers[i].value;
}
}
else if(parser._datas2Parse !== "")
@ -132,10 +132,10 @@ export class ParserForCSV implements Parsers
{
// Attention, Papa Parse peut accepter un nom de champ vide
let realFields: string[]=[];
for(let i in results.meta.fields)
for(let field of results.meta.fields)
{
if(results.meta.fields[i].trim() !== "")
realFields.push(results.meta.fields[i]);
if(field.trim() !== "")
realFields.push(field);
}
if(realFields.length === 0)
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 )
{
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.fieldsEnding;
}
@ -49,7 +49,7 @@ export class Render implements DatasRenders
// Suivant les objets/lignes, les champs peuvent se trouver dans un ordre différent,
// 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.
for (let row of datas)
for(let row of datas)
{
datasHTML+=this.settings.lineBegining;
for(let field of this._converter.fields)

View File

@ -60,34 +60,35 @@ export class Selector implements Selectors
else
{
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)
{
let checkedValue=String(this._converter.datas[row][this.name]);
if(checkedValue !== "" && this.values.indexOf(checkedValue) === -1)
this.values.push(checkedValue);
if(row[this.name] !== "" && this.values.indexOf(row[this.name]) === -1)
this.values.push(row[this.name]);
}
else
{
let checkedValues=String(this._converter.datas[row][this.name]).split(this._separator); // String() car les données peuvent être des chiffres, etc.
for(let i in checkedValues)
let checkedValues=row[this.name].split(this._separator);
for(let value of checkedValues)
{
let checkedValue=checkedValues[i];
if(checkedValue !== "" && this.values.indexOf(checkedValue) === -1)
this.values.push(checkedValue);
if(value !== "" && this.values.indexOf(value) === -1)
this.values.push(value);
}
}
}
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)
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
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)
selectorsHTML+="<option value='"+(Number(j)+1)+"'>"+this.values[j]+"</option>";
// Création et injection du SELECT dans le DOM
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>";
this. _datasViewElt.eltDOM.innerHTML=selectorsHTML;
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)
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;
const selectedValueTxt=this.values[selectedValue] ;
@ -138,10 +139,10 @@ export class Selector implements Selectors
else
{
let find=false;
let checkedValues=String(data[this.name]).split(this._separator);
for(let j in checkedValues)
let checkedValues=data[this.name].split(this._separator);
for(let value of checkedValues)
{
if(checkedValues[j] === selectedValueTxt)
if(value === selectedValueTxt)
{
find=true;
break;