diff --git a/HISTORY.md b/HISTORY.md index 4695bb0..6812655 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,6 @@ +2.0.1 / 2025-03-27 +========== + 2.0.0 / 2024-08-31 ========== diff --git a/index.js b/index.js index a5f1109..e773845 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'] @@ -215,9 +207,9 @@ function mimeMatch (expected, actual) { } // validate suffix wildcard - if (expectedParts[1].substr(0, 2) === '*+') { + if (expectedParts[1].slice(0, 2) === '*+') { return expectedParts[1].length <= actualParts[1].length + 1 && - expectedParts[1].substr(1) === actualParts[1].substr(1 - expectedParts[1].length) + expectedParts[1].slice(1) === actualParts[1].slice(1 - expectedParts[1].length) } // validate subtype @@ -232,36 +224,26 @@ function mimeMatch (expected, actual) { * Normalize a type and remove parameters. * * @param {string} value - * @return {string} + * @return {(string|null)} * @private */ - function normalizeType (value) { - // parse the type + // Parse the type var type = contentType.parse(value).type - if (!typer.test(type)) { - return null - } - - return type + return typer.test(type) ? type : null } /** * Try to normalize a type and remove parameters. * * @param {string} value - * @return {string} + * @return {(string|null)} * @private */ - function tryNormalizeType (value) { - if (!value) { - return null - } - try { - return normalizeType(value) + return value ? normalizeType(value) : null } catch (err) { return null } diff --git a/package.json b/package.json index 83c572e..08586d2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "type-is", "description": "Infer the content-type of a request.", - "version": "2.0.0", + "version": "2.0.1", "contributors": [ "Douglas Christopher Wilson ", "Jonathan Ong (http://jongleberry.com)"