From 61e847453620206e1309b8973b3275065fba212a Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Wed, 6 Nov 2024 02:09:19 +0100 Subject: [PATCH 1/5] Refactor zeroPad to use padStart for improved readability Replaced the manual loop with `padStart` to simplify the code and improve readability. This aligns with modern JavaScript practices, making the function more concise and easier to maintain. No functional changesjust a cleaner, more efficient approach to padding numbers with leading zeros. --- scripts/version-history.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/scripts/version-history.js b/scripts/version-history.js index b8a2b0e..daf631a 100644 --- a/scripts/version-history.js +++ b/scripts/version-history.js @@ -52,12 +52,6 @@ function repeat (str, length) { return out } -function zeroPad (number, length) { - var num = number.toString() - - while (num.length < length) { - num = '0' + num - } - - return num +function zeroPad(number, length) { + return number.toString().padStart(length, '0'); } From 8574a805dbbc76275f7d07519e99263fcb681da6 Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Wed, 6 Nov 2024 02:39:36 +0100 Subject: [PATCH 2/5] 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(/^\/+/, '/') || '/'; } /** From ae5c14148a54864021c921d7de8dc51f8a2b2ca0 Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Wed, 6 Nov 2024 02:39:36 +0100 Subject: [PATCH 3/5] 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..f5de1f0 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(/^\/+/, '/') || '/' } /** From 7fc73c8e5c8b0acf7ec0c0d33d9654a8ccae5bf2 Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Wed, 6 Nov 2024 02:09:19 +0100 Subject: [PATCH 4/5] Refactor zeroPad to use padStart for improved readability Replaced the manual loop with `padStart` to simplify the code and improve readability. This aligns with modern JavaScript practices, making the function more concise and easier to maintain. No functional changesjust a cleaner, more efficient approach to padding numbers with leading zeros. --- scripts/version-history.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/scripts/version-history.js b/scripts/version-history.js index b8a2b0e..0baecb7 100644 --- a/scripts/version-history.js +++ b/scripts/version-history.js @@ -53,11 +53,5 @@ function repeat (str, length) { } function zeroPad (number, length) { - var num = number.toString() - - while (num.length < length) { - num = '0' + num - } - - return num + return number.toString().padStart(length, '0') } From d3f9d3c36b4f1827981688a44334bc75d37abe76 Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Wed, 6 Nov 2024 02:39:36 +0100 Subject: [PATCH 5/5] 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 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index f5de1f0..eac6ce4 100644 --- a/index.js +++ b/index.js @@ -127,6 +127,9 @@ function serveStatic (root, options) { /** * Collapse all leading slashes into a single slash + * + * @param {string} str + * @returns {string} * @private */