2020-03-19 22:59:06 +01:00
|
|
|
const errorHandling = require('../../utils/errorHandling');
|
2020-03-17 12:02:12 +01:00
|
|
|
const { requiredFields, generalError } = require('../../config/errors');
|
2020-03-24 17:02:30 +01:00
|
|
|
const formatNumberResult = require('../secondary/formatNumberResult');
|
2020-03-17 12:02:12 +01:00
|
|
|
|
|
|
|
/* Variable pour convertRomanArabicNumbers */
|
|
|
|
const correspondancesRomainArabe = [
|
|
|
|
[1000, "M"],
|
|
|
|
[900, "CM"],
|
|
|
|
[500, "D"],
|
|
|
|
[400, "CD"],
|
|
|
|
[100, "C"],
|
|
|
|
[90, "XC"],
|
|
|
|
[50, "L"],
|
|
|
|
[40, "XL"],
|
|
|
|
[10, "X"],
|
|
|
|
[9, "IX"],
|
|
|
|
[5, "V"],
|
|
|
|
[4, "IV"],
|
|
|
|
[1, "I"],
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Convertis un nombre arabe en nombre romain.
|
|
|
|
* @param {Number} nombre - Le nombre arabe à convertir
|
|
|
|
* @returns {String}
|
|
|
|
* @examples convertArabicToRoman(24) → 'XXIV'
|
|
|
|
*/
|
|
|
|
function convertArabicToRoman(nombre) {
|
|
|
|
// Initialisation de la variable qui va contenir le résultat de la conversion
|
|
|
|
let chiffresRomains = "";
|
|
|
|
|
|
|
|
function extraireChiffreRomain(valeurLettre, lettres) {
|
|
|
|
while (nombre >= valeurLettre) {
|
|
|
|
chiffresRomains = chiffresRomains + lettres;
|
|
|
|
nombre = nombre - valeurLettre;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
correspondancesRomainArabe.forEach(correspondance => {
|
|
|
|
extraireChiffreRomain(correspondance[0], correspondance[1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return chiffresRomains;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description Convertis un nombre romain en nombre arabe.
|
|
|
|
* @param {String} str - Le nombre romain à convertir
|
|
|
|
* @returns {Number}
|
|
|
|
* @examples convertRomanToArabic('XXIV') → 24
|
|
|
|
*/
|
|
|
|
function convertRomanToArabic(str) {
|
|
|
|
let result = 0;
|
2020-03-24 17:02:30 +01:00
|
|
|
for (let i = 0; i < correspondancesRomainArabe.length; i++) {
|
2020-03-17 12:02:12 +01:00
|
|
|
while (str.indexOf(correspondancesRomainArabe[i][1]) === 0) {
|
|
|
|
// Ajout de la valeur décimale au résultat
|
|
|
|
result += correspondancesRomainArabe[i][0];
|
|
|
|
// Supprimer la lettre romaine correspondante du début
|
|
|
|
str = str.replace(correspondancesRomainArabe[i][1],'');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (str != '') {
|
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OUTPUTS */
|
2020-03-24 17:02:30 +01:00
|
|
|
const convertRomanToArabicOutput = ({ res, next }, number) => {
|
2020-03-17 12:02:12 +01:00
|
|
|
|
|
|
|
// S'il n'y a pas les champs obligatoire
|
2020-03-24 17:02:30 +01:00
|
|
|
if (!(number)) {
|
2020-03-19 22:59:06 +01:00
|
|
|
return errorHandling(next, requiredFields);
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Formate le paramètre
|
2020-03-24 17:02:30 +01:00
|
|
|
number = number.toUpperCase();
|
2020-03-17 12:02:12 +01:00
|
|
|
|
2020-03-24 17:02:30 +01:00
|
|
|
const result = convertRomanToArabic(number);
|
2020-03-17 12:02:12 +01:00
|
|
|
if (result === 0) {
|
2020-03-19 22:59:06 +01:00
|
|
|
return errorHandling(next, generalError);
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:02:30 +01:00
|
|
|
return res.status(200).json({ result, resultHTML: `<p><span class="important">${number}</span> s'écrit <span class="important">${result}</span> en chiffres arabes.</p>` });
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:02:30 +01:00
|
|
|
const convertArabicToRomanOutput = ({ res, next }, number) => {
|
2020-03-17 18:35:03 +01:00
|
|
|
|
2020-03-17 12:02:12 +01:00
|
|
|
// S'il n'y a pas les champs obligatoire
|
|
|
|
if (!(number)) {
|
2020-03-19 22:59:06 +01:00
|
|
|
return errorHandling(next, requiredFields);
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|
2020-03-17 18:35:03 +01:00
|
|
|
|
2020-03-17 12:02:12 +01:00
|
|
|
// Si ce n'est pas un nombre
|
2020-03-17 18:35:03 +01:00
|
|
|
number = parseInt(number);
|
2020-03-17 12:02:12 +01:00
|
|
|
if (isNaN(number)) {
|
2020-03-19 22:59:06 +01:00
|
|
|
return errorHandling(next, { message: "Veuillez rentré un nombre valide.", statusCode: 400 });
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-24 17:02:30 +01:00
|
|
|
const result = convertArabicToRoman(number);
|
|
|
|
return res.status(200).json({ result, resultHTML: `<p><span class="important">${formatNumberResult(number)}</span> s'écrit <span class="important">${result}</span> en chiffres romains.</p>` });
|
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|