From 0517f1b849a74a158558ff0b40db9660dcd9887d Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Mon, 11 Nov 2024 02:19:44 +0100 Subject: [PATCH] Optimize argument handling in typeofrequest function - Streamlined the parsing of multiple content type arguments for improved readability. - Simplified the logic for handling flattened arguments while maintaining existing functionality. - Ensured consistent behavior for checking request body presence and validating content types. --- index.js | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index a5f1109..ec03765 100644 --- a/index.js +++ b/index.js @@ -116,27 +116,19 @@ function hasbody (req) { * * this.is('html'); // => false * - * @param {String|Array} types... - * @return {String|false|null} + * @param {Object} req + * @param {(String|Array)} types... + * @return {(String|false|null)} * @public */ function typeofrequest (req, types_) { - var types = types_ - // no body - if (!hasbody(req)) { - return null - } - + if (!hasbody(req)) return null // support flattened arguments - if (arguments.length > 2) { - types = new Array(arguments.length - 1) - for (var i = 0; i < types.length; i++) { - types[i] = arguments[i + 1] - } - } - + var types = arguments.length > 2 + ? Array.prototype.slice.call(arguments, 1) + : types_ // request content type var value = req.headers['content-type']