All files / src StackUtils.js

100% Statements 19/19
100% Branches 8/8
100% Functions 8/8
100% Lines 15/15

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46              4x   32x 68x               14x 14x                 32x 95x 61x 27x   2x   32x 63x 59x 27x   32x          
/* @flow */
 
import * as React from 'react'
import { type Location, matchPath } from 'react-router'
import shallowEqual from 'fbjs/lib/shallowEqual'
import type { RouteProps } from './TypeDefinitions'
 
const StackUtils = {
  create: (children: ?Array<React$Node>, props: *) => {
    return React.Children.toArray(children).reduce((stack, child) => {
      return [
        ...stack,
        { ...props, ...child.props },
      ]
    }, [])
  },
 
  shallowEqual: (oldStack: Array<*>, newStack: Array<*>): boolean => {
    return oldStack.every((oldItem, index) => {
      return shallowEqual(oldItem, newStack[index])
    })
  },
 
  getHistoryEntries: (
    stack: Array<RouteProps>,
    entries: Array<Location>,
    location: Location,
  ): Array<Location> => {
    const startHistoryIndex = entries.reduce((acc, entry, index) => {
      if (stack.find(item => matchPath(entry.pathname, item))) {
        if (acc === -1) return index
        return acc
      }
      return -1
    }, -1)
    const lastHistoryIndex = entries.reduce((acc, entry, index) => {
      if (index < startHistoryIndex) return -1
      if (location.pathname === entry.pathname) return index
      return acc
    }, -1)
    return entries.slice(startHistoryIndex, lastHistoryIndex + 1)
  },
}
 
export default StackUtils