Skip to content

Refactor collapseLeadingSlashes function for simplicity and readability #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
14 changes: 5 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -127,18 +127,14 @@ function serveStatic (root, options) {

/**
* Collapse all leading slashes into a single slash
*
* @param {string} str
* @returns {string}
* @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(/^\/+/, '/') || '/'
}

/**
8 changes: 1 addition & 7 deletions scripts/version-history.js
Original file line number Diff line number Diff line change
@@ -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')
}