Skip to content

Commit

Permalink
Refactor collapseLeadingSlashes function for simplicity and readability
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ayoub-Mabrouk committed Nov 6, 2024
1 parent 87c5f09 commit 8574a80
Showing 1 changed file with 2 additions and 9 deletions.
11 changes: 2 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/^\/+/, '/') || '/';
}

/**
Expand Down

0 comments on commit 8574a80

Please sign in to comment.