All files anchor.tsx

100% Statements 16/16
100% Branches 6/6
100% Functions 2/2
100% Lines 14/14

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 31 32 33 3419x   19x 19x 19x   19x 9x 2x     7x 3x     4x             19x 3x   3x   3x            
import * as React from 'react';
 
const MATCHES_AMPERSAND = /&/gi;
const MATCHES_NON_WORD_CHARACTERS = /[\W_]+/gi;
const MATCHES_LEADING_AND_TRAILING_HYPHENS = /(^-+|-+$)/gi;
 
export const getHref = (children?: React.ReactNode, href?: string): string | undefined => {
  if (href) {
    return href;
  }
 
  if (typeof children !== 'string') {
    return undefined;
  }
 
  return children
    .replace(MATCHES_AMPERSAND, '-and-')
    .replace(MATCHES_NON_WORD_CHARACTERS, '-')
    .replace(MATCHES_LEADING_AND_TRAILING_HYPHENS, '')
    .toLowerCase();
}
 
export const Anchor: React.SFC<React.HTMLProps<HTMLAnchorElement>> = (props) => {
  const { children, href, ...remainingProps } = props;
 
  const automaticHref = getHref(children, href);
 
  return (
    <a {...remainingProps} id={automaticHref} href={automaticHref ? '#' + automaticHref : undefined}>
      {children}
    </a>
  );
};