FunctionProject/api/assets/functions/main/convertRomanArabicNumbers.js

123 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-08-03 12:04:07 +02:00
const errorHandling = require('../../utils/errorHandling')
const { requiredFields, generalError } = require('../../config/errors')
const formatNumberResult = require('../secondary/formatNumberResult')
/* Variable pour convertRomanArabicNumbers */
const correspondancesRomainArabe = [
2020-08-03 12:04:07 +02:00
[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.
2020-05-13 00:08:51 +02:00
* @param {number} nombre - Le nombre arabe à convertir
* @returns {string}
* @examples convertArabicToRoman(24) 'XXIV'
*/
2020-08-03 12:04:07 +02:00
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
}
2020-08-03 12:04:07 +02:00
}
2020-08-03 12:04:07 +02:00
correspondancesRomainArabe.forEach(correspondance => {
extraireChiffreRomain(correspondance[0], correspondance[1])
})
2020-08-03 12:04:07 +02:00
return chiffresRomains
}
2020-08-03 12:04:07 +02:00
/**
* @description Convertis un nombre romain en nombre arabe.
2020-05-13 00:08:51 +02:00
* @param {string} string - Le nombre romain à convertir
* @return {number}
* @example convertRomanToArabic('XXIV') 24
*/
2020-08-03 12:04:07 +02:00
function convertRomanToArabic (string) {
let result = 0
correspondancesRomainArabe.forEach((correspondance) => {
while (string.indexOf(correspondance[1]) === 0) {
// Ajout de la valeur décimale au résultat
result += correspondance[0]
// Supprimer la lettre romaine correspondante du début
string = string.replace(correspondance[1], '')
}
2020-08-03 12:04:07 +02:00
})
if (string !== '') {
result = 0
}
return result
}
/* OUTPUTS */
const convertRomanToArabicOutput = ({ res, next }, number) => {
2020-08-03 12:04:07 +02:00
// S'il n'y a pas les champs obligatoire
if (!(number)) {
return errorHandling(next, requiredFields)
}
2020-08-03 12:04:07 +02:00
// Formate le paramètre
number = number.toUpperCase()
2020-08-03 12:04:07 +02:00
const result = convertRomanToArabic(number)
if (result === 0) {
return errorHandling(next, generalError)
}
return res.status(200).json({ result, resultHTML: `<p><span class="important">${number}</span> s'écrit <span class="important">${result}</span> en chiffres arabes.</p>` })
}
const convertArabicToRomanOutput = ({ res, next }, number) => {
2020-08-03 12:04:07 +02:00
// S'il n'y a pas les champs obligatoire
if (!(number)) {
return errorHandling(next, requiredFields)
}
// Si ce n'est pas un nombre
number = parseInt(number)
if (isNaN(number)) {
return errorHandling(next, { message: 'Veuillez rentré un nombre valide.', statusCode: 400 })
}
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>` })
}
2020-08-03 12:04:07 +02:00
const convertRomanArabicObject = { convertRomanToArabicOutput, convertArabicToRomanOutput }
function executeFunction (option, value, { res, next }) {
return convertRomanArabicObject[option]({ res, next }, value)
}
2020-08-03 12:04:07 +02:00
module.exports = ({ res, next }, argsObject) => {
const { value, functionName } = argsObject
2020-08-03 12:04:07 +02:00
// S'il n'y a pas les champs obligatoire
if (!(value && functionName)) {
return errorHandling(next, requiredFields)
}
2020-08-03 12:04:07 +02:00
// Si la fonction n'existe pas
// eslint-disable-next-line
if (!convertRomanArabicObject.hasOwnProperty(functionName)) {
return errorHandling(next, { message: "Cette conversion n'existe pas.", statusCode: 400 })
}
2020-08-03 12:04:07 +02:00
executeFunction(functionName, value, { res, next })
}