This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/api/assets/functions/main/convertDistance.js

51 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-03-19 22:59:06 +01:00
const errorHandling = require('../../utils/errorHandling');
const { requiredFields, generalError } = require('../../config/errors');
const formatNumberResult = require('../secondary/formatNumberResult');
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 {
result,
2020-04-21 18:03:16 +02:00
resultHTML: `<p>${formatNumberResult(firstValue)} ${unitFirstValue} = ${formatNumberResult(result)} ${unitFinalValue}</p>`
};
}
return false;
}
/* OUTPUTS */
module.exports = convertDistanceOutput = ({ res, next }, argsObject) => {
let { number, numberUnit, finalUnit } = argsObject;
// S'il n'y a pas les champs obligatoire
if (!(number && numberUnit && finalUnit)) {
2020-03-19 22:59:06 +01:00
return errorHandling(next, requiredFields);
}
// Si ce n'est pas un nombre
2020-04-21 18:03:16 +02:00
number = parseFloat(number);
if (isNaN(number)) {
2020-03-19 22:59:06 +01:00
return errorHandling(next, { message: "Veuillez rentré un nombre valide.", statusCode: 400 });
}
const result = convertDistance(number, numberUnit, finalUnit);
if (!result) {
return errorHandling(next, generalError);
}
2020-03-19 22:59:06 +01:00
return res.status(200).json(result);
}