All files / src RouteUtils.js

100% Statements 15/15
100% Branches 19/19
100% Functions 1/1
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          3x   98x 95x 95x 95x 95x 95x 95x 95x       20x 19x 19x 19x                  
/* @flow */
 
import { matchPath, type Location } from 'react-router'
import type { RouteProps, Route } from './TypeDefinitions'
 
const RouteUtils = {
  create: (item: RouteProps, location?: ?Location): ?Route => {
    if (!item || !item.path) return null
    const routeName = item.path
    const routeMatch = location ? matchPath(location.pathname, item) : null
    const routePath = item.routePath || item.path
    const route = { ...item, path: routePath }
    const match = location && matchPath(location.pathname, route)
    const key = match ? match.url : routeName
    return { key, routeMatch, routeName }
  },
 
  equal(oldRoute: Route, newRoute: Route): boolean {
    if (!oldRoute || !newRoute) return false
    const { routeMatch: oldRouteMatch } = oldRoute
    const { routeMatch: newRouteMatch } = newRoute
    return !!(
      oldRouteMatch &&
      newRouteMatch &&
      oldRouteMatch.url === newRouteMatch.url
    )
  },
}
 
export default RouteUtils