https://github.com/chrisru/es6-router
🌐 Simple client side router built in ES6
https://github.com/chrisru/es6-router
es6 es6-router es6-routing jest router routing
Last synced: 4 months ago
JSON representation
🌐 Simple client side router built in ES6
- Host: GitHub
- URL: https://github.com/chrisru/es6-router
- Owner: ChrisRu
- License: mit
- Created: 2017-06-24T19:13:01.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-01-02T13:42:29.000Z (almost 5 years ago)
- Last Synced: 2025-03-18T22:09:36.868Z (8 months ago)
- Topics: es6, es6-router, es6-routing, jest, router, routing
- Language: JavaScript
- Homepage: https://npm.im/es6-router
- Size: 63.5 KB
- Stars: 16
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ES6 Router
[](https://github.com/ChrisRu/es6-router/blob/master/LICENSE.md)
[](https://circleci.com/gh/ChrisRu/es6-router)
A simple client side router built in **ES6** *with 0 dependencies* and TypeScript definitions.
## Usage
```js
const router = new Router({ ... })
.add(() => {
// getPage('/');
})
.add(/about/, () => {
// getPage('about');
})
.add('contact', () => {
// getPage('contact');
});
router.remove('contact');
router.navigate('about');
```
## API
#### constuctor
| Param | Type | Description |
| ---------------------- | ---------------------------- | -------------------------------------- |
| options | Object | Options object |
| options.debug | boolean (false) | Enable debugging |
| options.context | Object (window) | Context to add event listener to |
| options.startListening | boolean (true) | Start listening when router is created |
#### currentRoute ⇒ string
Name of the current route
_Returns_: string - Current route
#### add(route, handler) ⇒ Router
Add a new route
| Param | Type | Description |
| ------- | ------------------------------------------ | ------------------------------------ |
| re | string \| RegExp | Name of route to match |
| handler | function | Method to execute when route matches |
_Returns_: Router - This router instance
#### remove(route, [handler]) ⇒ Router
Remove a route from the routerc
| Param | Type | Description |
| --------- | ------------------------------------------ | -------------------------- |
| re | string \| RegExp | Name of route to remove |
| [handler] | function | Function handler to remove |
_Returns_: Router - This router instance
#### reload() ⇒ Router
Reload the current route
_Returns_: Router - This router instance
#### listen([instance]) ⇒ Router
Start listening for hash changes on the window
| Param | Type | Default | Description |
| ---------- | ---------------- | ------------------- | ----------------------------- |
| [instance] | any | Window | Context to start listening on |
_Returns_: Router - This router instance
#### stopListen([instance]) ⇒ Router
Stop listening for hash changes on the window
| Param | Type | Default | Description |
| ---------- | ---------------- | ------------------- | ---------------------------- |
| [instance] | any | Window | Context to stop listening on |
_Returns_: Router - This router instance
#### navigate(path) ⇒ Router
Navigate router to path
| Param | Type | Description |
| ----- | ------------------- | ------------------------------ |
| path | string | Path to navigate the router to |
_Returns_: Router - This router instance
#### navigateError(hash) ⇒ Router
Navigate to the error page
| Param | Type |
| ----- | ------------------- |
| hash | string |
_Returns_: Router - This router instance
#### cleanPath(path) ⇒ string
Strip the path of slashes and hashes
| Param | Type | Description |
| ----- | ------------------- | ----------------------- |
| path | string | Path to clean of hashes |
_Returns_: string - Cleaned path
#### parseRoute(path) ⇒ string
Parse a route URL to get all parts
| Param | Type | Description |
| ----- | ------------------- | ------------------------- |
| path | string | Route to split into parts |
_Returns_: string[] - Parts of the url
## License
MIT