1.0.0 • Published 8 years ago

@carrd/url-path-router v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
8 years ago

url-router

Example

import Router from '@carrd/url-path-router'

const index = () => {
  // le code
}

const account = () => {
  // le code
}

const router = Router()

router.add({
  path: '/',
  handler: index
})

router.add({
  path: '/account/{id}',
  handler: account
})

const match = router.match('/account/1234')

// match.handlers == [index, account]
// match.params == { id: '1234' }
// unmatched == []

Router(options)

Create a URL path router:

const router = Router()

Options:

  • cache - cache route matches (memoization)

router.add(options)

Adds a route, options:

  • path, format:
    • /some/path - match the exact path
    • /some/{path} - dynamic segment
    • /some/{path*} - wildcard segment
    • /some/{path?} - optional segment

router.match(path)

Matches a given path returning an object with the following:

{
  handlers: [], // all matched handlers
  params: {}, // key / value, dynamic segment pairs
  unmatched: [] // unmatched segments
}

License ISC

1.0.0

8 years ago