-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix: updated combine url fix method #1647
Conversation
daffae8
to
f838689
Compare
Codecov Report
@@ Coverage Diff @@
## master #1647 +/- ##
==========================================
+ Coverage 84.86% 84.88% +0.02%
==========================================
Files 47 47
Lines 2477 2481 +4
Branches 579 580 +1
==========================================
+ Hits 2102 2106 +4
Misses 370 370
Partials 5 5
|
src/lib/utils.ts
Outdated
@@ -143,10 +143,13 @@ export function validateMetadata(object: Package, name: string): Package { | |||
export function combineBaseUrl(protocol: string, host: string | void, prefix?: string | void): string { | |||
let result = `${protocol}://${host}`; | |||
|
|||
if (prefix) { | |||
prefix = prefix.replace(/\/$/, ''); | |||
if (prefix && prefix.length !== 0) { |
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.
if (prefix && prefix.length !== 0) { | |
if (prefix && prefix.length) { |
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.
Already tried that:
Prefer using an optional chain expression instead, as it's more concise and easier to read.eslint(@typescript-eslint/prefer-optional-chain)
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.
What's the point of prefix
property type void
?
Because that's why I can't use the optional chain operator:
if (prefix?.length) {
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.
Why not _.isString()
? We have used lodash
all over the place anyway, also @DanielRuf says is not a critical code, it is called once in a time.
Hi @Grabauskas, thanks for your PR. What exactly does this fix? Can you show a failing example or the relevant issue? |
Ah ok I see. One case that is fixed now. Let me check if this can be simplified.
|
I would propose the following Return Early Pattern based approach: export function combineBaseUrl(protocol: string, host: string | void, prefix?: string | void): string {
let result = `${protocol}://${host}`;
const prefixOnlySlash = prefix === '/';
if (prefix && !prefixOnlySlash) {
if (prefix.endsWith('/')) {
prefix = prefix.slice(0, -1);
}
if (prefix.startsWith('/')) {
return `${result}${prefix}`;
}
return prefix;
}
return result;
} Opinions @verdaccio/collaborators @verdaccio/core-team |
One more test can be added to check if triming is applied. expect(combineBaseUrl("http", 'domain', ' ')).toEqual('http://domain'); |
I don't know if this is a valid or common usecase. |
src/lib/utils.ts
Outdated
|
||
result = prefix.indexOf('/') === 0 ? `${result}${prefix}` : prefix; | ||
result = prefix.length === 0 || prefix.indexOf('/') === 0 ? `${result}${prefix}` : prefix; |
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.
result = prefix.length === 0 || prefix.indexOf('/') === 0 ? `${result}${prefix}` : prefix; | |
result = prefix.length === 0 || prefix.startsWith('/') ? `${result}${prefix}` : prefix; |
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.
All too complicated, also ternary operators are not great to read. See my proposed alternative solution.
Much easier to read, I like it. |
We can trim in the one single place we use this function, I think the job of the method is |
Agreed 👍 |
f838689
to
e50279f
Compare
Updated by @DanielRuf solution. |
expect(combineBaseUrl("http", 'domain', '/prefix/')).toEqual('http://domain/prefix'); | ||
expect(combineBaseUrl("http", 'domain', '/prefix/deep')).toEqual('http://domain/prefix/deep'); | ||
expect(combineBaseUrl("http", 'domain', 'only-prefix')).toEqual('only-prefix'); |
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.
expect(combineBaseUrl("http", 'domain', 'only-prefix')).toEqual('only-prefix');
🤔 ... this outcome is odd ... (not related with this PR but called my attention)
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.
In some cases url_prefix
can be used as domain.
#622 (comment)
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.
Thank u that makes totally sense .. then I would update the scenario with a real domain, a single string is a bit misleading.
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.
Will one of you create anew PR or issue for that?
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.
Thanks @Grabauskas nice catch ! 👏
🤖This thread has been automatically locked 🔒 since there has not been any recent activity after it was closed. |
Type: bug
Added more unit test cases and fixed
combineBaseUrl
method.RegExp
was changed toslice
for optimization.