diff --git a/api/assets/functions/functionObject.js b/api/assets/functions/functionObject.js index b4a2aa4..fe5e824 100644 --- a/api/assets/functions/functionObject.js +++ b/api/assets/functions/functionObject.js @@ -1,26 +1,25 @@ -const { randomNumberOutput } = require('./main/randomNumber'); -const { convertArabicToRomanOutput, convertRomanToArabicOutput } = require('./main/convertRomanArabicNumbers'); -const { convertDistanceOutput } = require('./main/convertDistance'); -const { convertTemperatureOutput } = require('./main/convertTemperature'); -const { armstrongNumberOutput } = require('./main/armstrongNumber'); -const { weatherRequestOutput } = require('./main/weatherRequest'); -const { convertCurrencyOutput } = require('./main/convertCurrency'); -const { calculateAgeOutput } = require('./main/calculateAge'); -const { heapAlgorithmOutput } = require('./main/heapAlgorithm'); -const { convertEncodingOutput } = require('./main/convertEncoding'); +const randomNumberOutput = require('./main/randomNumber'); +const convertRomanArabicNumbersOutput = require('./main/convertRomanArabicNumbers'); +const convertDistanceOutput = require('./main/convertDistance'); +const convertTemperatureOutput = require('./main/convertTemperature'); +const armstrongNumberOutput = require('./main/armstrongNumber'); +const weatherRequestOutput = require('./main/weatherRequest'); +const convertCurrencyOutput = require('./main/convertCurrency'); +const calculateAgeOutput = require('./main/calculateAge'); +const heapAlgorithmOutput = require('./main/heapAlgorithm'); +const convertEncodingOutput = require('./main/convertEncoding'); const functionObject = { - randomNumber : randomNumberOutput, - convertArabicToRoman: convertArabicToRomanOutput, - convertRomanToArabic: convertRomanToArabicOutput, - convertDistance : convertDistanceOutput, - convertTemperature : convertTemperatureOutput, - armstrongNumber : armstrongNumberOutput, - weatherRequest : weatherRequestOutput, - convertCurrency : convertCurrencyOutput, - calculateAge : calculateAgeOutput, - heapAlgorithm : heapAlgorithmOutput, - convertEncoding : convertEncodingOutput + randomNumber : randomNumberOutput, + convertRomanArabicNumbers: convertRomanArabicNumbersOutput, + convertDistance : convertDistanceOutput, + convertTemperature : convertTemperatureOutput, + armstrongNumber : armstrongNumberOutput, + weatherRequest : weatherRequestOutput, + convertCurrency : convertCurrencyOutput, + calculateAge : calculateAgeOutput, + heapAlgorithm : heapAlgorithmOutput, + convertEncoding : convertEncodingOutput }; // Choisi la fonction à exécuter diff --git a/api/assets/functions/main/armstrongNumber.js b/api/assets/functions/main/armstrongNumber.js index edf0f95..84c4a97 100644 --- a/api/assets/functions/main/armstrongNumber.js +++ b/api/assets/functions/main/armstrongNumber.js @@ -28,7 +28,7 @@ function armstrongNumber(number) { } /* OUTPUTS */ -exports.armstrongNumberOutput = ({ res, next }, argsObject) => { +module.exports = armstrongNumberOutput = ({ res, next }, argsObject) => { let { number } = argsObject; // S'il n'y a pas les champs obligatoire diff --git a/api/assets/functions/main/calculateAge.js b/api/assets/functions/main/calculateAge.js index 5ce806a..7cfd736 100644 --- a/api/assets/functions/main/calculateAge.js +++ b/api/assets/functions/main/calculateAge.js @@ -20,7 +20,7 @@ function calculateAge(currentDate, { birthDateDay, birthDateMonth, birthDateYear } /* OUTPUTS */ -exports.calculateAgeOutput = ({ res, next }, argsObject) => { +module.exports = calculateAgeOutput = ({ res, next }, argsObject) => { let { birthDate } = argsObject; // S'il n'y a pas les champs obligatoire diff --git a/api/assets/functions/main/convertCurrency.js b/api/assets/functions/main/convertCurrency.js index 7ae55ec..8efb5e2 100644 --- a/api/assets/functions/main/convertCurrency.js +++ b/api/assets/functions/main/convertCurrency.js @@ -4,7 +4,7 @@ const { requiredFields } = require('../../config/errors'); const formatNumberResult = require('../secondary/formatNumberResult'); /* OUTPUTS */ -exports.convertCurrencyOutput = ({ res, next }, argsObject) => { +module.exports = convertCurrencyOutput = ({ res, next }, argsObject) => { let { number, baseCurrency, finalCurrency } = argsObject; // S'il n'y a pas les champs obligatoire diff --git a/api/assets/functions/main/convertDistance.js b/api/assets/functions/main/convertDistance.js index 60adeda..1eccbf1 100644 --- a/api/assets/functions/main/convertDistance.js +++ b/api/assets/functions/main/convertDistance.js @@ -28,7 +28,7 @@ function convertDistance(firstValue, unitFirstValue, unitFinalValue) { } /* OUTPUTS */ -exports.convertDistanceOutput = ({ res, next }, argsObject) => { +module.exports = convertDistanceOutput = ({ res, next }, argsObject) => { let { number, numberUnit, finalUnit } = argsObject; // S'il n'y a pas les champs obligatoire diff --git a/api/assets/functions/main/convertEncoding.js b/api/assets/functions/main/convertEncoding.js index 72c4927..1aa395f 100644 --- a/api/assets/functions/main/convertEncoding.js +++ b/api/assets/functions/main/convertEncoding.js @@ -214,12 +214,11 @@ function hexadecimalToText (s) { /* OUTPUTS */ const convertEncoding = { decimalToBinary, binaryToDecimal, decimalToHexadecimal, hexadecimalToDecimal, binaryToHexadecimal, hexadecimalToBinary, textToNumberUnicode, numberUnicodeToText, textToBinary, binaryToText, textToHexadecimal, hexadecimalToText }; - function executeFunction(option, value) { return convertEncoding[option](value); } -exports.convertEncodingOutput = ({ res, next }, argsObject) => { +module.exports = convertEncodingOutput = ({ res, next }, argsObject) => { let { value, functionName } = argsObject; // S'il n'y a pas les champs obligatoire @@ -227,9 +226,9 @@ exports.convertEncodingOutput = ({ res, next }, argsObject) => { return errorHandling(next, requiredFields); } - // Si la fonction de convertEncoding n'existe pas + // Si la fonction n'existe pas if (!convertEncoding.hasOwnProperty(functionName)) { - return errorHandling(next, { message: "Cette conversion de convertEncoding n'existe pas." }); + return errorHandling(next, { message: "Cette conversion n'existe pas.", statusCode: 400 }); } const result = executeFunction(functionName, value); diff --git a/api/assets/functions/main/convertRomanArabicNumbers.js b/api/assets/functions/main/convertRomanArabicNumbers.js index 9b5842c..8c578fb 100644 --- a/api/assets/functions/main/convertRomanArabicNumbers.js +++ b/api/assets/functions/main/convertRomanArabicNumbers.js @@ -1,5 +1,6 @@ const errorHandling = require('../../utils/errorHandling'); const { requiredFields, generalError } = require('../../config/errors'); +const formatNumberResult = require('../secondary/formatNumberResult'); /* Variable pour convertRomanArabicNumbers */ const correspondancesRomainArabe = [ @@ -20,7 +21,6 @@ const correspondancesRomainArabe = [ /** * @description Convertis un nombre arabe en nombre romain. - * @requires {@link correspondancesRomainArabe} * @param {Number} nombre - Le nombre arabe à convertir * @returns {String} * @examples convertArabicToRoman(24) → 'XXIV' @@ -45,14 +45,13 @@ function convertArabicToRoman(nombre) { /** * @description Convertis un nombre romain en nombre arabe. - * @requires {@link correspondancesRomainArabe} * @param {String} str - Le nombre romain à convertir * @returns {Number} * @examples convertRomanToArabic('XXIV') → 24 */ function convertRomanToArabic(str) { let result = 0; - for (let i = 0;i < correspondancesRomainArabe.length; i++) { + for (let i = 0; i < correspondancesRomainArabe.length; i++) { while (str.indexOf(correspondancesRomainArabe[i][1]) === 0) { // Ajout de la valeur décimale au résultat result += correspondancesRomainArabe[i][0]; @@ -67,27 +66,25 @@ function convertRomanToArabic(str) { } /* OUTPUTS */ -exports.convertRomanToArabicOutput = ({ res, next }, argsObject) => { - let { romanNumber } = argsObject; +const convertRomanToArabicOutput = ({ res, next }, number) => { // S'il n'y a pas les champs obligatoire - if (!(romanNumber)) { + if (!(number)) { return errorHandling(next, requiredFields); } // Formate le paramètre - romanNumber = romanNumber.toUpperCase(); + number = number.toUpperCase(); - const result = convertRomanToArabic(romanNumber); + const result = convertRomanToArabic(number); if (result === 0) { return errorHandling(next, generalError); } - return res.status(200).json({ result }); + return res.status(200).json({ result, resultHTML: `

