All files / src/components Hashtag.js

100% Statements 8/8
100% Branches 10/10
100% Functions 1/1
100% Lines 8/8
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 34 35 36 37 38 39 40 41 42 43 44                                    3x 3x   3x 2x     3x 1x     3x             3x              
/**
 * @copyright   2016, Miles Johnson
 * @license     https://opensource.org/licenses/MIT
 * @flow
 */
 
import React, { PropTypes } from 'react';
import Link from './Link';
 
import type { HashtagProps } from '../types';
 
export default function Hashtag({
  children,
  hashtagUrl,
  encodeHashtag = false,
  preserveHash = false,
  ...props
}: HashtagProps) {
  const url = hashtagUrl || '{{hashtag}}';
  let hashtag = children;
 
  if (!preserveHash && hashtag.charAt(0) === '#') {
    hashtag = hashtag.substr(1);
  }
 
  if (encodeHashtag) {
    hashtag = encodeURIComponent(hashtag);
  }
 
  return (
    <Link {...props} href={url.replace('{{hashtag}}', hashtag)}>
      {children}
    </Link>
  );
}
 
Hashtag.propTypes = {
  children: PropTypes.string.isRequired,
  hashtagName: PropTypes.string,
  hashtagUrl: PropTypes.string,
  encodeHashtag: PropTypes.bool,
  preserveHash: PropTypes.bool,
};