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');
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
* @requires {@link correspondancesRomainArabe}
|
|
|
|
* @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.
|
|
|
|
* @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++) {
|
|
|
|
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-19 22:59:06 +01:00
|
|
|
exports.convertRomanToArabicOutput = ({ res, next }, argsObject) => {
|
2020-03-17 12:02:12 +01:00
|
|
|
let { romanNumber } = argsObject;
|
|
|
|
|
|
|
|
// S'il n'y a pas les champs obligatoire
|
|
|
|
if (!(romanNumber)) {
|
2020-03-19 22:59:06 +01:00
|
|
|
return errorHandling(next, requiredFields);
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Formate le paramètre
|
|
|
|
try {
|
|
|
|
romanNumber = romanNumber.toUpperCase();
|
|
|
|
}
|
|
|
|
catch {
|
2020-03-19 22:59:06 +01:00
|
|
|
return errorHandling(next, generalError);
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const result = convertRomanToArabic(romanNumber);
|
|
|
|
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-19 22:59:06 +01:00
|
|
|
return res.status(200).json({ result });
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|
|
|
|
|
2020-03-19 22:59:06 +01:00
|
|
|
exports.convertArabicToRomanOutput = ({ res, next }, argsObject) => {
|
2020-03-17 12:02:12 +01:00
|
|
|
let { number } = argsObject;
|
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-19 22:59:06 +01:00
|
|
|
return res.status(200).json({ result: convertArabicToRoman(number) });
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|