convertEncoding Update
This commit is contained in:
		| @@ -23,11 +23,6 @@ Le projet est disponible sur [function.divlo.fr](https://function.divlo.fr/). | ||||
| | **filterStudents(filteredLetter, students)** | Affiche uniquement les prénoms (qui sont dans la liste) qui commence par la lettre souhaitée. | - filteredLetter : la lettre à filtré - students : la liste des prénoms  | | ||||
| | **randomQuote()** | Génère aléatoirement une citation ou un proverbe. | Aucun paramètre  | | ||||
| | **convertCurrency(value, currency, url)** | Convertis une valeur dans une devise dans une autre devise grâce à l'API [exchangeratesapi.io](https://exchangeratesapi.io/). | - value : la valeur à convertir - currency : la devise à avoir après conversion - url : l'url de la requête à l'API en fonction du paramètre dans l'url '?base=' | | ||||
| | **utf8ToBin(s)** | Texte en Binaire (UTF-8). | - s : la valeur à convertir  | | ||||
| | **binToUtf8(s)** | Binaire (UTF-8) en Texte. | - s : la valeur à convertir  | | ||||
| | **utf8ToHex(s)** | Texte en Hexadécimal (UTF-8). | - s : la valeur à convertir  | | ||||
| | **hexToUtf8(s)** | Hexadécimal (UTF-8) en Texte. | - s : la valeur à convertir  | | ||||
| | **convertDecimalBinaryHexadecimal(value, option)** | Convertis des nombres de différents bases. | - value : la valeur à convertir - option : En quelle base convertir | | ||||
| | **convertArabicToRoman(nombre)** | Convertis un nombre arabe en nombre romain. | - nombre : le nombre à convertir  | | ||||
| | **convertRomanToArabic(str)** | Convertis un nombre romain en nombre arabe. | - str : le nombre romain à convertir  | | ||||
| | **armstrongNumber(nombre)** | Vérifie si un nombre fait partie des nombres d'Armstrong. | - nombre : le nombre à tester  | | ||||
|   | ||||
| @@ -84,7 +84,7 @@ header { | ||||
|     width: 270px !important; | ||||
| } | ||||
| #option { | ||||
|     width: 315px !important; | ||||
|     width: 380px !important; | ||||
| } | ||||
| #convertIn { | ||||
|     width: 6.7em; | ||||
|   | ||||
| @@ -172,8 +172,94 @@ function convertCurrency(value, currency, url) { | ||||
| } | ||||
|  | ||||
| // Convertis des nombres de différentes bases et convertis en UTF-8. (source : http://jsfiddle.net/47zwb41o) | ||||
| // DecimalToBinary | ||||
| function decimalToBinary(value) { | ||||
|     value = value.replace(" ", ""); | ||||
|     value = Number(value); | ||||
|     if (isNaN(value)) { | ||||
|         return messageError; | ||||
|     } else { | ||||
|         return value.toString(2); | ||||
|     } | ||||
| } | ||||
| // BinaryToDecimal | ||||
| function binaryToDecimal(value) { | ||||
|     value = Number(value); | ||||
|     const result = formatNumberResult(parseInt(value, 2)); | ||||
|     if (isNaN(result)) { | ||||
|         return messageError; | ||||
|     } else { | ||||
|         return result | ||||
|     } | ||||
| } | ||||
| // DecimalToHexadecimal | ||||
| function decimalToHexadecimal(value) { | ||||
|     value = value.replace(" ", ""); | ||||
|     value = Number(value); | ||||
|     if (isNaN(value)) { | ||||
|         return messageError; | ||||
|     } else { | ||||
|         return value.toString(16).toUpperCase(); | ||||
|     } | ||||
| } | ||||
| // HexadecimalToDecimal | ||||
| function hexadecimalToDecimal(value) { | ||||
|     const result = formatNumberResult(parseInt(value, 16)); | ||||
|     if (isNaN(result)) { | ||||
|         return messageError; | ||||
|     } else { | ||||
|         return result; | ||||
|     } | ||||
| } | ||||
| // BinaryToHexadecimal  | ||||
| function binaryToHexadecimal(value) { | ||||
|     value = Number(value); | ||||
|     value = parseInt(value, 2); | ||||
|     if (isNaN(value)) { | ||||
|         return messageError; | ||||
|     } else { | ||||
|         return parseInt(value).toString(16).toUpperCase(); | ||||
|     }    | ||||
| } | ||||
| // HexadecimalToBinary | ||||
| function hexadecimalToBinary(value) { | ||||
|     value = Number(value); | ||||
|     value = parseInt(value, 16); | ||||
|     if (isNaN(value)) { | ||||
|         return messageError; | ||||
|     } else { | ||||
|         return parseInt(value).toString(2); | ||||
|     } | ||||
| } | ||||
| // Each letters has its own codePoint (Unicode Code) | ||||
| function textToNumberUnicode(string) { | ||||
|     try { | ||||
|         let resultat = ""; | ||||
|         for (let index in string) { | ||||
|           resultat = resultat + string.codePointAt(index) + " "; | ||||
|         } | ||||
|         return resultat; | ||||
|     } | ||||
|     catch(error) { | ||||
|         return messageError; | ||||
|     } | ||||
| } | ||||
| // Each codePoint has its own letter  | ||||
| function numberUnicodeToText(string) { | ||||
|     try { | ||||
|         const array = string.split(" "); | ||||
|         let resultat = ""; | ||||
|         for (let index in array) { | ||||
|           resultat = resultat + String.fromCodePoint(parseInt(array[index]).toString()); | ||||
|         } | ||||
|         return resultat; | ||||
|     } | ||||
|     catch(error) { | ||||
|         return messageError; | ||||
|     } | ||||
| } | ||||
| // Texte en Binaire (UTF-8) | ||||
| function utf8ToBin(s) { | ||||
| function textToBinary(s) { | ||||
|     try { | ||||
|         s = unescape( encodeURIComponent(s)); | ||||
|         var chr, i = 0, l = s.length, out = ''; | ||||
| @@ -188,7 +274,7 @@ function utf8ToBin(s) { | ||||
|     } | ||||
| } | ||||
| // Binaire (UTF-8) en Texte | ||||
| function binToUtf8(s){ | ||||
| function binaryToText(s){ | ||||
|     try { | ||||
|         var i = 0, l = s.length, chr, out = ''; | ||||
|         for( ; i < l; i += 8){ | ||||
| @@ -201,7 +287,7 @@ function binToUtf8(s){ | ||||
|     } | ||||
| } | ||||
| // Texte en Hexadécimal (UTF-8) | ||||
| function utf8ToHex (s) { | ||||
| function textToHexadecimal (s) { | ||||
|     try { | ||||
|         s = unescape( encodeURIComponent( s ) ); | ||||
|         var chr, i = 0, l = s.length, out = ''; | ||||
| @@ -217,7 +303,7 @@ function utf8ToHex (s) { | ||||
|     } | ||||
| } | ||||
| // Hexadécimal (UTF-8) en Texte | ||||
| function hexToUtf8 (s) { | ||||
| function hexadecimalToText (s) { | ||||
|     try { | ||||
|         return decodeURIComponent( s.replace( /../g, '%$&' ) ); | ||||
|     } | ||||
| @@ -225,52 +311,6 @@ function hexToUtf8 (s) { | ||||
|         return s; | ||||
|     } | ||||
| } | ||||
| // Convertis des nombres de différents bases | ||||
| function convertDecimalBinaryHexadecimal(value, option) { | ||||
|     try { | ||||
|         switch (option) { | ||||
|             case 'DecimalToBinary': | ||||
|                 value = value.replace(" ", ""); | ||||
|                 value = parseInt(value); | ||||
|                 if (isNaN(value)) { | ||||
|                     return messageError; | ||||
|                 } else { | ||||
|                     return value.toString(2); | ||||
|                 } | ||||
|             case 'BinaryToDecimal': | ||||
|                 return formatNumberResult(parseInt(value, 2)); | ||||
|             case 'DecimalToHexadecimal': | ||||
|                 value = value.replace(" ", ""); | ||||
|                 value = parseInt(value); | ||||
|                 if (isNaN(value)) { | ||||
|                     return messageError; | ||||
|                 } else { | ||||
|                     return value.toString(16).toUpperCase(); | ||||
|                 } | ||||
|             case 'HexadecimalToDecimal': | ||||
|                 return formatNumberResult(parseInt(value, 16));  | ||||
|             case 'BinaryToHexadecimal': | ||||
|                 value = parseInt(value, 2); | ||||
|                 if (isNaN(value)) { | ||||
|                     return messageError; | ||||
|                 } else { | ||||
|                     return parseInt(value).toString(16).toUpperCase(); | ||||
|                 }    | ||||
|             case 'HexadecimalToBinary': | ||||
|                 value = parseInt(value, 16); | ||||
|                 if (isNaN(value)) { | ||||
|                     return messageError; | ||||
|                 } else { | ||||
|                     return parseInt(value).toString(2); | ||||
|                 } | ||||
|             default: | ||||
|                 return messageError; | ||||
|         } | ||||
|     }  | ||||
|     catch (error) { | ||||
|         return messageError; | ||||
|     } | ||||
| } | ||||
|  | ||||
| // Convertis un nombre arabe en nombre romain | ||||
| function convertArabicToRoman(nombre) { | ||||
|   | ||||
| @@ -2,7 +2,7 @@ $(function () { | ||||
|  | ||||
|     /* ÉXECUTION DES FONCTONS */ | ||||
|  | ||||
|     // Touche entrer génère un clique | ||||
|     // Touche entrer génère un clique sur les classes .btn | ||||
|     $("body").keydown(function(e){ | ||||
|         if(e.which === 13){ | ||||
|             $(".btn").click(); | ||||
| @@ -39,8 +39,7 @@ $(function () { | ||||
|  | ||||
|     $("#birthDateValue").bind("keyup change", () =>  | ||||
|     { | ||||
|         const birthDateEntered = $('#birthDateValue').val(); | ||||
|         $('.results').html(calculateAge(birthDateEntered)); | ||||
|         $('.results').html(calculateAge($('#birthDateValue').val())); | ||||
|     }); | ||||
|  | ||||
|     $("#submitConvertTemperature").click(() =>  | ||||
| @@ -104,7 +103,7 @@ $(function () { | ||||
|         for (index in quotes) { | ||||
|             resultat = resultat + `<tr> <td class="quote-element-list important">${quotes[index]["source"]}</td> <td class="quote-element-list">${quotes[index]["quote"]}</td> </tr>`; | ||||
|         } | ||||
|         $( ".quote-list" ).append(resultat); | ||||
|         $(".quote-list").append(resultat); | ||||
|     } | ||||
|  | ||||
|     $("#submitConvertCurrency").click(() => { | ||||
| @@ -128,32 +127,22 @@ $(function () { | ||||
|             $('.results').html(messageError); | ||||
|         } | ||||
|         else { | ||||
|           if (option === 'DecimalToBinary' || option === 'BinaryToDecimal' || option === 'DecimalToHexadecimal' || option === 'HexadecimalToDecimal' || option === 'BinaryToHexadecimal' || option === 'HexadecimalToBinary') { | ||||
|             const result = convertDecimalBinaryHexadecimal(value, option); | ||||
|             $('.results').html(result); | ||||
|           } | ||||
|           else if (option === 'BinaryToText') { | ||||
|             // Le replace enlève les espaces | ||||
|             const textResult = binToUtf8(value.replace(/\s/g,''));  | ||||
|             $('.results').html(textResult); | ||||
|           } | ||||
|           else if (option === 'TextToBinary') { | ||||
|               // Les 2 replace permettent de rajouter un espace tout les 8 bits | ||||
|               let binaryResult = utf8ToBin(value); | ||||
|               binaryResult = binaryResult.replace(/(\d{8})/g, '$1 ').replace(/(^\s+|\s+$)/,'');  | ||||
|               $('.results').html(binaryResult); | ||||
|           } | ||||
|           else if (option === 'TextToHexadecimal') { | ||||
|             const result = utf8ToHex(value); | ||||
|             $('.results').html(result.toUpperCase()); | ||||
|           } | ||||
|           else if (option === 'HexadecimalToText') { | ||||
|               const result = hexToUtf8(value.replace(/\s/g,'')); | ||||
|               $('.results').html(result); | ||||
|           } | ||||
|           else { | ||||
|             $('.results').html(messageError); | ||||
|           } | ||||
|             // Objet qui recense toutes les fonctions de convertEncoding | ||||
|             const convertEncoding = { decimalToBinary, binaryToDecimal, decimalToHexadecimal, hexadecimalToDecimal, binaryToHexadecimal, hexadecimalToBinary, textToNumberUnicode, numberUnicodeToText, textToBinary, binaryToText, textToHexadecimal, hexadecimalToText }; | ||||
|             try { | ||||
|                 function executionFunction(option, value) { | ||||
|                     if (convertEncoding[option]) { | ||||
|                         return convertEncoding[option](value) | ||||
|                     } else { | ||||
|                         console.log(convertEncoding[option]); | ||||
|                         return messageError; | ||||
|                     } | ||||
|                 } | ||||
|                 $('.results').html(executionFunction(option, value)); | ||||
|             } catch (error) { | ||||
|                 $('.results').html(messageError); | ||||
|                 console.log(error); | ||||
|             } | ||||
|         } | ||||
|     }); | ||||
|  | ||||
| @@ -216,6 +205,21 @@ $(function () { | ||||
|             $('.results').html(`Il y a ${formatNumberResult(stringPermutationsResult.length)} possibilités d'anagramme pour le mot "${value}" qui contient ${value.length} caractères, la liste : <br><br> ${result}`); | ||||
|         } | ||||
|     }); | ||||
|  | ||||
|     /* Changement du texte accueil (exemples de fonction) */ | ||||
|     if(chemin === "/" || chemin === '/index.php') { | ||||
|         let index=-1; | ||||
|         function change() { | ||||
|             if(index === texteFonctionChange.length-1) { | ||||
|                 index = 0; | ||||
|             } | ||||
|             else { | ||||
|                 index++; | ||||
|             } | ||||
|             document.getElementById("change").innerHTML = texteFonctionChange[index]; | ||||
|         } | ||||
|         setInterval(change,10000); | ||||
|     } | ||||
|      | ||||
|     /* Permet d'afficher l'heure en temps réel sur le footer */ | ||||
|     window.onload = realDateTime('realDateTime'); | ||||
| @@ -244,21 +248,6 @@ $(function () { | ||||
|         language: 'fr', | ||||
|         autoclose: false, | ||||
|         todayHighlight: true | ||||
|     }) | ||||
|     }); | ||||
|  | ||||
| }) | ||||
|  | ||||
| /* Changement du texte accueil (exemples de fonction) */ | ||||
| if(chemin === "/" || chemin === '/index.php') { | ||||
|     let index=-1; | ||||
|     function change() { | ||||
|         if(index === texteFonctionChange.length-1) { | ||||
|             index = 0; | ||||
|         } | ||||
|         else { | ||||
|             index++; | ||||
|         } | ||||
|         document.getElementById("change").innerHTML = texteFonctionChange[index]; | ||||
|     } | ||||
|     setInterval("change()",10000); | ||||
| } | ||||
| }); | ||||
| @@ -17,16 +17,18 @@ | ||||
|         <br> | ||||
| 		<label for="option">Choisissez une option : </label> <br> | ||||
|         <select class="form-control selectInline" id="option"> | ||||
|         <option value="DecimalToBinary">Décimal en Binaire</option> | ||||
|             <option value="BinaryToDecimal">Binaire en Décimal</option> | ||||
|             <option value="DecimalToHexadecimal">Décimal en Hexadecimal</option> | ||||
|             <option value="HexadecimalToDecimal">Hexadecimal en Décimal</option> | ||||
|             <option value="BinaryToHexadecimal">Binaire en Hexadécimal</option> | ||||
|             <option value="HexadecimalToBinary">Hexadécimal en Binaire</option> | ||||
|             <option value="TextToBinary">Texte en Binaire (UTF-8)</option> | ||||
|             <option value="BinaryToText">Binaire (UTF-8) en Texte</option> | ||||
|             <option value="TextToHexadecimal">Texte en Hexadécimal (UTF-8)</option> | ||||
|             <option value="HexadecimalToText">Hexadécimal (UTF-8) en Texte</option> | ||||
|             <option value="decimalToBinary">Décimal en Binaire</option> | ||||
|             <option value="binaryToDecimal">Binaire en Décimal</option> | ||||
|             <option value="decimalToHexadecimal">Décimal en Hexadecimal</option> | ||||
|             <option value="hexadecimalToDecimal">Hexadecimal en Décimal</option> | ||||
|             <option value="binaryToHexadecimal">Binaire en Hexadécimal</option> | ||||
|             <option value="hexadecimalToBinary">Hexadécimal en Binaire</option> | ||||
|             <option value="textToNumberUnicode">Chaque caractère a un nombre Unicode</option> | ||||
|             <option value="numberUnicodeToText">Chaque nombre Unicode a un caractère</option> | ||||
|             <option value="textToBinary">Texte en Binaire (UTF-8)</option> | ||||
|             <option value="binaryToText">Binaire (UTF-8) en Texte</option> | ||||
|             <option value="textToHexadecimal">Texte en Hexadécimal (UTF-8)</option> | ||||
|             <option value="hexadecimalToText">Hexadécimal (UTF-8) en Texte</option> | ||||
|         </select> | ||||
|         <div class="form-row text-center"> | ||||
|             <div class="col-12"> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user