Skip to content

Commit 321dc6d

Browse files
committed
Reduce regex usage.
1 parent 0b7a027 commit 321dc6d

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/resource-mapper.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ResourceMapper {
4444
// Determine the path of an existing file
4545
} else {
4646
// Read all files in the corresponding folder
47-
const filename = /[^/]*$/.exec(pathname)[0]
47+
const filename = pathname.substr(pathname.lastIndexOf('/') + 1)
4848
const folder = `${basePath}${pathname.substr(0, pathname.length - filename.length)}`
4949
const files = await this._readdir(folder)
5050

@@ -81,12 +81,14 @@ class ResourceMapper {
8181

8282
// Removes a possible trailing slash from a path
8383
function removeTrailingSlash (path) {
84-
return path ? path.replace(/\/+$/, '') : ''
84+
const lastPos = path.length - 1
85+
return lastPos < 0 || path[lastPos] !== '/' ? path : path.substr(0, lastPos)
8586
}
8687

8788
// Removes everything beyond the dollar sign from a path
8889
function removeDollarExtension (path) {
89-
return path.replace(/\$.*/, '')
90+
const dollarPos = path.lastIndexOf('$')
91+
return dollarPos < 0 ? path : path.substr(0, dollarPos)
9092
}
9193

9294
// Gets the expected content type based on the extension of the path

0 commit comments

Comments
 (0)