Skip to content

Commit ae5c141

Browse files
committed
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.
1 parent 87c5f09 commit ae5c141

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,9 @@ function serveStatic (root, options) {
129129
* Collapse all leading slashes into a single slash
130130
* @private
131131
*/
132-
function collapseLeadingSlashes (str) {
133-
for (var i = 0; i < str.length; i++) {
134-
if (str.charCodeAt(i) !== 0x2f /* / */) {
135-
break
136-
}
137-
}
138132

139-
return i > 1
140-
? '/' + str.substr(i)
141-
: str
133+
function collapseLeadingSlashes (str) {
134+
return str.replace(/^\/+/, '/') || '/'
142135
}
143136

144137
/**

0 commit comments

Comments
 (0)