Skip to content

Feat/Text highlight #1514

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

Merged
merged 5 commits into from
Sep 13, 2021
Merged

Feat/Text highlight #1514

merged 5 commits into from
Sep 13, 2021

Conversation

lidord-wix
Copy link
Contributor

Description

Update Text highlight method
WOAUILIB-2058

Changelog

Update Text highlight method

@lidord-wix lidord-wix marked this pull request as ready for review September 5, 2021 07:29
@lidord-wix lidord-wix requested a review from ethanshar September 5, 2021 07:29
}
const parts = [];
let val = false;
for (let k = 0; k < indices.length; k++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about using a forEach or even a map in this case?

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 think I can't use map or forEach here since in every iteration I use the current index and the next one (k and k+1) which is not possible with those methods..

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, map and forEach that comes with javascript don't retrieve the Index..
But using lodash's _.forEach or _.map, you also have access to the index

_.map(array, (item, index) => console.log(index))
_.forEach(array, (item, index) => console.log(index))

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 see, but I need the item and the next item..
I can use it that way:
_.forEach(array, (item, index) => console.log(item, array[index + 1]))
but I don't think it makes that more clear..

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I have one last suggestion, let me know what you think (:

What if instead of keeping the indices as couples (which I personally find confusing) you'll keep them as objects of {start, end}, so indices looks like this

[{start: 2, end: 6}, {start: 10, end: 12}, ...]

I think it'll make a easier to understand (:
WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, done :)

Comment on lines 110 to 119
if (i >= 0) {
const newIndex = index + lastWordLength + i;
indices.push(index + lastWordLength + i);
indices.push(index + lastWordLength + i + word.length);
index = newIndex;
lastWordLength = word.length;
} else {
break;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have a suggestion that can shorten this code a little (:
Let me know if it can work..

First, regardless.. lodash has lots of great utils I'd use instead of for..loops when possible, I find them easier to read.

Anyway, there's the _.split util.
You can go over the target string and split it according to the highlight strings array and this way break the strings as you did without having to save their indices.

Here I had to understand you saved the indices in couples of start and end of highlight and it's not really clear at first glance.

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 can't find a way to make it work with the split method.
If I don't keep the indices I can only return a lower case string and not the original one...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I understand.

It's just I'd like to make the code a little more readable.
(Btw, my old code there is not so readable also)

Everything together makes the Text component to look a bit too much so I was trying to think of ways to clean this up a little before it all become legacy code.

...Hmm another thought of how to shorten this up.
Do you think we can take the use case of a single highlight string and use your implementation as well?
(and then we can remove my old implementation)

Technically if the user pass a string (and not an array of strings) we can just pass it to your method wrapped with an array.
WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, we said that if the user passes only one string we highlight all the repeats of this string (as the old method does), and if he passes an array of strings we highlight it according to its order (as the new method does)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh right, I remember there was a good reason for the separation.

@ethanshar ethanshar merged commit 77c924d into master Sep 13, 2021
@lidord-wix lidord-wix deleted the feat/text_highlight branch September 14, 2021 13:57
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.

2 participants