Fix and improve split-issue-pr-search-results#3837
Fix and improve split-issue-pr-search-results#3837fregante merged 19 commits intorefined-github:masterfrom
split-issue-pr-search-results#3837Conversation
|
Good catch. The code is a little verbose but I haven’t looked exactly at how it can be improved |
This cleans up the initial search query string
The search type is only capitalized in GHE
fregante
left a comment
There was a problem hiding this comment.
Untested but it looks right!
Can you add some tests to ensure that the query is cleaned correctly? Also add some tricky queries to ensure that they're not broken by mistake.
|
@fregante Sure, just not sure what a "tricky" query would be 😅 Also I noticed that before the passed Edit: Just testing something for another PR // https://github.com/fregante |
|
I just realized that the regex is acting on the raw string like it did at the beginning of the PR. I’d rather use getQueryParts as previously suggested
This way you don’t also have drop duplicate spaces because it already does that. |
So you mean something like this? set(query) {
const parts = splitQueryParts(query);
this.searchParams.set('q', deduplicate(parts).join(' '));
} |
|
Okay this now are changes then I initially intended 😅 (Not sure if this is the cleanest code for deduplicating the query parts.) |
test/search-query.ts
Outdated
|
|
||
| test('.getQueryParts', t => { | ||
| const query = new SearchQuery(new URLSearchParams('?q=cool+is%3Aissue')); | ||
| const query = new SearchQuery('?q=cool+is%3Aissue'); |
There was a problem hiding this comment.
I'd rather see a more readable
| const query = new SearchQuery('?q=cool+is%3Aissue'); | |
| const query = new SearchQuery(['q', 'cool is:issue']); |
There was a problem hiding this comment.
Sure, so just accept all URLSearchParams options?
URLSearchParams(init?: string | string[][] | Record<string, string> | URLSearchParams)Would be this then
new SearchQuery([['q', 'cool is:issue']]);But it would also work to just pass the not escaped string if it is just for readability in the tests
new SearchQuery('q=cool is:issue');There was a problem hiding this comment.
I just changed SearchQuery to accept all URLSearchParams options.
And then used the {q: 'query'} parameter (thought it was more readable than the entries option).
Hope that is okay otherwise I can also change it back or to something else.
| let indexToKeep = -1; | ||
| for (const value of values) { | ||
| const lastIndex = array.lastIndexOf(value); | ||
| if (lastIndex > indexToKeep) { | ||
| indexToKeep = lastIndex; | ||
| } | ||
| } | ||
|
|
||
| if (indexToKeep === -1) { | ||
| return array; | ||
| } | ||
|
|
||
| return array.filter((value, index) => index >= indexToKeep || !values.includes(value)); |
There was a problem hiding this comment.
This uses only a loop:
| let indexToKeep = -1; | |
| for (const value of values) { | |
| const lastIndex = array.lastIndexOf(value); | |
| if (lastIndex > indexToKeep) { | |
| indexToKeep = lastIndex; | |
| } | |
| } | |
| if (indexToKeep === -1) { | |
| return array; | |
| } | |
| return array.filter((value, index) => index >= indexToKeep || !values.includes(value)); | |
| let wasKeywordFound = false; | |
| const deduplicated = [] | |
| for (const current of array.reverse()) { | |
| const isKeyword = keywords.includes(current); | |
| if (!isKeyword || !wasKeywordFound) { | |
| deduplicated.unshift(current); | |
| wasKeywordFound = wasKeywordFound || isKeyword; | |
| } | |
| } | |
| return deduplicated; |
There was a problem hiding this comment.
I think this might be a bit "slower" in some edgcases (e.g. if the keywords are not in the array at all) but it's more readable 👍
|
Thanks for fixing this! |
Fixes #3824
Steps to test as mentioned in the issue:
The problem was that
createUrlalways appended the nextis:<type>query part.This is also used by Github for the filter and every previous
is:<type>is ignored.But the detection did just check for the first occurence of
is:<type>so the ui wasn't updated correctly.I changed the code to first clear all
is:<type>and then add the new filter.I also improved the detection if multiple
is:<types>are in the search.As a bonus I also added that the language and state search parameters are also copied for each url