backend: Ajouts de fonctions à exécuter pour /functions/:functionName
This commit is contained in:
parent
f631873893
commit
fd3224ee42
@ -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
|
||||
|
46
backend/assets/functions/main/armstrongNumber.js
Normal file
46
backend/assets/functions/main/armstrongNumber.js
Normal file
@ -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 1<sup>3</sup> + 5<sup>3</sup> + 3<sup>3</sup> = 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] + "<sup>" + numberStringLength + "</sup>";
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
50
backend/assets/functions/main/convertDistance.js
Normal file
50
backend/assets/functions/main/convertDistance.js
Normal file
@ -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);
|
||||
}
|
@ -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 });
|
||||
}
|
||||
|
52
backend/assets/functions/main/convertTemperature.js
Normal file
52
backend/assets/functions/main/convertTemperature.js
Normal file
@ -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);
|
||||
}
|
@ -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 });
|
||||
}
|
||||
|
14
backend/assets/functions/secondary/formatNumberResult.js
Normal file
14
backend/assets/functions/secondary/formatNumberResult.js
Normal file
@ -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;
|
@ -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;
|
@ -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 });
|
||||
}
|
Reference in New Issue
Block a user