From fd3224ee42962b938da612651e4665c2adfd4649 Mon Sep 17 00:00:00 2001 From: Divlo Date: Tue, 17 Mar 2020 18:35:03 +0100 Subject: [PATCH] =?UTF-8?q?backend:=20Ajouts=20de=20fonctions=20=C3=A0=20e?= =?UTF-8?q?x=C3=A9cuter=20pour=20/functions/:functionName?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/assets/functions/functionObject.js | 21 ++++---- .../assets/functions/main/armstrongNumber.js | 46 ++++++++++++++++ .../assets/functions/main/convertDistance.js | 50 ++++++++++++++++++ .../main/convertRomanArabicNumbers.js | 8 +-- .../functions/main/convertTemperature.js | 52 +++++++++++++++++++ backend/assets/functions/main/randomNumber.js | 10 ++-- .../functions/secondary/formatNumberResult.js | 14 +++++ backend/assets/utils/sendResponse.js | 2 +- backend/controllers/functions.js | 6 +-- 9 files changed, 184 insertions(+), 25 deletions(-) create mode 100644 backend/assets/functions/main/armstrongNumber.js create mode 100644 backend/assets/functions/main/convertDistance.js create mode 100644 backend/assets/functions/main/convertTemperature.js create mode 100644 backend/assets/functions/secondary/formatNumberResult.js diff --git a/backend/assets/functions/functionObject.js b/backend/assets/functions/functionObject.js index daa683e..e30bf60 100644 --- a/backend/assets/functions/functionObject.js +++ b/backend/assets/functions/functionObject.js @@ -1,19 +1,16 @@ 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 functionObject = { - randomNumber: { - functionOutput: randomNumberOutput, - args: ["min", "max"] - }, - convertArabicToRoman: { - functionOutput: convertArabicToRomanOutput, - args: ["number"] - }, - convertRomanToArabic: { - functionOutput: convertRomanToArabicOutput, - args: ["romanNumber"] - } + randomNumber : randomNumberOutput, + convertArabicToRoman: convertArabicToRomanOutput, + convertRomanToArabic: convertRomanToArabicOutput, + convertDistance : convertDistanceOutput, + convertTemperature : convertTemperatureOutput, + armstrongNumber : armstrongNumberOutput }; // Choisi la fonction à exécuter diff --git a/backend/assets/functions/main/armstrongNumber.js b/backend/assets/functions/main/armstrongNumber.js new file mode 100644 index 0000000..b224f68 --- /dev/null +++ b/backend/assets/functions/main/armstrongNumber.js @@ -0,0 +1,46 @@ +const sendResponse = require('../../utils/sendResponse'); +const { requiredFields } = require('../../config/errors'); +const formatNumberResult = require('../secondary/formatNumberResult'); + +/** + * @description Vérifie si un nombre fait partie des nombres d'Armstrong. + * @param {Number} number - Le nombre à tester + * @returns {Object} Un objet contenant l'explication en html et le booléen si oui ou non c'est un nombre d'armstrong + * @examples armstrongNumber(153) → 153 est un nombre d'Armstrong, car 13 + 53 + 33 = 153. + */ +function armstrongNumber(number) { + let numberString = number.toString(); + let numberStringLength = numberString.length; + + let result = 0; + let resultString = ""; + for (let index = 0; index < numberStringLength; index++) { + result = result + parseInt(numberString[index]) ** numberStringLength; + resultString = resultString + " + " + numberString[index] + "" + numberStringLength + ""; + } + + const formattedNumber = formatNumberResult(number); + const isArmstrongNumber = (result === number); + return { + isArmstrongNumber, + htmlExplanation: `${formattedNumber} ${isArmstrongNumber ? "" : "n'"}est pas un nombre d'Armstrong, car ${resultString.slice(2)} = ${formatNumberResult(result)}.` + } +} + +/* OUTPUTS */ +exports.armstrongNumberOutput = (res, argsObject) => { + let { number } = argsObject; + + // S'il n'y a pas les champs obligatoire + if (!(number)) { + return sendResponse(res, requiredFields); + } + + // Si ce n'est pas un nombre + number = parseInt(number); + if (isNaN(number) && number >= 0) { + return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 }); + } + + return sendResponse(res, { result: armstrongNumber(number) }, true); +} \ No newline at end of file diff --git a/backend/assets/functions/main/convertDistance.js b/backend/assets/functions/main/convertDistance.js new file mode 100644 index 0000000..3fa5e7a --- /dev/null +++ b/backend/assets/functions/main/convertDistance.js @@ -0,0 +1,50 @@ +const sendResponse = require('../../utils/sendResponse'); +const { requiredFields, generalError } = require('../../config/errors'); + +const correspondancesDistance = ["pm", null, null, "nm", null, null, "µm", null, null, "mm", "cm", "dm", "m", "dam", "hm", "km", null, null, "Mm", null, null, "Gm", null, null, "Tm"]; + +/** + * @description Convertis la longueur (distance) avec les unités allant de picomètre au Téramètre. + * @requires {@link correspondancesDistance} + * @param {Number} firstValue - Le nombre que vous voulez convertir + * @param {String} unitFirstValue - L'unité du nombre que vous voulez convertir + * @param {String} unitFinalValue - L'unité de votre nombre après la conversion + * @returns {Object|Boolean} → false si arguments non valides et sinon un objet contenant la string et le nombre résultat + * @examples convertDistance(500, 'cm', 'm') → { resultNumber: 5, resultString: "5 m" } + */ +function convertDistance(firstValue, unitFirstValue, unitFinalValue) { + const index1 = correspondancesDistance.indexOf(unitFirstValue); + const index2 = correspondancesDistance.indexOf(unitFinalValue); + if (index1 !== -1 && index2 !== -1) { + const difference = index1 - index2; + const result = firstValue * Math.pow(10, difference); + return { + resultNumber: result, + resultString: `${result} ${unitFinalValue}` + }; + } + return false; +} + +/* OUTPUTS */ +exports.convertDistanceOutput = (res, argsObject) => { + let { number, numberUnit, finalUnit } = argsObject; + + // S'il n'y a pas les champs obligatoire + if (!(number && numberUnit && finalUnit)) { + return sendResponse(res, requiredFields); + } + + // Si ce n'est pas un nombre + number = parseInt(number); + if (isNaN(number)) { + return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 }); + } + + const result = convertDistance(number, numberUnit, finalUnit); + if (!result) { + return sendResponse(res, generalError); + } + + return sendResponse(res, { result }, true); +} \ No newline at end of file diff --git a/backend/assets/functions/main/convertRomanArabicNumbers.js b/backend/assets/functions/main/convertRomanArabicNumbers.js index de21aa1..11fb787 100644 --- a/backend/assets/functions/main/convertRomanArabicNumbers.js +++ b/backend/assets/functions/main/convertRomanArabicNumbers.js @@ -88,19 +88,19 @@ exports.convertRomanToArabicOutput = (res, argsObject) => { return sendResponse(res, generalError); } - return sendResponse(res, { result, httpStatus: 200 }, true); + return sendResponse(res, { result }, true); } exports.convertArabicToRomanOutput = (res, argsObject) => { let { number } = argsObject; - number = parseInt(number); - + // S'il n'y a pas les champs obligatoire if (!(number)) { return sendResponse(res, requiredFields); } - + // Si ce n'est pas un nombre + number = parseInt(number); if (isNaN(number)) { return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 }); } diff --git a/backend/assets/functions/main/convertTemperature.js b/backend/assets/functions/main/convertTemperature.js new file mode 100644 index 0000000..fb6dfd1 --- /dev/null +++ b/backend/assets/functions/main/convertTemperature.js @@ -0,0 +1,52 @@ +const sendResponse = require('../../utils/sendResponse'); +const { requiredFields, generalError } = require('../../config/errors'); + +/** + * @description Convertis des °C en °F et l'inverse aussi. + * @param {Number} degree - Nombre de degrès + * @param {String} unit - Unité du nombre (°C ou °F) + * @returns {Object} false si arguments non valides et sinon un objet contenant la string et le nombre résultat + * @examples convertTemperature(23, '°C') → { resultNumber: 73.4, resultString: "73.4 °F" } + */ +function convertTemperature(degree, unit) { + let temperatureValue = 0; + let temperatureUnit; + if (unit === "°C") { + temperatureUnit = "°F"; + temperatureValue = ((degree * 9/5) + 32); + } + else if (unit === "°F") { + temperatureUnit = "°C"; + temperatureValue = (degree - 32) * 5/9; + } + else { + return false; + } + return { + resultNumber: temperatureValue, + resultString: `${temperatureValue} ${temperatureUnit}` + }; +} + +/* OUTPUTS */ +exports.convertTemperatureOutput = (res, argsObject) => { + let { degree, unit } = argsObject; + + // S'il n'y a pas les champs obligatoire + if (!(degree && unit)) { + return sendResponse(res, requiredFields); + } + + // Si ce n'est pas un nombre + degree = parseInt(degree); + if (isNaN(degree)) { + return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 }); + } + + const result = convertTemperature(degree, unit); + if (!result) { + return sendResponse(res, generalError); + } + + return sendResponse(res, { result }, true); +} \ No newline at end of file diff --git a/backend/assets/functions/main/randomNumber.js b/backend/assets/functions/main/randomNumber.js index 4493aa2..d4c5b38 100644 --- a/backend/assets/functions/main/randomNumber.js +++ b/backend/assets/functions/main/randomNumber.js @@ -15,15 +15,15 @@ function randomNumber(min, max) { /* OUTPUTS */ exports.randomNumberOutput = (res, argsObject) => { let { min, max } = argsObject; - min = parseInt(min); - max = parseInt(max); - + // S'il n'y a pas les champs obligatoire if (!(min && max)) { return sendResponse(res, requiredFields); } - - // Si ce n'est pas des nombres + + // Si ce ne sont pas des nombres + min = parseInt(min); + max = parseInt(max); if (isNaN(min) || isNaN(max)) { return sendResponse(res, { result: "Les paramètres min et max doivent être des nombres...", httpStatus: 400 }); } diff --git a/backend/assets/functions/secondary/formatNumberResult.js b/backend/assets/functions/secondary/formatNumberResult.js new file mode 100644 index 0000000..8308cbb --- /dev/null +++ b/backend/assets/functions/secondary/formatNumberResult.js @@ -0,0 +1,14 @@ +/** + * @description Formate un nombre avec des espaces. + * @param {Number} number + * @param {String} separator Le séparateur utilisé pour la virgule (exemple: "." ou ",") + * @returns {String} - Le nombre formaté + * @examples formatNumberResult(76120) → '76 120' + */ +function formatNumberResult(number, separator = ".") { + let parts = number.toString().split(separator); + parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, " "); + return parts.join(separator); +} + +module.exports = formatNumberResult; \ No newline at end of file diff --git a/backend/assets/utils/sendResponse.js b/backend/assets/utils/sendResponse.js index 9adb20b..e090263 100644 --- a/backend/assets/utils/sendResponse.js +++ b/backend/assets/utils/sendResponse.js @@ -5,7 +5,7 @@ * @param {Boolean} isSuccess (false par defaut) */ function sendResponse (res, object, isSuccess = false) { - res.status(object.httpStatus || 200).send({ isSuccess, ...object.customProperties, result: object.result }); + res.status(object.httpStatus || 200).json({ isSuccess, ...object.customProperties, result: object.result }); } module.exports = sendResponse; \ No newline at end of file diff --git a/backend/controllers/functions.js b/backend/controllers/functions.js index 3439f63..704679c 100644 --- a/backend/controllers/functions.js +++ b/backend/controllers/functions.js @@ -2,9 +2,9 @@ const functionToExecute = require('../assets/functions/functionObject'); const sendResponse = require('../assets/utils/sendResponse'); exports.executeFunctionName = (req, res, _next) => { - const functionObject = functionToExecute(req.params.functionName); - if (functionObject !== undefined) { - return functionObject.functionOutput(res, req.body); + const functionOutput = functionToExecute(req.params.functionName); + if (functionOutput !== undefined) { + return functionOutput(res, req.body); } return sendResponse(res, { result: "La fonction n'existe pas.", httpStatus: 404 }); } \ No newline at end of file