2020-08-03 12:04:07 +02:00
|
|
|
const errorHandling = require('../../utils/errorHandling')
|
|
|
|
const { requiredFields } = require('../../config/errors')
|
|
|
|
const formatNumberResult = require('../secondary/formatNumberResult')
|
|
|
|
|
|
|
|
function minNumber (array) {
|
|
|
|
let minNumber = { index: 0, value: array[0] }
|
|
|
|
for (let index = 1; index < array.length; index++) {
|
|
|
|
const number = array[index]
|
|
|
|
if (number < minNumber.value) {
|
|
|
|
minNumber = { index: index, value: array[index] }
|
2020-05-07 16:45:05 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
}
|
|
|
|
return minNumber
|
2020-05-07 16:45:05 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
function sortArray (array) {
|
|
|
|
const arrayDuplicated = [...array]
|
|
|
|
const resultArray = []
|
|
|
|
while (array.length !== resultArray.length) {
|
|
|
|
const min = minNumber(arrayDuplicated)
|
|
|
|
resultArray.push(min.value)
|
|
|
|
arrayDuplicated.splice(min.index, 1)
|
|
|
|
}
|
|
|
|
return resultArray
|
2020-05-07 16:45:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* OUTPUTS */
|
2020-08-03 12:04:07 +02:00
|
|
|
module.exports = ({ res, next }, argsObject) => {
|
|
|
|
const { numbersList } = argsObject
|
2020-05-07 16:45:05 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
// S'il n'y a pas les champs obligatoire
|
2020-08-03 14:14:45 +02:00
|
|
|
if (!numbersList) {
|
2020-08-03 12:04:07 +02:00
|
|
|
return errorHandling(next, requiredFields)
|
|
|
|
}
|
2020-05-07 16:45:05 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const numbersListArray = numbersList
|
|
|
|
.split(',')
|
|
|
|
.map(number => number.trim().replace(' ', ''))
|
|
|
|
.map(Number)
|
2020-05-07 16:45:05 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
// Si ce n'est pas une liste de nombres
|
|
|
|
if (numbersListArray.includes(NaN)) {
|
2020-08-03 14:14:45 +02:00
|
|
|
return errorHandling(next, {
|
|
|
|
message:
|
|
|
|
'Vous devez rentrer une liste de nombres séparée par des virgules valide.',
|
|
|
|
statusCode: 400
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Si la taille du tableau dépasse LIMIT_ARRAY_LENGTH
|
|
|
|
const LIMIT_ARRAY_LENGTH = 31
|
|
|
|
if (numbersListArray.length >= LIMIT_ARRAY_LENGTH) {
|
2020-08-03 14:14:45 +02:00
|
|
|
return errorHandling(next, {
|
|
|
|
message: `Par souci de performance, vous ne pouvez pas exécuter cette fonction avec une liste de nombres dépassant ${LIMIT_ARRAY_LENGTH -
|
|
|
|
1} nombres.`,
|
|
|
|
statusCode: 400
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
}
|
2020-05-07 16:45:05 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
const result = sortArray(numbersListArray)
|
2020-08-03 14:14:45 +02:00
|
|
|
const resultFormatted = result.map(number => formatNumberResult(number))
|
2020-08-03 12:04:07 +02:00
|
|
|
return res.status(200).json({
|
|
|
|
result,
|
|
|
|
resultFormatted,
|
2020-08-03 14:14:45 +02:00
|
|
|
resultHTML: `<p>La liste de nombres dans l'ordre croissant :<br/> ${resultFormatted.join(
|
|
|
|
', '
|
|
|
|
)}</p>`
|
2020-08-03 12:04:07 +02:00
|
|
|
})
|
|
|
|
}
|