all files / modules/ RouteUtils.js

98.28% Statements 57/58
90.24% Branches 37/41
100% Functions 10/10
97.56% Lines 40/41
6 statements, 1 function, 5 branches Ignored     
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      267×     214×     190×   190× 956× 956×     956×         190×     190× 190×   190× 190×   190× 74×   74× 61×   74×     190×                                       148×   148× 193×   190× 190×   190× 158×             148×             111× 70× 41× 12×     111×      
import React from 'react'
import warning from 'warning'
 
function isValidChild(object) {
  return object == null || React.isValidElement(object)
}
 
export function isReactChildren(object) {
  return isValidChild(object) || (Array.isArray(object) && object.every(isValidChild))
}
 
function checkPropTypes(componentName, propTypes, props) {
  componentName = componentName || 'UnknownComponent'
 
  for (const propName in propTypes) {
    Eif (propTypes.hasOwnProperty(propName)) {
      const error = propTypes[propName](props, propName, componentName)
 
      /* istanbul ignore if: error logging */
      if (error instanceof Error)
        warning(false, error.message)
    }
  }
}
 
function createRoute(defaultProps, props) {
  return { ...defaultProps, ...props }
}
 
export function createRouteFromReactElement(element) {
  const type = element.type
  const route = createRoute(type.defaultProps, element.props)
 
  Eif (type.propTypes)
    checkPropTypes(type.displayName || type.name, type.propTypes, route)
 
  if (route.children) {
    const childRoutes = createRoutesFromReactChildren(route.children, route)
 
    if (childRoutes.length)
      route.childRoutes = childRoutes
 
    delete route.children
  }
 
  return route
}
 
/**
 * Creates and returns a routes object from the given ReactChildren. JSX
 * provides a convenient way to visualize how routes in the hierarchy are
 * nested.
 *
 *   import { Route, createRoutesFromReactChildren } from 'react-router'
 *   
 *   const routes = createRoutesFromReactChildren(
 *     <Route component={App}>
 *       <Route path="home" component={Dashboard}/>
 *       <Route path="news" component={NewsFeed}/>
 *     </Route>
 *   )
 *
 * Note: This method is automatically used when you provide <Route> children
 * to a <Router> component.
 */
export function createRoutesFromReactChildren(children, parentRoute) {
  const routes = []
 
  React.Children.forEach(children, function (element) {
    if (React.isValidElement(element)) {
      // Component classes may have a static create* method.
      Eif (element.type.createRouteFromReactElement) {
        const route = element.type.createRouteFromReactElement(element, parentRoute)
 
        if (route)
          routes.push(route)
      } else {
        routes.push(createRouteFromReactElement(element))
      }
    }
  })
 
  return routes
}
 
/**
 * Creates and returns an array of routes from the given object which
 * may be a JSX route, a plain object route, or an array of either.
 */
export function createRoutes(routes) {
  if (isReactChildren(routes)) {
    routes = createRoutesFromReactChildren(routes)
  } else if (routes && !Array.isArray(routes)) {
    routes = [ routes ]
  }
 
  return routes
}