Skip to content
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(frontend/controller): highlight full words in search #536

Merged
merged 7 commits into from
May 20, 2020

Conversation

Jujhaar
Copy link
Contributor

@Jujhaar Jujhaar commented May 18, 2020

Summary

Closes #170. Does not change the functionality of the firstLetter search highlighting, but modifies the fullWord highlighting to include all words.

Before

Screen Shot 2020-05-15 at 11 45 09 PM

After

Screen Shot 2020-05-18 at 10 14 55 AM

Screen Shot 2020-05-18 at 12 38 39 PM

First letter search still works

Screen Shot 2020-05-18 at 6 13 22 PM

How was this tested?

I tried the following test cases:

  1. The word is at the beginning of the line.
  2. The word comes after a special character.
  3. Trailing space after the word.
  4. Ensured the first letter search still worked.
    Happy to write unit tests once those exist (if they exist).

Other odds and ends

I didn't have permission to push to ShabadOS/desktop, and the instructions said to fork so that's what I did - not as well versed with the workflow so let me know if I need to change things.

I was surprised at some of the eslint rules, especially the extraneous semicolon being enforced.

Reviewers

@saihaj @Harjot1Singh

@saihaj saihaj requested a review from Harjot1Singh May 18, 2020 23:25
Copy link
Member

@Harjot1Singh Harjot1Singh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your first PR! A huge fan of the way you've put in and documented your work 👌. Really appreciate the thorough testing and listing out of the cases, along with screenshots 💯

The approach is good (scan left + right to widen the highlight boundaries to whitespaces).

Left some comments, minor changes. I think we can evolve this and make it a little more readable + concise.

We should also note this somewhere in the contributing docs - don't commit package-lock.json as part of a PR unless you've specifically updated or installed a package, since resolving merge conflicts across merged branches in the file is a pain (especially when npm i will just regenerate/update it for you)

app/frontend/src/Controller/Search.js Outdated Show resolved Hide resolved
app/frontend/src/Controller/Search.js Outdated Show resolved Hide resolved
app/frontend/src/Controller/Search.js Outdated Show resolved Hide resolved
app/frontend/src/Controller/Search.js Outdated Show resolved Hide resolved
app/frontend/src/Controller/Search.js Outdated Show resolved Hide resolved

/**
* Separates the line into words before the first match, the first match, and after the match.
* TODO: Extend this logic to find every match, rather than simply the first one.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What did you think of this suggestion? Should I create an issue or is it not the correct functionality/so low pri that it's not worth documenting? Normally my workflow would be to create an issue and reference it in this TODO

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I wasn't quite sure why/where else we'd apply this, but I now see that the case that it might apply is when you've got two of the same word within the string. However, I still do not believe we'd ever want to highlight more than what's actually been entered, since a single entry of ihr (har) will not yield a higher match to ihr ihr ihr gun gwvou compared against ihr gun gwo mnw etc.

@bhajneet perhaps you can validate my assumption.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take this TODO out for now.

@Jujhaar
Copy link
Contributor Author

Jujhaar commented May 19, 2020

@Harjot1Singh PTAL

Copy link
Member

@Harjot1Singh Harjot1Singh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely pleased with the refactor, great job! Had 1-2 queries but we're effectively there.

// will be -1. In this case, we want to end at the last position in the line.
const matchEndPosition = wordEndPosition === -1 ? foundPosition + query.length : wordEndPosition
return [
line.substring( 0, matchStartPosition ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you use substring here over slice out of interest? It'll definitely function the same way, just curious. In fact, by doing so, you no longer need L66, since the behaviour of substring is to treat any negative or NaN arguments as 0. So, technically you could scrap L66 entirely, and move the ternary for matchEndPosition to where wordEndPosition is declared (thus, two less variables declared, less cognitive overload).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you use substring here over slice out of interest?

I mostly did it out of cheekiness in the sense that there's no reason to prefer one over the other and I like substring more 😂 but then I didn't want to modify what existed too much so slice was used below. The removal L66 is a good enough reason for me, I'll refactor. If you all prefer I use slice happy to do that too.

move the ternary for matchEndPosition to where wordEndPosition is declared (thus, two
less variables declared, less cognitive overload).

This would require me to write out and evaluate the code twice, right? The two options are:

const wordEndPosition = line.indexOf(  '  ', foundPosition + query.length )
const matchEndPosition = wordEndPosition === -1 ? foundPosition + query.length : wordEndPosition

or

const matchEndPosition = line.indexOf(  '  ', foundPosition + query.length ) === -1 
  ? foundPosition + query.length 
  : line.indexOf( ' ', foundPosition + query.length )

The second one seems more clunky to me, evaluates the same line twice, and I think the extra variable makes things clearer. Is there another way you are thinking that I'm missing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, a nice unintended pro to using substring in that instance ;)

Totally happy with your reasoning and with what you've done above, those are the only two options I can see as well.

app/frontend/src/Controller/Search.js Outdated Show resolved Hide resolved
app/frontend/src/Controller/Search.js Show resolved Hide resolved
@Jujhaar
Copy link
Contributor Author

Jujhaar commented May 20, 2020

@Harjot1Singh ready for another look!

Copy link
Member

@Harjot1Singh Harjot1Singh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're in business 💼

// will be -1. In this case, we want to end at the last position in the line.
const matchEndPosition = wordEndPosition === -1 ? foundPosition + query.length : wordEndPosition
return [
line.substring( 0, matchStartPosition ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, a nice unintended pro to using substring in that instance ;)

Totally happy with your reasoning and with what you've done above, those are the only two options I can see as well.

@Harjot1Singh Harjot1Singh merged commit 308ed63 into shabados:dev May 20, 2020
@Jujhaar Jujhaar deleted the jujhaar_singh-highlight_full_word branch May 20, 2020 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix incorrect highlighting of full word search results
2 participants