All files index.js

100% Statements 24/24
92.59% Branches 25/27
100% Functions 7/7
100% Lines 22/22
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 432x 2x   18x   4x   1x       3x   3x 3x 3x       10x 4x 6x   2x         6x 6x 6x 11x 1x     6x 2x 5x 6x        
import React from 'react';
import interpolate from './interpolate';
 
function Superficial(Component) {
  // Punt on stateless components for now
  if (!Component.render &&
      !(Component.prototype && Component.prototype.render)) {
    return Component;
  }
 
  class Enhanced extends Component {
    render() { return lookify(super.render(), this.props.width); }
  }
  Enhanced.propTypes = { width: React.PropTypes.number };
  Enhanced.prototype.looks = Component.looks;
  return Enhanced;
}
 
function lookify(component, width) {
  if (!component || !component.props ||
      (!component.props.looks && !component.props.children)) return component;
  const style = interpolate(Object.assign({},
    Array.isArray(component.props.looks)
      ? component.props.looks.reduce((look, h) =>
          Object.assign({}, h, look), {})
      : component.props.looks,
    component.props.style,
  ));
  const newProps = { style };
  if (typeof style === 'function') newProps.style = style(width);
  Object.keys(component.props).forEach((k) => {
    if (k !== 'looks' && k !== 'style' && k !== 'children') {
      newProps[k] = component.props[k];
    }
  });
  if (Array.isArray(component.props.children)) {
    newProps.children = component.props.children.map(c => lookify(c, width));
  } else newProps.children = lookify(component.props.children, width);
  return Object.assign({}, component, { props: newProps });
}
 
export default Superficial;