From 8574a805dbbc76275f7d07519e99263fcb681da6 Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Wed, 6 Nov 2024 02:39:36 +0100 Subject: [PATCH] Refactor collapseLeadingSlashes function for simplicity and readability Replaced the old implementation of collapseLeadingSlashes, which iterated over each character, with a more concise and efficient version using a regular expression. The new version uses eplace() to collapse leading slashes into a single slash and handles empty strings by returning '/'. This update improves code readability and leverages modern JavaScript string methods. --- index.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 1bee463..9cc2837 100644 --- a/index.js +++ b/index.js @@ -129,16 +129,9 @@ function serveStatic (root, options) { * Collapse all leading slashes into a single slash * @private */ -function collapseLeadingSlashes (str) { - for (var i = 0; i < str.length; i++) { - if (str.charCodeAt(i) !== 0x2f /* / */) { - break - } - } - return i > 1 - ? '/' + str.substr(i) - : str +function collapseLeadingSlashes(str) { + return str.replace(/^\/+/, '/') || '/'; } /**