All files index.js

96.3% Statements 26/27
92% Branches 23/25
100% Functions 6/6
96% Lines 24/25
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 711x 1x   1x 12x     1x 16x     1x 20x         1x 12x 2x 2x 4x 4x             1x 12x     12x   10x 10x 10x 10x   10x     1x         1x                                            
const { cloneElement, createElement, isValidElement } = require('react')
const PropTypes = require('prop-types')
 
const isFunction = (target) => {
  return target && ({}).toString.call(target) === '[object Function]'
}
 
const isObject = (target, isArray = false) => {
  return typeof target === 'object' && target.hasOwnProperty('length') === isArray
}
 
const composeElement = (candidate, props) => {
  return isValidElement(candidate)
    ? cloneElement(candidate, props)
    : createElement(candidate, props)
}
 
const selectWithProps = (withProps) => {
  if (isObject(withProps)) return withProps
  Eif (isObject(withProps, true) && isObject(withProps[0])) {
    return Object.entries(withProps[0]).reduce((memo, [key, value]) => {
      const property = withProps.includes(key) ? { [key]: value } : {}
      return Object.assign({}, memo, property)
    }, {})
  }
 
  return {}
}
 
const Mayre = module.exports = (props) => {
  const shallRenderOf = isFunction(props.when) ? props.when() : props.when
 
  // Avoid unnecessary evaluation
  if (!shallRenderOf && !props.or) return null
 
  const ofProps = selectWithProps(props.with)
  const element = composeElement(props.of, ofProps)
  const eitherProps = props.orWith ? selectWithProps(props.orWith) : ofProps
  const either = composeElement(props.or, eitherProps)
 
  return shallRenderOf ? element : either
}
 
Mayre.defaultProps = {
  or: null,
  with: {}
}
 
Mayre.propTypes = {
  of: PropTypes.oneOfType([
    PropTypes.element,
    PropTypes.func
  ]).isRequired,
  or: PropTypes.oneOfType([
    PropTypes.element,
    PropTypes.func
  ]),
  orWith: PropTypes.oneOfType([
    PropTypes.array,
    PropTypes.object
  ]),
  when: PropTypes.oneOfType([
    PropTypes.bool,
    PropTypes.func
  ]),
  with: PropTypes.oneOfType([
    PropTypes.array,
    PropTypes.object
  ])
}