All files / src/components substring-highlight.tsx

100% Statements 11/11
100% Branches 6/6
100% Functions 1/1
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30              11x 25x 20x   5x 5x 5x 2x   3x 3x 3x 3x                    
import { getLastIndexOfSubstringIgnoreCase } from '../utils';
 
type Props = {
  children: string;
  substring: string;
};
 
const SubstringHighlight = ({ children: string, substring }: Props) => {
  if (!string || !substring) {
    return <>{string}</>;
  }
  const trimmed = substring.trim();
  const i = getLastIndexOfSubstringIgnoreCase(string, trimmed);
  if (i < 0) {
    return <>{string}</>;
  }
  const prestring = string.slice(0, i);
  const highlight = string.slice(i, i + trimmed.length);
  const poststring = string.slice(i + trimmed.length);
  return (
    <>
      {prestring}
      <mark>{highlight}</mark>
      {poststring}
    </>
  );
};
 
export default SubstringHighlight;