81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
// Quelques fonctions utiles pour les chaînes
|
|
|
|
class Tool
|
|
{
|
|
static isEmpty(myVar)
|
|
{
|
|
if(myVar===undefined || myVar===null)
|
|
return true;
|
|
else
|
|
{
|
|
myVar+="";// si autre chose qu'une chaîne envoyé...
|
|
myVar=myVar.trim();
|
|
if(myVar==="")
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static trimIfNotNull(myString)
|
|
{
|
|
if(Tool.isEmpty(myString))
|
|
myString=null;
|
|
else
|
|
{
|
|
myString+="";// si autre chose qu'une chaîne envoyé...
|
|
myString=myString.trim();
|
|
}
|
|
return myString;
|
|
}
|
|
|
|
|
|
static shortenIfLongerThan(myString, max)
|
|
{
|
|
myString+="";// au cas où cela ne serait pas une chaîne...
|
|
if(myString.length > max)
|
|
myString=myString.substring(0, (max-3))+"...";
|
|
return myString;
|
|
}
|
|
|
|
// source : https://stackoverflow.com/questions/15604140/replace-multiple-strings-with-multiple-other-strings
|
|
static replaceAll(myString, mapObj)
|
|
{
|
|
const replaceElts = new RegExp(Object.keys(mapObj).join("|"),"gi");
|
|
return myString.replace(replaceElts, (matched) =>
|
|
{
|
|
return mapObj[matched];
|
|
});
|
|
}
|
|
|
|
// source : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Math/random
|
|
static getRandomInt(min, max)
|
|
{
|
|
min = Math.ceil(min);
|
|
max = Math.floor(max);
|
|
return Math.floor(Math.random() * (max - min)) + min;
|
|
}
|
|
|
|
// à compléter : https://en.wikipedia.org/wiki/Date_format_by_country
|
|
static dateFormat(dateString, lang="fr")
|
|
{
|
|
if(Tool.isEmpty(dateString))
|
|
return "";
|
|
let myDate=new Date(dateString);
|
|
let myDay=myDate.getDate()+"";
|
|
if(myDay.length===1)
|
|
myDay="0"+myDay;
|
|
let myMounth=(myDate.getMonth()+1)+"";
|
|
if(myMounth.length===1)
|
|
myMounth="0"+myMounth;
|
|
let myYear=myDate.getFullYear();
|
|
if(lang==="fr")
|
|
return myDay+"/"+myMounth+"/"+myYear;
|
|
else if (lang==="form")// 2014-02-09
|
|
return myYear+"-"+myMounth+"-"+myDay;
|
|
else
|
|
return myMounth+"/"+myDay+"/"+myYear;
|
|
}
|
|
}
|
|
|
|
module.exports = Tool; |