Skip to content

Instantly share code, notes, and snippets.

@camd
Created April 12, 2018 18:51
Show Gist options
  • Save camd/5ae1075a10bbbccf614fef25f758433b to your computer and use it in GitHub Desktop.
Save camd/5ae1075a10bbbccf614fef25f758433b to your computer and use it in GitHub Desktop.
highlightCommonTerms jsx
// 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