-
Notifications
You must be signed in to change notification settings - Fork 960
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
hasBasename bugfix #544
hasBasename bugfix #544
Conversation
I'm uncomfortable merging this because I think that using a regex in the underlying code is probably too complex for our needs. All we really need to do is make a string comparison that asserts the next char is one of the URL-delimiting characters (i.e. Would you be open to doing a more extensive refactoring here? |
I use the following successfully: export const hasBasename = (path, prefix) =>
path.startsWith(prefix) && "/?#".indexOf(path.charAt(prefix.length)) !== -1; This obviously works in the delimited case, but in the end-of-string case it also works because the |
@Parakleta I added your change to the PR, just also converting |
@mjackson could you please comment on this PR. Is it ready to be merged? |
modules/PathUtils.js
Outdated
@@ -5,7 +5,7 @@ export const stripLeadingSlash = path => | |||
path.charAt(0) === "/" ? path.substr(1) : path | |||
|
|||
export const hasBasename = (path, prefix) => | |||
new RegExp("^" + prefix + "(\\/|\\?|#|$)", "i").test(path) | |||
path.toLowerCase().lastIndexOf(prefix.toLowerCase(), 0) === 0 && "/?#".indexOf(path.charAt(prefix.length)) !== -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a particular reason you're using lastIndexOf
instead of indexOf
here?
Thanks for working on this, @hmate9. Looks great. Just curious about the usage of |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, don’t do that, performance is a dog. Leave it as it was.
ETA: to expand, indexOf
will check every position even though you’re only interested in the first, wasting time. lastIndexOf
only checks the first position because it’s the last it can check and you told it to start there. The substr
example I gave previously is less confusing, but is wasteful because it creates a new string object to check against (although maybe if JS has interning and is smart this might be free).
IMO, If ES6 is OK (or polyfilled or transpiled) then use startsWith
, otherwise use substr
if performance is less important than clarity, otherwise use lastIndexOf
, maybe with a comment explaining what it does.
@Parakleta Are you sure about that? According to the docs:
It wouldn't make sense to look for more than one match if you have already found one. |
You’re assuming you find a match... Put this way, the |
@Parakleta cool, makes sense. Thank you. What do you think @mjackson ? Should I change it back to |
@mjackson could we get an update on this please? |
ping @mjackson This is ready to be merged and is pretty useful fix |
Is this going to be merged soon? |
Any news on this? I just ran into this issue myself. |
we just ran into this as well LGTM! 👍 |
Thanks for the PR, @hmate9, and sorry for sitting on it for so long. I just merged this manually. It will be released in 4.10 and will be present in the 5.0 release as well. |
I think you should be using one of |
Previously it didn't work when
prefix
had special regex characters (eg$
)