${number} s'écrit ${result} en chiffres arabes.

` }); } -exports.convertArabicToRomanOutput = ({ res, next }, argsObject) => { - let { number } = argsObject; +const convertArabicToRomanOutput = ({ res, next }, number) => { // S'il n'y a pas les champs obligatoire if (!(number)) { @@ -100,5 +97,27 @@ exports.convertArabicToRomanOutput = ({ res, next }, argsObject) => { return errorHandling(next, { message: "Veuillez rentré un nombre valide.", statusCode: 400 }); } - return res.status(200).json({ result: convertArabicToRoman(number) }); + const result = convertArabicToRoman(number); + return res.status(200).json({ result, resultHTML: `

${formatNumberResult(number)} s'écrit ${result} en chiffres romains.

` }); +} + +const convertRomanArabicObject = { convertRomanToArabicOutput, convertArabicToRomanOutput }; +function executeFunction(option, value, { res, next }) { + return convertRomanArabicObject[option]({ res, next}, value); +} + +module.exports = convertRomanArabicNumbersOutput = ({ res, next }, argsObject) => { + let { value, functionName } = argsObject; + + // S'il n'y a pas les champs obligatoire + if (!(value && functionName)) { + return errorHandling(next, requiredFields); + } + + // Si la fonction n'existe pas + if (!convertRomanArabicObject.hasOwnProperty(functionName)) { + return errorHandling(next, { message: "Cette conversion n'existe pas.", statusCode: 400 }); + } + + executeFunction(functionName, value, { res, next }); } \ No newline at end of file diff --git a/api/assets/functions/main/convertTemperature.js b/api/assets/functions/main/convertTemperature.js index d40eb83..9feb0bd 100644 --- a/api/assets/functions/main/convertTemperature.js +++ b/api/assets/functions/main/convertTemperature.js @@ -27,7 +27,7 @@ function convertTemperature(degree, unit) { } /* OUTPUTS */ -exports.convertTemperatureOutput = ({ res, next }, argsObject) => { +module.exports = convertTemperatureOutput = ({ res, next }, argsObject) => { let { degree, unitToConvert } = argsObject; // S'il n'y a pas les champs obligatoire diff --git a/api/assets/functions/main/heapAlgorithm.js b/api/assets/functions/main/heapAlgorithm.js index 90d08b1..976630e 100644 --- a/api/assets/functions/main/heapAlgorithm.js +++ b/api/assets/functions/main/heapAlgorithm.js @@ -28,7 +28,7 @@ function heapAlgorithm(string) { } /* OUTPUTS */ -exports.heapAlgorithmOutput = ({ res, next }, argsObject) => { +module.exports = heapAlgorithmOutput = ({ res, next }, argsObject) => { let { string } = argsObject; // S'il n'y a pas les champs obligatoire diff --git a/api/assets/functions/main/randomNumber.js b/api/assets/functions/main/randomNumber.js index 78571a0..e3dfdc6 100644 --- a/api/assets/functions/main/randomNumber.js +++ b/api/assets/functions/main/randomNumber.js @@ -14,7 +14,7 @@ function randomNumber(min, max) { } /* OUTPUTS */ -exports.randomNumberOutput = ({ res, next }, argsObject) => { +module.exports = randomNumberOutput = ({ res, next }, argsObject) => { let { min, max } = argsObject; // S'il n'y a pas les champs obligatoire diff --git a/api/assets/functions/main/weatherRequest.js b/api/assets/functions/main/weatherRequest.js index 7ef48a1..0c92d78 100644 --- a/api/assets/functions/main/weatherRequest.js +++ b/api/assets/functions/main/weatherRequest.js @@ -3,7 +3,7 @@ const Queue = require('smart-request-balancer'); const errorHandling = require('../../utils/errorHandling'); const { requiredFields } = require('../../config/errors'); const { WEATHER_API_KEY } = require('../../config/config'); -const { dateTimeUTC } = require('../secondary/dateTimeManagement'); +const dateTimeUTC = require('../secondary/dateTimeManagement'); const capitalize = require('../secondary/capitalize'); const queue = new Queue({ @@ -22,7 +22,7 @@ const queue = new Queue({ }); /* OUTPUTS */ -exports.weatherRequestOutput = ({ res, next }, argsObject) => { +module.exports = weatherRequestOutput = ({ res, next }, argsObject) => { let { cityName } = argsObject; // S'il n'y a pas les champs obligatoire diff --git a/api/assets/functions/secondary/dateTimeManagement.js b/api/assets/functions/secondary/dateTimeManagement.js index f05a707..8473d49 100644 --- a/api/assets/functions/secondary/dateTimeManagement.js +++ b/api/assets/functions/secondary/dateTimeManagement.js @@ -1,7 +1,3 @@ -const timeNow = new Date(); -const utcOffset = timeNow.getTimezoneOffset(); -timeNow.setMinutes(timeNow.getMinutes() + utcOffset); - /** * @description Donne la date et l'heure selon l'UTC (Universal Time Coordinated). * @param {String} utc Heure de décalage par rapport à l'UTC @@ -9,9 +5,12 @@ timeNow.setMinutes(timeNow.getMinutes() + utcOffset); * @examples dateTimeUTC('0') */ function dateTimeUTC(utc) { + const timeNow = new Date(); + const utcOffset = timeNow.getTimezoneOffset(); + timeNow.setMinutes(timeNow.getMinutes() + utcOffset); const enteredOffset = parseFloat(utc)*60; timeNow.setMinutes(timeNow.getMinutes() + enteredOffset); - return showDateTime(enteredOffset); + return showDateTime(timeNow); } /** @@ -21,7 +20,7 @@ function dateTimeUTC(utc) { * @returns {Object} Retourne un objet contenant l'année, le mois, le jour, l'heure, les minutes, les secondes et la date formaté * @examples dateTimeUTC('0') → dateTimeUTC vous renvoie l'exécution de showDateTime */ -function showDateTime(enteredOffset) { +function showDateTime(timeNow) { const year = timeNow.getFullYear(); const month = ('0'+(timeNow.getMonth()+1)).slice(-2); const day = ('0'+timeNow.getDate()).slice(-2); @@ -38,8 +37,7 @@ function showDateTime(enteredOffset) { second: second, showDateTimeValue: showDateTimeValue }; - timeNow.setMinutes(timeNow.getMinutes() - enteredOffset); return objectDateTime; } -module.exports = { showDateTime, dateTimeUTC }; \ No newline at end of file +module.exports = dateTimeUTC; \ No newline at end of file