0.0.4 • Published 6 years ago

@jsbit/find-matching-route v0.0.4

Weekly downloads
-
License
ISC
Repository
github
Last release
6 years ago

findMatchingRoute

findMatchingRoute(routes, currentPath) => object | null

  • Function for finding a route definition based on some path
  • Useful for building your own spa router.
  • A building block for @jsbit/spa-router.
  • Supports nested routes (as deeply as you need)
  • Not coupled to any rendering framework like React or Preact
const onboardingRoutes = {
  landing: {path: '/landing', component: Landing},
  login: {path: '/login', component: Login}
}

const mainRoutes = {
  profile: {path: '/profile/:profileId', component: Profile},
  onboarding: {routes: onboardingRoutes, component: OnboardingApp}
}

findMatchingRoute(mainRoutes, "/profile/123?query=example")
> {
  component: OnboardingApp,
  args: {profileId: 123, query: 'example'},
  definition: {path: '/profile/:profileId', component: Profile, name: 'onboarding'}
}

findMatchingRoute(mainRoutes, "/owls")
> null