Created
April 12, 2018 18:51
-
-
Save camd/5ae1075a10bbbccf614fef25f758433b to your computer and use it in GitHub Desktop.
highlightCommonTerms jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Remove disabling when there is more than one export in the file. | |
// eslint-disable-next-line import/prefer-default-export | |
export const highlightCommonTerms2 = function highlightCommonTerms2(text, terms) { | |
const termTokens = terms.split(/[^a-zA-Z0-9_-]+/); | |
const textTokens = text.split(" "); | |
// can we do an "includes" that works on substrings? Otherwise write your own checker | |
return ( | |
<span> | |
{textTokens.map((textElem, idx) => ( | |
termTokens.some(arrElem => (textElem.replace(/\W/g, '') === arrElem)) ? | |
(<strong key={idx}>{textElem} </strong>) : // eslint-disable-line react/no-array-index-key | |
(<span key={idx}>{textElem} </span>) // eslint-disable-line react/no-array-index-key | |
))} | |
</span> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment