All files index.js

85.71% Statements 54/63
76.92% Branches 40/52
84.62% Functions 11/13
85.71% Lines 54/63
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                  29x   29x   29x 30x   30x   30x 10x   30x 10x     30x     29x 1x     29x       9x 9x 9x   9x         1x     1x 1x     1x 1x 1x 1x   1x 1x   1x 1x     1x       30x 30x 30x 28x 28x 9x       9x   19x 39x 39x 48x 47x                 30x       15x             6x 6x                           6x 3x     6x                                                                     29x 29x 29x               4x         4x 2x        
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import withSideEffect from 'react-side-effect';
 
import { clone, defaults } from './utils';
 
import { canUseDOM, insertDocumentMeta } from './dom';
 
function reducePropsTostate(propsList) {
  const props = {};
 
  let extend = true;
 
  for (var i = propsList.length - 1; extend && i >= 0; i--) {
    extend = propsList[i].hasOwnProperty('extend');
 
    const _props = clone(propsList[i]);
 
    if (_props.hasOwnProperty('description')) {
      defaults(_props, { meta: { name: { description: _props.description } } });
    }
    if (_props.hasOwnProperty('canonical')) {
      defaults(_props, { link: { rel: { canonical: _props.canonical } } });
    }
 
    defaults(props, _props);
  }
 
  if (props.auto && props.auto.ograph) {
    ograph(props);
  }
 
  return props;
}
 
function handleStateChangeOnClient(props) {
  Eif (canUseDOM) {
    Eif (typeof props.title === 'string') {
      document.title = props.title;
    }
    insertDocumentMeta(getTags(props));
  }
}
 
function ograph(p) {
  Iif (!p.meta) {
    p.meta = {};
  }
  Eif (!p.meta.property) {
    p.meta.property = {};
  }
 
  const group = p.meta.property;
  Eif (group) {
    Eif (p.title && !group['og:title']) {
      group['og:title'] = p.title;
    }
    Eif (p.hasOwnProperty('description') && !group['og:description']) {
      group['og:description'] = p.description;
    }
    Eif (p.hasOwnProperty('canonical') && !group['og:url']) {
      group['og:url'] = p.canonical;
    }
  }
  return p;
}
 
function parseTags(tagName, props = {}) {
  const tags = [];
  const contentKey = tagName === 'link' ? 'href' : 'content';
  Object.keys(props).forEach(groupKey => {
    const group = props[groupKey];
    if (typeof group === 'string') {
      tags.push({
        tagName,
        [groupKey]: group
      });
      return;
    }
    Object.keys(group).forEach(key => {
      const values = Array.isArray(group[key]) ? group[key] : [group[key]];
      values.forEach(value => {
        if (value !== null) {
          tags.push({
            tagName,
            [groupKey]: key,
            [contentKey]: value
          });
        }
      });
    });
  });
  return tags;
}
 
function getTags(_props) {
  return [].concat(
    parseTags('meta', _props.meta),
    parseTags('link', _props.link)
  );
}
 
export function render(meta = {}) {
  let i = 0;
  const tags = [];
 
  function renderTag(entry) {
    const { tagName, ...attr } = entry;
 
    if (tagName === 'meta') {
      return <meta {...attr} key={i++} data-rdm />;
    }
    if (tagName === 'link') {
      return <link {...attr} key={i++} data-rdm />;
    }
    return null;
  }
 
  if (meta.title) {
    tags.push(<title key={i++}>{meta.title}</title>);
  }
 
  return getTags(meta).reduce((acc, entry) => {
    acc.push(renderTag(entry));
    return acc;
  }, tags);
}
 
class DocumentMeta extends Component {
  static propTypes = {
    title: PropTypes.string,
    description: PropTypes.string,
    base: PropTypes.string,
    canonical: PropTypes.string,
    meta: PropTypes.objectOf(
      PropTypes.oneOfType([
        PropTypes.string,
        PropTypes.objectOf(
          PropTypes.oneOfType([
            PropTypes.string,
            PropTypes.arrayOf(PropTypes.string)
          ])
        )
      ])
    ),
    link: PropTypes.objectOf(
      PropTypes.objectOf(
        PropTypes.oneOfType([
          PropTypes.string,
          PropTypes.arrayOf(PropTypes.string)
        ])
      )
    ),
    auto: PropTypes.objectOf(PropTypes.bool)
  };
 
  render() {
    const { children } = this.props;
    const count = React.Children.count(children);
    return count === 1 ? (
      React.Children.only(children)
    ) : count ? (
      <div>{this.props.children}</div>
    ) : null;
  }
}
 
const DocumentMetaWithSideEffect = withSideEffect(
  reducePropsTostate,
  handleStateChangeOnClient
)(DocumentMeta);
 
DocumentMetaWithSideEffect.renderAsReact = function rewindAsReact() {
  return render(DocumentMetaWithSideEffect.rewind());
};
 
export default DocumentMetaWithSideEffect;